Skip to content

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

Closed as duplicate

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

Activity

  1. ghost added
    needs-area-labelUsed by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically
    on Mar 6, 2025
  2. added
    area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etc
    and removed
    needs-area-labelUsed by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically
    on Mar 6, 2025
  3. captainsafia commented on Mar 6, 2025

    @captainsafia
    Contributor

    @Madajevas Thanks for filing this issue! This is a dupe of #60381. PR with fix is open at #60410.

  4. DavidNorena commented on Mar 6, 2025

    @DavidNorena

    @captainsafia i see this is impacting several use cases and several teams, seems the PR is already in place, not sure about ETA, but after 3 weeks, what is missing for that PR to be merged into the 9.0 release ?

    Thanks a lot and great work for helping us all to resolve this issue.

  5. captainsafia commented on Mar 6, 2025

    @captainsafia
    Contributor

    @DavidNorena Unfortunately, with these kinds of servicing related PRs, we have to follow a pre-existing schedule (see this document) that means things can't be merged until the servicing branches are open for merge. That + time to evaluate the right fix to make and gather test cases can delay things.

    In this particular case, I started investigating three weeks ago but have just finalized things and reached an open servicing window this week.

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

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