aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

Respect JsonSerializerOptions in validation errors

Open captainsafia opened this issue 5 months ago • 1 comments

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 GetJsonPropertyName to determine effective property names during JSON serialization, considering JsonPropertyNameAttribute and naming policies.
    • Updated validation logic to use JSON naming policies and JsonPropertyName attributes for formatting property names and paths. [1] [2]
  • Performance Optimization:

    • Cached the presence of DisplayAttribute in the constructor of ValidatablePropertyInfo to avoid repeated reflection calls.
    • Introduced the HasDisplayAttribute method to determine if a property or its corresponding record parameter has a DisplayAttribute.

Dependency and Context Updates:

  • New Dependencies:
    • Added references to Microsoft.AspNetCore.Http.Extensions and System.Text.Json for JSON serialization support. [1] [2] [3]
    • Exposed JsonSerializerOptions in ValidateContext to retrieve JSON naming policies from the DI container.

Test Case Updates:

  • Validation Tests:
    • Updated test cases to verify the correct application of JSON naming policies and JsonPropertyName attributes in validation error messages. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]

captainsafia avatar Jun 13 '25 05:06 captainsafia