-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Description
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
When JsonOptions
are configured either through AddControllers().AddJsonOptions()
or PostConfigure<JsonOptions>
, endpoints that return IActionResult
respect this configuratrion. Endpoints that return TypedResult
do not.
builder.Services.PostConfigure<JsonOptions>(opt =>
opt.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull);
// this works as expected and omits null values from the response
[HttpGet("{thingId}")]
public async Task<IActionResult> GetThing(string thingId, CancellationToken cancellationToken) =>
await handler.Handle(new GetThingRequest(thingId), cancellationToken) switch
{
{ } thing=> new JsonResult(thing),
_ => NotFound()
};
// this does not work, null values are always included
[HttpGet("{thingId}")]
public async Task<Results<Ok<Thing>, NotFound>> GetThing(string thingId, CancellationToken cancellationToken) =>
await handler.Handle(new GetThingRequest(thingId), cancellationToken) switch
{
{ } thing => TypedResults.Ok(thing ),
_ => TypedResults.NotFound()
};
Changing Ok<Thing>
to JsonHttpResult<Thing>
and passing the DI'd JsonOptions
serializer settings does work correctly but this feels cumbersome and defeats the purpose of it being configured globally.
Expected Behavior
I could see the argument that minimal APIs wouldn't use JsonOptions
that were configured by chaining off AddControllers
because you're not really using the MVC pipeline, but not respecting the options that are configured via PostConfigure is confusing. Having to DI another dependency into your controller or endpoint just to have TypedResult.Json
respect what has already been configured also seems odd. I would expect it to simply use what was configured - same goes for TypedResult.Ok
when your default serialization is JSON.
Steps To Reproduce
No response
Exceptions (if any)
No response
.NET Version
7.0.101
Anything else?
No response