Skip to content

Enum schemas for route parameters are inlined in openapi spec #60789

Open
@sbomer

Description

@sbomer

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

A simple web api that has an enum route parameter does not use $ref for the enum parameter in the openapi spec.

Note that:

  • Using the enum from multiple route handlers fixes it.
  • Viewing the spec in a browser shows the inlined enum on first load, but has the $ref on reload.

Expected Behavior

The generated spec should use a $ref for the enum, according to https://learn.microsoft.com/en-us/aspnet/core/fundamentals/openapi/customize-openapi?view=aspnetcore-9.0#customize-schema-reuse:
"Schemas for enum types are always replaced with a $ref to a schema in components.schemas."

Instead the generated spec has an inlined enum:

    "/myenum/{e}": {
      "get": {
        "tags": [
          "apigen"
        ],
        "parameters": [
          {
            "name": "e",
            "in": "path",
            "required": true,
            "schema": {
              "enum": [
                "ValueA",
                "ValueB"
              ]
            }
          }
        ],

Steps To Reproduce

Generate an openapi spec for a simple app that has an enum route parameter:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.OpenApi;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Text.Json.Serialization;


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

var app = builder.Build();
app.MapGet("/myenum/{e}", (MyEnum e) => 
{
    return Results.Ok(e);
});
app.MapOpenApi();
app.Run();

[JsonConverter(typeof(JsonStringEnumConverter<MyEnum>))]
enum MyEnum {
    ValueA,
    ValueB
}
<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net9.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <OpenApiGenerateDocuments>true</OpenApiGenerateDocuments>
    <OpenApiGenerateDocumentsOptions>--file-name my-open-api</OpenApiGenerateDocumentsOptions>
    <OpenApiDocumentsDirectory>.</OpenApiDocumentsDirectory>
  </PropertyGroup>

  <ItemGroup>
   <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.2" />
   <PackageReference Include="Microsoft.Extensions.ApiDescription.Server" Version="9.0.2" />
  </ItemGroup>

</Project>

Exceptions (if any)

No response

.NET Version

No response

Anything else?

No response

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