-
Notifications
You must be signed in to change notification settings - Fork 9.9k
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
Use JSON Property Name attributes when creating ModelState Validation errors #39010
Comments
Thanks for contacting us. We're moving this issue to the |
Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:
|
While this overlooked and downplayed bug is being looked at, what workarounds exist? |
Let's seal these types and name them to be more explicit about what they are associated with (STJ vs NewtonsoftJson and that they have to do with validation) - public class JsonMetadataProvider
+ public sealed class SystemTextJsonValidationMetadataProvider
- public class NewtonsoftJsonMetadataProvider : IDisplayMetadataProvider, IValidationMetadataProvider
+ public sealed class NewtonsoftJsonValidationMetadataProvider : IDisplayMetadataProvider, IValidationMetadataProvider |
@pranavkm just to include we discussed that will not add the providers as default. So, they will need to be included during the configuration using: STJservices..AddControllers(options => options.ModelMetadataDetailsProviders.Add(new SystemTextJsonValidationMetadataProvider())) or services.Configure<MvcOptions>(options =>
options.ModelMetadataDetailsProviders.Add(new SystemTextJsonValidationMetadataProvider(JsonNamingPolicy.CamelCase))); NewtonsoftJsonservices..AddControllers(options => options.ModelMetadataDetailsProviders.Add(new NewtonsoftJsonValidationMetadataProvider ())) or services.Configure<MvcOptions>(options =>
options.ModelMetadataDetailsProviders.Add(new NewtonsoftJsonValidationMetadataProvider (new CamelCaseNamingStrategy()))); |
Any reason why this is not enabled by default? |
@RehanSaeed, the new providers will set the new |
@brunolins16 will there be a way to configure this without AddControllers? I'm using api controllers with ModelState driven Problem Details Also, can this be backported to 6.x? |
@niemyjski can you share a sample app, so, I can provide what you need to do. Right now we do not have plans to backport. |
@brunolins16 here is a link to one of our apps: https://github.com/exceptionless/Exceptionless/blob/master/src/Exceptionless.Web/Startup.cs#L269-L272 |
@niemyjski you can add the provider here: Example: services.AddControllers(o => {
o.Filters.Add<ApiExceptionFilter>();
+ o.ModelMetadataDetailsProviders.Add(new SystemTextJsonValidationMetadataProvider());
o.ModelBinderProviders.Insert(0, new CustomAttributesModelBinderProvider());
o.InputFormatters.Insert(0, new RawRequestBodyFormatter());
}).AddNewtonsoftJson(o => {
o.SerializerSettings.DefaultValueHandling = DefaultValueHandling.Include;
o.SerializerSettings.NullValueHandling = NullValueHandling.Include;
o.SerializerSettings.Formatting = Formatting.Indented;
o.SerializerSettings.ContractResolver = Core.Bootstrapper.GetJsonContractResolver(); // TODO: See if we can resolve this from the di.
}); |
Background and Motivation
Currently, when the model validation produces the ModelErrorDictionary it will use the property name as error's key, as described in the example bellow, however in scenarios as SPA applications it can be difficult to handle the response since most of the time the property name is an implementation detail and not exactly what is expose through the JSON data.
Also, even when the JSON attributes are not used to define the property name the current implementation does not follow the JSON Naming Policy, that have been a complain since 2016 (#5590).
The proposal is to add two new Metadata Providers,
JsonMetadataProvider
andNewtonsoftJsonMetadataProvider
, the last will be added when theAddNewtonsoftJson
is called while the first one could be added by the customer using theMvcOptions
. These new providers will be responsible for read fromNewtonsoft.Json.JsonPropertyAttribute
orSystem.Text.Json.SerializationJsonPropertyNameAttribute
to set the new propertyValidationMetadata.ValidationModelName
that will be used during theMicrosoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationEntry
creation.When the new providers are included, the same example provided will produce the following result:
Related:
Proposed API
namespace Microsoft.AspNetCore.Mvc.ModelBinding.Metadata { public class ValidationMetadata { + public string? ValidationModelName { get; set; } } }
Usage Examples
or
Is also possible remove the default provider, if needed:
The text was updated successfully, but these errors were encountered: