dotnet / dotnet/aspnetcore

Microsoft.AspNetCore.OpenApi does not generate a schema for an enum that is a key in a dictionary

Open
#60,163 4 comments 3 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

WebAPI .NET 9
Nuget: Microsoft.AspNetCore.OpenApi 9.0.1

Hello, when I use attribute:

`[ProducesResponseType>(403)]`

the enum values are not generated in the OpenApi.json under the components/schemas section.

The enum is not generated even when using minimal API
`app.MapGet("/minimal-api-approach", () =>
{
return "Hello World!";
}).Produces>();`

`public class WeatherForecastController : ControllerBase
{
public WeatherForecastController() {

[HttpGet(Name = "controller-approach")]
[ProducesResponseType>(401)]
[ProducesResponseType>(403)]

public Task Get()
{
throw new NotImplementedException();
}

public record FailureResult(Dictionary Errors) where T : Enum;

public record ErrorDetail(string Description);

[JsonConverter(typeof(JsonStringEnumConverter))]
public enum AuthErrorType
{
InvalidCredentials,
InvalidLogin
}}`

### Expected Behavior

The expected result is a generated enum values in the components/schemas section.
With NSwag the result is below:

`
"components": {

"schemas": {
"AuthErrorType": {
"type": "integer",
"description": "",
"x-enumNames": [
"InvalidCredentials",
"InvalidLogin"
],
"enum": [
0,
1
]
}
}
}`

### Steps To Reproduce

https://github.com/PatrezDev/openapispec-net-nswag

### Exceptions (if any)

_No response_

### .NET Version

.NET 9

### Anything else?

![Image](https://github.com/user-attachments/assets/964dd486-6560-4bcd-b34f-553ec21c37d7)

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.