aspnetcore
aspnetcore copied to clipboard
Respect JsonSerializerOptions in validation errors
This pull request introduces enhancements to JSON serialization and validation in the Microsoft.Extensions.Validation library. Key changes include improved handling of JSON naming policies, support for JsonPropertyName attributes, and caching of reflection results to optimize performance. Additionally, test cases have been updated to reflect these changes.
Enhancements to JSON Serialization and Validation:
-
Support for JSON Naming Policies and Attributes:
- Added a private method
GetJsonPropertyNameto determine effective property names during JSON serialization, consideringJsonPropertyNameAttributeand naming policies. - Updated validation logic to use JSON naming policies and
JsonPropertyNameattributes for formatting property names and paths. [1] [2]
- Added a private method
-
Performance Optimization:
- Cached the presence of
DisplayAttributein the constructor ofValidatablePropertyInfoto avoid repeated reflection calls. - Introduced the
HasDisplayAttributemethod to determine if a property or its corresponding record parameter has aDisplayAttribute.
- Cached the presence of
Dependency and Context Updates:
- New Dependencies:
- Added references to
Microsoft.AspNetCore.Http.ExtensionsandSystem.Text.Jsonfor JSON serialization support. [1] [2] [3] - Exposed
JsonSerializerOptionsinValidateContextto retrieve JSON naming policies from the DI container.
- Added references to
Test Case Updates:
- Validation Tests:
- Updated test cases to verify the correct application of JSON naming policies and
JsonPropertyNameattributes in validation error messages. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
- Updated test cases to verify the correct application of JSON naming policies and