Skip to content

OpenApi: Multiple array properties of the same type do not get the correct type reference #63353

@Jan-Weiss

Description

@Jan-Weiss

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When generating an OpenApi document from a class which has more than one collection property (List, array, IEnumerable) of the same type, only the first property gets the correct reference to the type. The other properties are always Any.

For example this object:

internal record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
    public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}

internal record WeatherForeCasts(WeatherForecast[] Germany, WeatherForecast[] France);

results in the following OpenApi Document:

{
  "openapi": "3.0.1",
  "info": {
    "title": "OpenApiMulitpleListsOfSameType | v1",
    "version": "1.0.0"
  },
  "servers": [
    {
      "url": "https://localhost:7253/"
    }
  ],
  "paths": {
    "/weatherforecasts": {
      "get": {
        "tags": [
          "OpenApiMulitpleListsOfSameType"
        ],
        "operationId": "GetWeatherForecasts",
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/WeatherForeCasts"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "WeatherForecast": {
        "required": [
          "date",
          "temperatureC",
          "summary"
        ],
        "type": "object",
        "properties": {
          "date": {
            "type": "string",
            "format": "date"
          },
          "temperatureC": {
            "type": "integer",
            "format": "int32"
          },
          "summary": {
            "type": "string",
            "nullable": true
          },
          "temperatureF": {
            "type": "integer",
            "format": "int32"
          }
        }
      },
      "WeatherForeCasts": {
        "required": [
          "germany",
          "france"
        ],
        "type": "object",
        "properties": {
          "germany": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/WeatherForecast"
            }
          },
          "france": {
            "type": "array",
            "items": { }
          }
        }
      }
    }
  },
  "tags": [
    {
      "name": "OpenApiMulitpleListsOfSameType"
    }
  ]
}

The properties "Germany" and "France" are both collections of the same type, but only the first (Germany) gets the type referenced in the OpenApi document.

Expected Behavior

Every collection property of the same type gets a reference to that type in the OpenApi document.

Steps To Reproduce

Clone this repository.
Run the Project and navigate to the OpenApi Document (/openapi/v1.json) to see the issue.

Exceptions (if any)

No response

.NET Version

9.0.302

Anything else?

Microsoft.OpenApi version 9.0.8

There is an issue for this in the OpenApi.NET repository: microsoft/OpenAPI.NET#2443

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-area-labelUsed by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions