Skip to content
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

Incorrect OpenAPI document generation with recursive object that contains a list #60012

Open
1 task done
marcominerva opened this issue Jan 23, 2025 · 2 comments
Open
1 task done
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-openapi

Comments

@marcominerva
Copy link
Contributor

marcominerva commented Jan 23, 2025

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

In a .NET 9.0.1 Minimal API project with Microsoft.AspNetCore.OpenApi, I have the following endpoint, returning a recursive object that also contains a list:

app.MapGet("/api/categories", () =>
{
    return TypedResults.Ok(new Category { Name = "My Category" });
});

public class Category
{
    public required string Name { get; set; }

    public Category? Parent { get; set; }

    public IEnumerable<Tag> Tags { get; set; } = [];
}

public class Tag
{
    public required string Name { get; set; }
}

With this code, the OpenAPI definition is wrong:

"components": {
    "schemas": {
      "Category": {
        "required": [
          "name"
        ],
        "type": "object",
        "properties": {
          "name": {
            "type": "string"
          },
          "parent": {
            "$ref": "#/components/schemas/Category"
          },
          "tags": {
            "$ref": "#/components/schemas/#/properties/parent/properties/tags"
          }
        }
      }
    }
}

If I remove the Parent property from Category, the document is correctly generated:

"components": {
	"schemas": {
	  "Category": {
		"required": [
		  "name"
		],
		"type": "object",
		"properties": {
		  "name": {
			"type": "string"
		  },
		  "tags": {
			"type": "array",
			"items": {
			  "$ref": "#/components/schemas/Tag"
			}
		  }
		}
	  },
	  "Tag": {
		"required": [
		  "name"
		],
		"type": "object",
		"properties": {
		  "name": {
			"type": "string"
		  }
		}
	  }
	}
}

Note that the issue happens even when using a primitive type for the collection (for example string).

Expected Behavior

No response

Steps To Reproduce

Minimal repro at https://github.com/marcominerva/RecursiveObjectIssue.

Exceptions (if any)

No response

.NET Version

9.0.102

Anything else?

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels label Jan 23, 2025
@martincostello martincostello added feature-openapi area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels labels Jan 23, 2025
@nanny07
Copy link

nanny07 commented Jan 24, 2025

I found out that if you invert the position of the two properties, putting the "Tags" first and then "Parent", it all works as expected

"components": {
    "schemas": {
      "Category": {
        "required": [
          "name"
        ],
        "type": "object",
        "properties": {
          "name": {
            "type": "string"
          },
          "tags": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Tag"
            }
          },
          "parent": {
            "$ref": "#/components/schemas/Category"
          }
        }
      },
      "Tag": {
        "required": [
          "name"
        ],
        "type": "object",
        "properties": {
          "name": {
            "type": "string"
          }
        }
      }
    }
  },
  "tags": [
    {
      "name": "RecursiveObjectIssue"
    }
  ]

@mikekistler
Copy link
Contributor

This looks like it might be a dup of #59976

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