Skip to content

.Net OpenApi - JsonStringEnumConverter Enums should explicitly be generated as string type (to be rendered correctly by SwaggerUI) #62022

Open
@steint23

Description

@steint23

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When using SwaggerUI with the built-In OpenAPI interface in .Net 9, enum members are not displayed in the Schemas section.

OpenAPI generates the enum schema:

      "MyStatus": {
        "enum": [
          "MyStatus1",
          "MyStatus2",
          "MyStatus3"
        ]
      },

In SwaggerUI enum members are not displayed in the Schemas section:

Image

Expected Behavior

OpenAPI should generate the schema with type explicitly set to "string":

      "MyStatus": {
        "enum": [
          "MyStatus1",
          "MyStatus2",
          "MyStatus3"
        ],
        "type": "string"
      },

... in order to be rendered correctly by SwaggerUI.

I assume "type": "string" is missing, because SwaggerGen creates the schema exactly like that.
Should JsonStringEnumConverter enums not explicitly be documented in OpenAPI Spec as string type anyway?

Steps To Reproduce

Brand new .NET 9 Web API from template, with OpenAPI enabled.

Define the enum:

[JsonConverter(typeof(JsonStringEnumConverter<MyStatus>))]
public enum MyStatus
{
    MyStatus1,
    MyStatus2,
    MyStatus3
}

Add enum property to the WeatherForecast model:

    public class WeatherForecast
    {
        ... other properties
        public MyStatus Status { get; set; }
    }

Nuget Package "Swashbuckle.AspNetCore.SwaggerUI"

                app.UseSwaggerUI(options =>
                {
                    options.SwaggerEndpoint("/openapi/v1.json", "My API v1");
                });

Launch app
Look at /swagger/index.html
Observe MyStatus in the Schemas section.

Exceptions (if any)

No response

.NET Version

9.0.204

Anything else?

Scalar can deal with the current OpenAPI generator output, enum members are displayed. But Scalar lacks OpenIdConnect support, which makes it currently unusable by our project.

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions