Skip to content

Microsoft.AspNetCore.OpenApi: Enum type is missing when using JsonStringEnumConverter #61303

Open
@jeremia

Description

@jeremia

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

We use Microsoft.AspNetCore.OpenApi v9.0.3

We have an endpoint for posting Leads:

public static void MapLeadsApiV1(this IEndpointRouteBuilder app)
{
    var leadsApi = app.MapGroup("/v1/leads").WithTags("Leads");

    leadsApi
        .MapPost(
            "/",
            (LeadV1 lead) => {...});
}

This is a simplified version of LeadV1:

public record LeadV1(
    LeadPurposeV1 Purpose,
);

public enum LeadPurposeV1
{
    GeneralSalesInquiry = 0,
    TestDriveRequest = 1,
}

We always want to expose enums as strings and therefore has the JsonStringEnumConverter registered globally:

builder.Services.Configure<JsonOptions>(options =>
{
    options.SerializerOptions.Converters.Add(new JsonStringEnumConverter());
});

The resulting schema for LeadPurposeV1 does not contain "type": "string" as would be expected:

"LeadPurposeV1": {
  "enum": [
    "GeneralSalesInquiry",
    "TestDriveRequest"
  ]
},

Note! It doesn't matter if the JsonStringEnumConverter is registered globally or just for the property in question.
Note 2: If we don't use JsonStringEnumConverter we get a schema looking like this:

"LeadPurposeV1": {
  "type": "integer"
},

That behaviour looks a lot like this bug which was supposedly fixed a long time ago:
#58195

Expected Behavior

"LeadPurposeV1": {
  "type": "string",
  "enum": [
    "GeneralSalesInquiry",
    "TestDriveRequest"
  ]
},

Steps To Reproduce

  1. Create a REST api with a enum as part of the post body.
  2. Reference Microsoft.AspNetCore.OpenApi v9.0.3
  3. services.AddOpenApi()
  4. app.MapOpenApi()
  5. Run API and navigate to OpenApi spec

Exceptions (if any)

No response

.NET Version

9.0.201

Anything else?

.NET SDK:
Version: 9.0.201
Commit: 071aaccdc2
Workload version: 9.0.200-manifests.a3a1a094
MSBuild version: 17.13.13+1c2026462

Runtime Environment:
OS Name: Windows
OS Version: 10.0.26100
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\9.0.201\

.NET workloads installed:
[aspire]
Installation Source: VS 17.13.35828.75
Manifest Version: 8.2.2/8.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.aspire\8.2.2\WorkloadManifest.json
Install Type: Msi

Configured to use loose manifests when installing new manifests.

Host:
Version: 9.0.3
Architecture: x64
Commit: 831d23e561

.NET SDKs installed:
9.0.104 [C:\Program Files\dotnet\sdk]
9.0.201 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 9.0.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 9.0.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 8.0.14 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 9.0.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
Not set

global.json file:
Not found

Learn more:
https://aka.ms/dotnet/info

Download .NET:
https://aka.ms/dotnet/download

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