Skip to content

Commit c9da716

Browse files
committed
Move SerializerOptions property to ValidationOptions
1 parent f035b35 commit c9da716

File tree

7 files changed

+165
-71
lines changed

7 files changed

+165
-71
lines changed

src/Http/Http.Abstractions/src/PublicAPI.Unshipped.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ Microsoft.AspNetCore.Http.Validation.ValidateContext.ValidationContext.get -> Sy
2828
Microsoft.AspNetCore.Http.Validation.ValidateContext.ValidationContext.set -> void
2929
Microsoft.AspNetCore.Http.Validation.ValidateContext.ValidationErrors.get -> System.Collections.Generic.Dictionary<string!, string![]!>?
3030
Microsoft.AspNetCore.Http.Validation.ValidateContext.ValidationErrors.set -> void
31-
Microsoft.AspNetCore.Http.Validation.ValidateContext.SerializerOptions.get -> System.Text.Json.JsonSerializerOptions?
32-
Microsoft.AspNetCore.Http.Validation.ValidateContext.SerializerOptions.set -> void
3331
Microsoft.AspNetCore.Http.Validation.ValidateContext.ValidationOptions.get -> Microsoft.AspNetCore.Http.Validation.ValidationOptions!
3432
Microsoft.AspNetCore.Http.Validation.ValidateContext.ValidationOptions.set -> void
3533
Microsoft.AspNetCore.Http.Validation.ValidationOptions
3634
Microsoft.AspNetCore.Http.Validation.ValidationOptions.MaxDepth.get -> int
3735
Microsoft.AspNetCore.Http.Validation.ValidationOptions.MaxDepth.set -> void
36+
Microsoft.AspNetCore.Http.Validation.ValidationOptions.SerializerOptions.get -> System.Text.Json.JsonSerializerOptions?
37+
Microsoft.AspNetCore.Http.Validation.ValidationOptions.SerializerOptions.set -> void
3838
Microsoft.AspNetCore.Http.Validation.ValidationOptions.Resolvers.get -> System.Collections.Generic.IList<Microsoft.AspNetCore.Http.Validation.IValidatableInfoResolver!>!
3939
Microsoft.AspNetCore.Http.Validation.ValidationOptions.TryGetValidatableParameterInfo(System.Reflection.ParameterInfo! parameterInfo, out Microsoft.AspNetCore.Http.Validation.IValidatableInfo? validatableInfo) -> bool
4040
Microsoft.AspNetCore.Http.Validation.ValidationOptions.TryGetValidatableTypeInfo(System.Type! type, out Microsoft.AspNetCore.Http.Validation.IValidatableInfo? validatableTypeInfo) -> bool

src/Http/Http.Abstractions/src/Validation/ValidatablePropertyInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ protected ValidatablePropertyInfo(
3232
PropertyType = propertyType;
3333
Name = name;
3434
DisplayName = displayName;
35-
35+
3636
// Cache the HasDisplayAttribute result to avoid repeated reflection calls
3737
var property = DeclaringType.GetProperty(Name);
3838
_hasDisplayAttribute = property is not null && HasDisplayAttribute(property);
@@ -73,7 +73,7 @@ public virtual async Task ValidateAsync(object? value, ValidateContext context,
7373
var validationAttributes = GetValidationAttributes();
7474

7575
// Calculate and save the current path
76-
var memberName = GetJsonPropertyName(Name, property, context.SerializerOptions?.PropertyNamingPolicy);
76+
var memberName = GetJsonPropertyName(Name, property, context.ValidationOptions.SerializerOptions?.PropertyNamingPolicy);
7777
var originalPrefix = context.CurrentValidationPath;
7878
if (string.IsNullOrEmpty(originalPrefix))
7979
{
@@ -88,7 +88,7 @@ public virtual async Task ValidateAsync(object? value, ValidateContext context,
8888
// If the property has a [Display] attribute (either on property or record parameter), use DisplayName directly without formatting
8989
context.ValidationContext.DisplayName = _hasDisplayAttribute
9090
? DisplayName
91-
: GetJsonPropertyName(DisplayName, property, context.SerializerOptions?.PropertyNamingPolicy);
91+
: GetJsonPropertyName(DisplayName, property, context.ValidationOptions.SerializerOptions?.PropertyNamingPolicy);
9292
context.ValidationContext.MemberName = memberName;
9393

9494
// Check required attribute first

src/Http/Http.Abstractions/src/Validation/ValidatableTypeInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ public virtual async Task ValidateAsync(object? value, ValidateContext context,
109109
// Format the member name using JsonSerializerOptions naming policy if available
110110
// Note: we don't respect [JsonPropertyName] here because we have no context of the property being validated.
111111
var formattedMemberName = memberName;
112-
if (context.SerializerOptions?.PropertyNamingPolicy != null)
112+
if (context.ValidationOptions.SerializerOptions?.PropertyNamingPolicy != null)
113113
{
114-
formattedMemberName = context.SerializerOptions.PropertyNamingPolicy.ConvertName(memberName);
114+
formattedMemberName = context.ValidationOptions.SerializerOptions.PropertyNamingPolicy.ConvertName(memberName);
115115
}
116116

117117
var key = string.IsNullOrEmpty(originalPrefix) ?

src/Http/Http.Abstractions/src/Validation/ValidateContext.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.ComponentModel.DataAnnotations;
55
using System.Diagnostics.CodeAnalysis;
66
using System.Linq;
7-
using System.Text.Json;
87

98
namespace Microsoft.AspNetCore.Http.Validation;
109

@@ -62,13 +61,6 @@ public sealed class ValidateContext
6261
/// </summary>
6362
public int CurrentDepth { get; set; }
6463

65-
/// <summary>
66-
/// Gets or sets the JSON serializer options to use for property name formatting.
67-
/// When available, property names in validation errors will be formatted according to the
68-
/// PropertyNamingPolicy and JsonPropertyName attributes.
69-
/// </summary>
70-
public JsonSerializerOptions? SerializerOptions { get; set; }
71-
7264
internal void AddValidationError(string key, string[] errors)
7365
{
7466
ValidationErrors ??= [];

src/Http/Http.Abstractions/src/Validation/ValidationOptions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Diagnostics.CodeAnalysis;
55
using System.Reflection;
6+
using System.Text.Json;
67

78
namespace Microsoft.AspNetCore.Http.Validation;
89

@@ -29,6 +30,13 @@ public class ValidationOptions
2930
/// </summary>
3031
public int MaxDepth { get; set; } = 32;
3132

33+
/// <summary>
34+
/// Gets or sets the JSON serializer options to use for property name formatting.
35+
/// When available, property names in validation errors will be formatted according to the
36+
/// PropertyNamingPolicy and JsonPropertyName attributes.
37+
/// </summary>
38+
public JsonSerializerOptions? SerializerOptions { get; set; }
39+
3240
/// <summary>
3341
/// Attempts to get validation information for the specified type.
3442
/// </summary>

0 commit comments

Comments
 (0)