Thanks to visit codestin.com
Credit goes to github.com

Skip to content

OpenApi document generation does not generate reference to array items #63735

@IceAndSnow

Description

@IceAndSnow

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I am experiencing an issue when generating an OpenAPI document using the Microsoft.AspNetCore.OpenApi NuGet package. Specifically, this occurs with object types that contain two properties of the same collection type (list of objects).
In version 9.0.9, the OpenAPI document generated for the second property does not contain a proper reference to its schema. In contrast, version 9.0.3 includes a reference, but it is malformed. Below is a minimal, reproducible example that demonstrates the problem:

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.MapOpenApi();
}

app.MapGet("/weatherforecasts", () => new List<WeatherForecast>()).WithName("GetWeatherForecasts");
app.Run();


record WeatherForecast
{
    public required List<WeatherTimepoint> Predicted { get; init; }
    public required List<WeatherTimepoint> Actual { get; init; }
}

record WeatherTimepoint(string Temperature);

When you access the OpenAPI document served at /openapi/v1.json, the schema for the WeatherForecast object shows incorrect definitions for the actual property. Below are the relevant sections from the generated OpenAPI document:

In version 9.0.9:

"WeatherForecast": {
        "required": [
          "predicted",
          "actual"
        ],
        "type": "object",
        "properties": {
          "predicted": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/WeatherTimepoint"
            }
          },
          "actual": {
            "type": "array",
            "items": { }
          }
        }
      }

In version 9.0.3:

"WeatherForecast": {
        "required": [
          "predicted",
          "actual"
        ],
        "type": "object",
        "properties": {
          "predicted": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/WeatherTimepoint"
            }
          },
          "actual": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/#/items/properties/predicted/items"
            }
          }
        }
      }

Expected Behavior

I would expect the schema reference of the two properties to be the same, e.g. something like this:

"WeatherForecast": {
        "required": [
          "predicted",
          "actual"
        ],
        "type": "object",
        "properties": {
          "predicted": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/WeatherTimepoint"
            }
          },
          "actual": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/WeatherTimepoint"
            }
          }
        }
      }

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

9.0.300

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