Skip to content

Invalid openapi.json produced when having properties of the declaring type #58340

Open

Description

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When building the project you will get an invalid openapi file:

look at WeatherForecast2

I think it tries to generate the type definition twice which not may cause this issue directly.

{
  "openapi": "3.0.1",
  "info": {
    "title": "OpenApiTest | v1",
    "version": "1.0.0"
  },
  "paths": {
    "/weatherforecast": {
      "get": {
        "tags": [
          "OpenApiTest"
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/WeatherForecast"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "WeatherForecast": {
        "type": "object",
        "properties": {
          "forecasts": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/WeatherForecast2"
            }
          }
        }
      },
      "WeatherForecast2": {
        "$ref": "#/components/schemas/#/items"
      }
    }
  },
  "tags": [
    {
      "name": "OpenApiTest"
    }
  ]
}

Expected Behavior

Should be this:

{
  "openapi": "3.0.1",
  "info": {
    "title": "OpenApiTest | v1",
    "version": "1.0.0"
  },
  "paths": {
    "/weatherforecast": {
      "get": {
        "tags": [
          "OpenApiTest"
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/WeatherForecast"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "WeatherForecast": {
        "type": "object",
        "properties": {
          "forecasts": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/WeatherForecast"
            }
          }
        }
      }
    }
  },
  "tags": [
    {
      "name": "OpenApiTest"
    }
  ]
}

Steps To Reproduce

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

var app = builder.Build();
app.MapOpenApi();
app.MapGet("/weatherforecast", () =>
{
    return new []{new WeatherForecast()}; // this part seems important - when I'm not returning an error its fine (implicit api description?)
});
app.Run();

public class WeatherForecast
{
    public IEnumerable<WeatherForecast> Forecasts { get; set; } = [];
}
<!-- OpenApiTest.csproj -->
<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net9.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <OpenApiGenerateDocuments>true</OpenApiGenerateDocuments>
    <OpenApiDocumentsDirectory>$(MSBuildProjectDirectory)</OpenApiDocumentsDirectory>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0-rc.2.24474.3" />
    <PackageReference Include="Microsoft.Extensions.ApiDescription.Server" Version="9.0.0-rc.2.24474.3">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>

</Project>

Exceptions (if any)

No response

.NET Version

9.0.100-rc.2.24474.11

Anything else?

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Needs: Attention 👋This issue needs the attention of a contributor, typically because the OP has provided an update.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