Skip to content

Commit f92b43e

Browse files
Copilotcaptainsafia
andcommitted
Refactor SerializerOptions property to dynamically access JsonOptions from service provider
Co-authored-by: captainsafia <[email protected]>
1 parent 216406a commit f92b43e

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,40 @@ public sealed class ValidateContext
6363

6464
/// <summary>
6565
/// Gets or sets the JSON serializer options to use for property name formatting.
66-
/// When set, property names in validation errors will be formatted according to the
66+
/// When available, property names in validation errors will be formatted according to the
6767
/// PropertyNamingPolicy and JsonPropertyName attributes.
6868
/// </summary>
69-
internal JsonSerializerOptions? SerializerOptions { get; set; }
69+
internal JsonSerializerOptions? SerializerOptions
70+
{
71+
get
72+
{
73+
// If explicit options have been set, use those (primarily for testing)
74+
if (_serializerOptions is not null)
75+
{
76+
return _serializerOptions;
77+
}
78+
79+
// Otherwise try to get them from DI
80+
var jsonOptionsType = Type.GetType("Microsoft.AspNetCore.Http.Json.JsonOptions, Microsoft.AspNetCore.Http.Extensions");
81+
if (jsonOptionsType is null)
82+
{
83+
return null;
84+
}
85+
86+
var jsonOptionsService = ValidationContext.GetService(jsonOptionsType);
87+
if (jsonOptionsService is null)
88+
{
89+
return null;
90+
}
91+
92+
// Get the SerializerOptions property via reflection
93+
var serializerOptionsProperty = jsonOptionsType.GetProperty("SerializerOptions");
94+
return serializerOptionsProperty?.GetValue(jsonOptionsService) as JsonSerializerOptions;
95+
}
96+
set => _serializerOptions = value;
97+
}
98+
99+
private JsonSerializerOptions? _serializerOptions;
70100

71101
internal void AddValidationError(string key, string[] error)
72102
{

src/Http/Routing/src/ValidationEndpointFilterFactory.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.ComponentModel.DataAnnotations;
77
using System.Linq;
88
using System.Reflection;
9-
using Microsoft.AspNetCore.Http.Json;
109
using Microsoft.AspNetCore.Http.Metadata;
1110
using Microsoft.Extensions.DependencyInjection;
1211
using Microsoft.Extensions.Options;
@@ -56,9 +55,7 @@ public static EndpointFilterDelegate Create(EndpointFilterFactoryContext context
5655
{
5756
ValidateContext? validateContext = null;
5857

59-
// Get JsonOptions from DI if available
60-
var jsonOptions = context.HttpContext.RequestServices.GetService<IOptions<JsonOptions>>();
61-
var jsonSerializerOptions = jsonOptions?.Value?.SerializerOptions;
58+
// JsonOptions will be retrieved directly by ValidateContext.SerializerOptions property
6259

6360
for (var i = 0; i < context.Arguments.Count; i++)
6461
{
@@ -80,13 +77,6 @@ public static EndpointFilterDelegate Create(EndpointFilterFactoryContext context
8077
ValidationOptions = options,
8178
ValidationContext = validationContext
8279
};
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-
}
9080
}
9181
else
9282
{

0 commit comments

Comments
 (0)