-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Description
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