Skip to content

OpenApi set JsonOptions for specific endpoint #62420

Open
@dennis-yemelyanov

Description

@dennis-yemelyanov

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

I have a minimal API endpoint using custom JsonSerializerOptions. I need to add OpenApi support for this endpoint using the same custom JSON options:

var options = new JsonSerializerOptions
{ 
    PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower 
};

app.MapGet("/custom", () => Results.Json(new Todo { Name = "Walk dog", IsComplete = false }, options)).Produces<Todo>();

It seems like the only way to configure JsonOptions for OpenApi is to set it globally like this:

builder.Services.ConfigureHttpJsonOptions(options =>
{
    options.SerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower;
});

But this global option is not ideal, since it affects all services and endpoints running in the same project, and it might not be feasible to find and update all of these other endpoints, so adding this global configuration introduces risk of breaking existing code.

Is there any way to pass JsonOptions to OpenApi configuration without affecting any global settings?

Describe the solution you'd like

It would be great if custom JsonOptions could be passed like this:

app.MapGet("/custom", () => Results.Json(new Todo { Name = "Walk dog", IsComplete = false }, options)).Produces<Todo>(jsonOptions: options);

There are a couple of existing issues related to this problem, but they are still open, and it's not clear if there is any plan to address these scenarios:

#35904
#60738

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etc

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions