Skip to content

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

Closed as duplicate

Description

@marcominerva

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

Activity

  1. ghost added
    old-area-web-frameworks-do-not-use
    on Jan 23, 2025
  2. added
    area-minimal
    and removed
    old-area-web-frameworks-do-not-use
    on Jan 23, 2025
  3. nanny07 commented on Jan 24, 2025

    @nanny07

    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"
        }
      ]
  4. mikekistler commented on Jan 25, 2025

    @mikekistler

    This looks like it might be a dup of #59976

  5. marcominerva commented on Feb 12, 2025

    @marcominerva
    ContributorAuthor

    The issue still happens with .NET 9.0.2

  6. captainsafia commented on Mar 5, 2025

    @captainsafia
    Contributor

    Thanks for filing this issue, @marcominerva!

    You've already dupped it to #60381 where I've left a comment but figured I'd make a reference to that comment here.

    #60381 (comment)

    Similar to the above, I've included a test for this in the branch associated with the backport. For now, closing this one as a dupe of the referenced issue.

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-minimalfeature-openapi

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions