Skip to content

OpenAPI generation leads to invalid openapi.json file #59496

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
tyler-boyd opened this issue Dec 16, 2024 · 1 comment
Closed
1 task done

OpenAPI generation leads to invalid openapi.json file #59496

tyler-boyd opened this issue Dec 16, 2024 · 1 comment
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-openapi ✔️ Resolution: Duplicate Resolved as a duplicate of another issue Status: Resolved

Comments

@tyler-boyd
Copy link

tyler-boyd commented Dec 16, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Consider the following minimal example:

app.MapGet("/test", () =>
{
    return "hello"; // not important, we only care about the openapi spec generation
}).Produces<Post>();
app.Run();

public class LinkDetails
{
    public string Name { get; set; }
    public string Url { get; set; }
}

public class Post
{
    public List<LinkDetails> Links1 { get; set; } = [];
    public List<LinkDetails> Links2 { get; set; } = [];
}

This leads to the following openapi.json (relevant section):

      "Post": {
        "type": "object",
        "properties": {
          "links1": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/LinkDetails"
            }
          },
          "links2": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/#/properties/links1/items"
            }
          }
        }
      }

Note that links1 looks correct, while links2 is different and leads to an error when viewing it in swagger UI ("Semantic error at components.schemas.Post.properties.links2.items.$ref - $ref values must be RFC3986-compliant percent-encoded URIs")

Removing links1 fixes the ref for links2, so it appears that when you have two properties with the same type, the openapi generation is being smart and encoding a ref to the first property, however swagger UI doesn't seem to support it, at least in this scenario.

Expected Behavior

I would expect .NET 9's openapi generation generates JSON files that are valid according to swagger UI.

Steps To Reproduce

I included most of a code example above. I think the behaviour here is probably intentional, but seems incompatible with swagger UI, so I'm assuming a complete repro repo isn't needed. If it would be helpful, let me know and I can create one.

Exceptions (if any)

No response

.NET Version

9.0.101

Anything else?

Brand new dotnet new webapi from .net 9.0.101; assuming dotnet --info isn't relevant here.

edit: this issue also seems to break the NSwagTypeScript code generator, with basically the same error message System.InvalidOperationException: Could not resolve the path '#/components/schemas/#/properties/links/items'.

@ghost ghost added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Dec 16, 2024
@martincostello martincostello added feature-openapi area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates labels Dec 16, 2024
@captainsafia
Copy link
Member

@tyler-boyd Thanks for filing this issue!

I'm going to dupe this to #58915 and close it since they relate to the same issue. I'll follow-up on that one with the notes and investigation.

@captainsafia captainsafia added the ✔️ Resolution: Duplicate Resolved as a duplicate of another issue label Dec 18, 2024
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 ✔️ Resolution: Duplicate Resolved as a duplicate of another issue Status: Resolved
Projects
None yet
Development

No branches or pull requests

3 participants