Skip to content

OpenApi generator messes up $ref of collection item if that type is used multiple times in same response #60779

@Madajevas

Description

@Madajevas

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Microsoft.AspNetCore.OpenApi v9.0.2

When response type references same type indirectly as collection elements more than once, all $refs but first gets messed up. It tries pointing to array element of first encountered one. See example bellow

builder.Services.AddOpenApi();
// ...
app.MapOpenApi();

app.MapGet("/test", () =>
{
    return new Both();
});

public class Nested { }
public class First
{
    public Nested[] Things { get; set; }
}
public class Second
{
    public Nested[] Things { get; set; }
}
public class Both
{
    public First First { get; set; }
    public Second Second { get; set; }
}

Opening open API json would show spec like so:

{
  // ....
  "components": {
    "schemas": {
      "Both": {
        "type": "object",
        "properties": {
          "first": {
            "$ref": "#/components/schemas/First"
          },
          "second": {
            "$ref": "#/components/schemas/Second"
          }
        }
      },
      "First": {
        "type": "object",
        "properties": {
          "things": {
            "type": "array",
            "items": {
              // correct reference at first encounter
              "$ref": "#/components/schemas/Nested"
            }
          }
        }
      },
      "Nested": {
        "type": "object"
      },
      "Second": {
        "type": "object",
        "properties": {
          "things": {
            "type": "array",
            "items": {
              // invalid at any other
              "$ref": "#/components/schemas/#/properties/first/properties/things/items"
            }
          }
        }
      }
    }
  }
}

Expected Behavior

No response

Steps To Reproduce

Simple repo with code mentioned above: https://github.com/Madajevas/OpenApiComponentReuseIssue/blob/master/OpenApiComponentReuseIssue/Program.cs

Exceptions (if any)

No response

.NET Version

9.0.200

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