dotnet / dotnet/aspnetcore

OpenAPI property-level `[JsonConverter]` on `[AsParameters]` enum ignored when the operation has a request body

Open
#69,156 1 comment 0 reactions 0 assignees View on GitHub
area-minimal feature-openapi
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Describe the bug

When an `[AsParameters]` DTO has an enum property annotated with `[JsonConverter(typeof(JsonStringEnumConverter))]`, the generated schema for that enum is a string enum — unless the same operation also has a request body, in which case the enum is emitted as a bare `{ "type": "integer" }` (no enum values).

### Expected Behavior

Adding a body parameter alongside the `[AsParamters]` parameter should not result in the converter being ignored and type of `integer` showing in the OpenAPI doc.

### Steps To Reproduce

`.csproj` file:

```xml


net10.0
enable
enable



```

`Program.cs` file:

```c#
using System.Text.Json.Serialization;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenApi();
var app = builder.Build();
app.MapOpenApi();

app.MapGet("/without-body", ([AsParameters] QueryWithoutBody query) => query);
app.MapPost("/with-body", ([AsParameters] QueryWithBody query, RequestBody body) => body);

app.Run();

public enum SortWithoutBody { A, B, C }
public enum SortWithBody { A, B, C }

public record QueryWithoutBody
{
[JsonConverter(typeof(JsonStringEnumConverter))]
public SortWithoutBody? Sort { get; init; }
}

public record QueryWithBody
{
[JsonConverter(typeof(JsonStringEnumConverter))]
public SortWithBody? Sort { get; init; }
}

public class RequestBody { public string? Term { get; init; } }
```

In the OpenAPI doc, the schemas show as:

```plaintext
"SortWithoutBody": { "enum": ["A", "B", "C", null] },
"SortWithBody": { "type": "integer" }
```

### Exceptions (if any)

_No response_

### .NET Version

10.0.401

### Anything else?

Using `Microsoft.AspNetCore.OpenApi` 10.0.12

Contributor guide

Open the contributing guide

Research direction

Reproduce the discrepancy using the provided .csproj and Program.cs with AddOpenApi, MapOpenApi, and the two endpoints. Trace OpenAPI schema generation for an [AsParameters] DTO when a request body is also present, then add coverage showing that the property-level JsonStringEnumConverter preserves the enum values and string type.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, openapi
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.