Skip to content

Commit 216406a

Browse files
Copilotcaptainsafia
andcommitted
Make SerializerOptions property internal and retrieve options from DI
Co-authored-by: captainsafia <1857993+captainsafia@users.noreply.github.com>
1 parent 03af06c commit 216406a

File tree

4 files changed

+15
-218
lines changed

4 files changed

+15
-218
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ Microsoft.AspNetCore.Http.Validation.ValidateContext.CurrentDepth.get -> int
2323
Microsoft.AspNetCore.Http.Validation.ValidateContext.CurrentDepth.set -> void
2424
Microsoft.AspNetCore.Http.Validation.ValidateContext.CurrentValidationPath.get -> string!
2525
Microsoft.AspNetCore.Http.Validation.ValidateContext.CurrentValidationPath.set -> void
26-
Microsoft.AspNetCore.Http.Validation.ValidateContext.SerializerOptions.get -> System.Text.Json.JsonSerializerOptions?
27-
Microsoft.AspNetCore.Http.Validation.ValidateContext.SerializerOptions.set -> void
2826
Microsoft.AspNetCore.Http.Validation.ValidateContext.ValidateContext() -> void
2927
Microsoft.AspNetCore.Http.Validation.ValidateContext.ValidationContext.get -> System.ComponentModel.DataAnnotations.ValidationContext!
3028
Microsoft.AspNetCore.Http.Validation.ValidateContext.ValidationContext.set -> void

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ public sealed class ValidateContext
6060
/// This is used to prevent stack overflows from circular references.
6161
/// </summary>
6262
public int CurrentDepth { get; set; }
63-
63+
6464
/// <summary>
6565
/// Gets or sets the JSON serializer options to use for property name formatting.
6666
/// When set, property names in validation errors will be formatted according to the
6767
/// PropertyNamingPolicy and JsonPropertyName attributes.
6868
/// </summary>
69-
public JsonSerializerOptions? SerializerOptions { get; set; }
69+
internal JsonSerializerOptions? SerializerOptions { get; set; }
7070

7171
internal void AddValidationError(string key, string[] error)
7272
{
@@ -108,7 +108,7 @@ internal void AddOrExtendValidationError(string key, string error)
108108
ValidationErrors[formattedKey] = [error];
109109
}
110110
}
111-
111+
112112
private string FormatKey(string key)
113113
{
114114
if (string.IsNullOrEmpty(key) || SerializerOptions?.PropertyNamingPolicy is null)
@@ -123,11 +123,10 @@ private string FormatKey(string key)
123123
return FormatComplexKey(key);
124124
}
125125

126-
// For JsonPropertyName attribute support, we'd need property info
127-
// but for basic usage, apply the naming policy directly
126+
// Apply the naming policy directly
128127
return SerializerOptions.PropertyNamingPolicy.ConvertName(key);
129128
}
130-
129+
131130
private string FormatComplexKey(string key)
132131
{
133132
// Use a more direct approach for complex keys with dots and array indices

src/Http/Http.Abstractions/test/Validation/ValidateContextTests.cs

Lines changed: 0 additions & 207 deletions
This file was deleted.

src/Http/Routing/src/ValidationEndpointFilterFactory.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ public static EndpointFilterDelegate Create(EndpointFilterFactoryContext context
5757
ValidateContext? validateContext = null;
5858

5959
// Get JsonOptions from DI if available
60-
var jsonOptions = context.HttpContext.RequestServices.GetService<IOptions<JsonOptions>>()?.Value;
60+
var jsonOptions = context.HttpContext.RequestServices.GetService<IOptions<JsonOptions>>();
61+
var jsonSerializerOptions = jsonOptions?.Value?.SerializerOptions;
6162

6263
for (var i = 0; i < context.Arguments.Count; i++)
6364
{
@@ -77,9 +78,15 @@ public static EndpointFilterDelegate Create(EndpointFilterFactoryContext context
7778
validateContext = new ValidateContext
7879
{
7980
ValidationOptions = options,
80-
ValidationContext = validationContext,
81-
SerializerOptions = jsonOptions?.SerializerOptions
81+
ValidationContext = validationContext
8282
};
83+
84+
// Set the serializer options via reflection as it's internal
85+
if (jsonSerializerOptions is not null)
86+
{
87+
var serializerOptionsProp = typeof(ValidateContext).GetProperty("SerializerOptions", BindingFlags.NonPublic | BindingFlags.Instance);
88+
serializerOptionsProp?.SetValue(validateContext, jsonSerializerOptions);
89+
}
8390
}
8491
else
8592
{

0 commit comments

Comments
 (0)