question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

When returning a TypedResult from a controller or minimal API endpoint the configured JsonOptions are not used

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 9 months ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
davidfowlcommented, Jan 5, 2023

@brunolins16 can you file an issue so we can build another API that sets the default in all the different cases?

image

1reaction
brunolins16commented, Jan 4, 2023

@MonocleKelso Unfortunately, there are two different JsonOptions

  1. https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.json.jsonoptions?view=aspnetcore-7.0
  2. https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.jsonoptions?view=aspnetcore-7.0

TypedResult depends on #1 and I believe you are configuring #2. Could you try add:

builder.Services.ConfigureHttpJsonOptions(opt =>
    opt.SerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Tutorial: Create a minimal API with ASP.NET Core
Learn how to build a minimal API with ASP.NET Core.
Read more >
How to configure json Name Case policy while using ...
I'm trying to get result from my minimal API who configured in endpoints of my MVC web application my Get action configured like...
Read more >
From MVC to Minimal APIs with ASP.NET Core 6.0 - Ben Foster
ASP.NET 6.0 introduces an alternative way to build HTTP APIs, using the aptly named “Minimal APIs”. This post provides a step-by-step guide ...
Read more >
You Need To Know Minimal API TypedResults #shorts
Comments4 · Goodbye controllers, hello Minimal APIs - Nick Chapsas - NDC London 2023 · How To Improve Performance With EF Compiled Query...
Read more >
STOP Returning IResult in Your .NET Minimal APIs! - YouTube
As a special typed result we also have an awesome union type that ... Fixing unit tests: 03:49 My setup : Camera -...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found