dotnet / dotnet/aspnetcore

OpenApi set JsonOptions for specific endpoint

Open
#62,420 0 comments 0 reactions 0 assignees View on GitHub
area-minimal feature-openapi
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [x] 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:

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

app.MapGet("/custom", () => Results.Json(new Todo { Name = "Walk dog", IsComplete = false }, options)).Produces();
```
It seems like the only way to configure `JsonOptions` for OpenApi is to set it globally like this:
```csharp
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:

```csharp
app.MapGet("/custom", () => Results.Json(new Todo { Name = "Walk dog", IsComplete = false }, options)).Produces(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:

https://github.com/dotnet/aspnetcore/issues/35904
https://github.com/dotnet/aspnetcore/issues/60738

### Additional context

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.