Skip to content

Microsoft.AspNetCore.OpenApi does not render schema when a Type is reused #63054

@jscarle

Description

@jscarle

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Microsoft.AspNetCore.OpenApi v10.0.0-preview.6.25358.103 does not render schema when a Type is reused within different types.

It took me over an hour to tear down my project down to the minimum repro I could encounter, so this is as minimum as I was able to get in order to repro it.

Notice "CityResponse": { } in the Open API document below:

{
  "openapi": "3.1.1",
  "info": {
    "title": "OpenApiBugRepro | v1",
    "version": "1.0.0"
  },
  "servers": [
    {
      "url": "https://localhost:4000/"
    }
  ],
  "paths": {
    "/": {
      "get": {
        "tags": [
          "OpenApiBugRepro"
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProjectResponse"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "AddressResponse": {
        "type": "object",
        "properties": {
          "city": {
            "$ref": "#/components/schemas/CityResponse"
          }
        }
      },
      "BuilderResponse": {
        "type": "object",
        "properties": {
          "address": {
            "$ref": "#/components/schemas/AddressResponse"
          }
        }
      },
      "CityResponse": { },
      "ProjectResponse": {
        "type": "object",
        "properties": {
          "address": {
            "$ref": "#/components/schemas/AddressResponse"
          },
          "builder": {
            "$ref": "#/components/schemas/BuilderResponse"
          }
        }
      }
    }
  },
  "tags": [
    {
      "name": "OpenApiBugRepro"
    }
  ]
}

Expected Behavior

Reused Types should render their schemas.

Steps To Reproduce

.csproj:

<Project Sdk="Microsoft.NET.Sdk.Web">

    <PropertyGroup>
        <TargetFramework>net10.0</TargetFramework>
        <Nullable>enable</Nullable>
        <ImplicitUsings>enable</ImplicitUsings>
        <LangVersion>preview</LangVersion>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.0-preview.6.25358.103"/>
        <PackageReference Include="Microsoft.OpenApi" Version="2.0.0"/>
    </ItemGroup>

</Project>

Program.cs:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddOpenApi();

var app = builder.Build();

app.MapOpenApi();

app.MapGet("/", () =>
{
    var response = new ProjectResponse();
    return TypedResults.Ok(response);
});

app.Run();

internal sealed record ProjectResponse
{
    public AddressResponse Address { get; init; } = new();
    public BuilderResponse Builder { get; init; } = new();
}

internal sealed record BuilderResponse
{
    public AddressResponse Address { get; init; } = new();
}

internal sealed record AddressResponse
{
    public CityResponse City { get; init; } = new();
}

internal sealed record CityResponse
{
    public string Name { get; init; } = "";
}

Exceptions (if any)

No response

.NET Version

10.0.100-preview.6.25358.103

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

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions