Skip to content

[Blazor] Models with "ValidateComplexTypeAttribute" give an error on the backend(server) #17316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
RichardBr opened this issue Nov 22, 2019 · 6 comments
Assignees
Labels
area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug. Done This issue has been fixed feature-blazor-wasm This issue is related to and / or impacts Blazor WebAssembly

Comments

@RichardBr
Copy link

RichardBr commented Nov 22, 2019

Describe the bug

If I have a model that has property decorated with attribute [ValidateComplexType] it works on the Blazor WebAssembly App but I get an error (see below) when that same model is used on the backend (server/api)

To Reproduce

What steps can we follow to reproduce the issue?
Please run the app I have included in the attached file.
The app was generated in visual studio 2019 preview. (Blazor WebAssembly app with ASP.NET Core hosted)
BlazorAppBug1.zip

Got Exceptions? Include both the message and the stack trace
System.InvalidOperationException: ValidateComplexTypeAttribute can only used with ObjectGraphDataAnnotationsValidator.
at System.ComponentModel.DataAnnotations.ValidateComplexTypeAttribute.IsValid(Object value, ValidationContext validationContext)
at System.ComponentModel.DataAnnotations.ValidationAttribute.GetValidationResult(Object value, ValidationContext validationContext)
at Microsoft.AspNetCore.Mvc.DataAnnotations.DataAnnotationsModelValidator.Validate(ModelValidationContext validationContext)
at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.ValidateNode()
at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.VisitComplexType(IValidationStrategy defaultStrategy)
at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.Visit(ModelMetadata metadata, String key, Object model)
at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.VisitChildren(IValidationStrategy strategy)
at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.VisitComplexType(IValidationStrategy defaultStrategy)
at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.Visit(ModelMetadata metadata, String key, Object model)
at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.Validate(ModelMetadata metadata, String key, Object model, Boolean alwaysValidateAtTopLevel)
at Microsoft.AspNetCore.Mvc.ModelBinding.ObjectModelValidator.Validate(ActionContext actionContext, ValidationStateDictionary validationState, String prefix, Object model, ModelMetadata metadata)

@javiercn javiercn added the area-blazor Includes: Blazor, Razor Components label Nov 22, 2019
@mkArtakMSFT mkArtakMSFT added the feature-blazor-wasm This issue is related to and / or impacts Blazor WebAssembly label Nov 22, 2019
@mkArtakMSFT mkArtakMSFT added this to the 5.0.0-preview1 milestone Nov 22, 2019
@mkArtakMSFT mkArtakMSFT added the bug This issue describes a behavior which is not expected - a bug. label Nov 22, 2019
@mkArtakMSFT
Copy link
Member

Thanks for reporting this issue. This seems to be a bug and we've assigned one of our team members to look into this based on its priority.

@pranavkm pranavkm removed the bug This issue describes a behavior which is not expected - a bug. label Nov 23, 2019
@pranavkm pranavkm modified the milestone: blazor-wasm Nov 23, 2019
@mkArtakMSFT mkArtakMSFT added the bug This issue describes a behavior which is not expected - a bug. label Nov 25, 2019
@pranavkm
Copy link
Contributor

This is a bit unfortunate, I hadn't anticipated using this attribute in code shared between Blazor and MVC.

The ValidateComplexTypeAttribute is at this point, only usable when used within the right context. It throws if the context isn't set up correctly. This is to avoid incorrectly using it with the DataAnnotationsValidator component that ships with Blazor. However, this also means using it with MVC results in the error you see above. Once this feature merges in to CoreFx (https://github.com/dotnet/corefx/issues/42384), we can make sure this particular scenario works end to end. However, for the time being there are a couple of options

a) We can require that the attribute does not appear in MVC. You could do this by cross-compiling for ns2.1 and netcoreapp3.1 and #ifdef the attribute decoration.
b) We could have an optional property that allows the validator to no-op if it's being invoked from the wrong context. [ValidateComplexTypeAttribute(SuppressThrowIfInvalidContext = true)]
c) We could not throw for invalid context and just no-op it. We'll rely on users making sure they're using the right validation component when this attribute is used.

@SteveSandersonMS \ @danroth27 what do you think?

@SteveSandersonMS
Copy link
Member

Automatically no-opping seems reasonable given that once we move this into CoreFx it's just going to work.

@knight1219
Copy link

Is there any work around for this current? I just got all of my front end code validating properly only to find that my backend is error'd now. Should I disable backend validation for now and add it back in once this gets fixed?

@pranavkm
Copy link
Contributor

pranavkm commented Mar 2, 2020

@knight1219 the change is shipping as part of the upcoming preview2 release. Outside of having different models on the server and the client, we don't have a very good recommendation as of yet.

@knight1219
Copy link

As the fix is going into preview, will it ship with the May update or get held until .Net 5 releases? Just want to get any work arounds into our company backlog and for when the fix release making sure it doesn't add any regressions when we go to update.

@ghost ghost locked as resolved and limited conversation to collaborators Apr 1, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug. Done This issue has been fixed feature-blazor-wasm This issue is related to and / or impacts Blazor WebAssembly
Projects
None yet
Development

No branches or pull requests

6 participants