-
Couldn't load subscription status.
- Fork 10.5k
Respect JsonSerializerOptions in validation errors #62341
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -3,6 +3,9 @@ | |||||
|
|
||||||
| using System.ComponentModel.DataAnnotations; | ||||||
| using System.Diagnostics.CodeAnalysis; | ||||||
| using System.Reflection; | ||||||
| using System.Text.Json; | ||||||
| using System.Text.Json.Serialization; | ||||||
|
|
||||||
| namespace Microsoft.Extensions.Validation; | ||||||
|
|
||||||
|
|
@@ -13,12 +16,13 @@ namespace Microsoft.Extensions.Validation; | |||||
| public abstract class ValidatablePropertyInfo : IValidatableInfo | ||||||
| { | ||||||
| private RequiredAttribute? _requiredAttribute; | ||||||
| private readonly bool _hasDisplayAttribute; | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// Creates a new instance of <see cref="ValidatablePropertyInfo"/>. | ||||||
| /// </summary> | ||||||
| protected ValidatablePropertyInfo( | ||||||
| [param: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] | ||||||
| [param: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicConstructors)] | ||||||
| Type declaringType, | ||||||
| Type propertyType, | ||||||
| string name, | ||||||
|
|
@@ -28,12 +32,18 @@ protected ValidatablePropertyInfo( | |||||
| PropertyType = propertyType; | ||||||
| Name = name; | ||||||
| DisplayName = displayName; | ||||||
|
|
||||||
| // Cache the HasDisplayAttribute result to avoid repeated reflection calls | ||||||
| // We only check for the existence of the DisplayAttribute here and not the | ||||||
| // Name value itself since we rely on the source generator populating it | ||||||
| var property = DeclaringType.GetProperty(Name); | ||||||
|
||||||
| var property = DeclaringType.GetProperty(Name); | |
| var property = DeclaringType.GetProperty(Name, BindingFlags.Public | BindingFlags.Instance); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and primary ctors?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not quite since there isn't the same case-based 1:1 mapping between property names and parameter names when using primary constructors. It's a variation of #61526.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,8 @@ | |
|
|
||
| using System.ComponentModel.DataAnnotations; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Text.Json; | ||
| using Microsoft.Extensions.Options; | ||
|
|
||
| namespace Microsoft.Extensions.Validation; | ||
|
|
||
|
|
@@ -60,10 +62,55 @@ public sealed class ValidateContext | |
| /// </summary> | ||
| public int CurrentDepth { get; set; } | ||
|
|
||
| private JsonSerializerOptions? _cachedSerializerOptions; | ||
| private bool _serializerOptionsResolved; | ||
|
|
||
| internal JsonSerializerOptions? SerializerOptions | ||
| { | ||
| get | ||
| { | ||
| if (_serializerOptionsResolved) | ||
| { | ||
| return _cachedSerializerOptions; | ||
| } | ||
|
|
||
| _cachedSerializerOptions = ResolveSerializerOptions(); | ||
| _serializerOptionsResolved = true; | ||
| return _cachedSerializerOptions; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Attempts to resolve the <see cref="JsonSerializerOptions"/> used for serialization | ||
| /// using reflection to access JsonOptions from the ASP.NET Core shared framework. | ||
| /// </summary> | ||
| private JsonSerializerOptions? ResolveSerializerOptions() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How AOT friendly is this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's generally safe. The main issue would be the resolution of the I am long overdue for adding a native AoT test project to this package... :/ |
||
| { | ||
| var targetType = "Microsoft.AspNetCore.Http.Json.JsonOptions, Microsoft.AspNetCore.Http.Extensions"; | ||
| var jsonOptionsType = Type.GetType(targetType, throwOnError: false); | ||
| if (jsonOptionsType is null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| var iOptionsType = typeof(IOptions<>).MakeGenericType(jsonOptionsType); | ||
|
|
||
| var optionsObj = ValidationContext.GetService(iOptionsType); | ||
| if (optionsObj is null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| var valueProp = iOptionsType.GetProperty("Value")!; | ||
| var jsonOptions = valueProp.GetValue(optionsObj); | ||
| var serializerProp = jsonOptionsType.GetProperty("SerializerOptions")!; | ||
|
|
||
| return serializerProp.GetValue(jsonOptions) as JsonSerializerOptions; | ||
| } | ||
|
|
||
| internal void AddValidationError(string key, string[] error) | ||
| { | ||
| ValidationErrors ??= []; | ||
|
|
||
| ValidationErrors[key] = error; | ||
| } | ||
|
|
||
|
|
@@ -90,7 +137,7 @@ internal void AddOrExtendValidationError(string key, string error) | |
|
|
||
| if (ValidationErrors.TryGetValue(key, out var existingErrors) && !existingErrors.Contains(error)) | ||
| { | ||
| ValidationErrors[key] = [.. existingErrors, error]; | ||
| ValidationErrors[key] = [..existingErrors, error]; | ||
| } | ||
| else | ||
| { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Comment about why we don't use the name from the attribute
And maybe we should set
DisplayNameto the attributes value if the provided value happens to be null? (I know the API definition says non-null but...)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would this help with?