Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task done
Madajevas opened this issue Mar 6, 2025 · 3 comments
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-openapi

Comments

@Madajevas
Copy link

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

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label Mar 6, 2025
@martincostello martincostello added feature-openapi area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically labels Mar 6, 2025
@captainsafia
Copy link
Member

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

@DavidNorena
Copy link

@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.

@captainsafia
Copy link
Member

@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
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-openapi
Projects
None yet
Development

No branches or pull requests

4 participants