Skip to content

Incorrect OpenAPI Schema Generation for Recursive Types #61139

Open
@sorcerb

Description

@sorcerb

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

We encountered an issue where ASP.NET Core incorrectly generates OpenAPI json schema for recursive types. Specifically, the generated schema for a recursive model (Nav) is valid, but when attempting to define a separate object (Nav2) that references an array of items, it incorrectly resolves the $ref path.

Expected Behavior

The generated OpenAPI schema should correctly resolve $ref references for recursive types, ensuring Nav2.children.items properly refers to the Nav schema.

Steps To Reproduce

app.MapPost("/a", (Nav n) => "Hello World!");
app.MapPost("/b", (List<Nav> n) => "Hello World!");

public class Nav
{
    public long Id { get; set; }
    public List<Nav> Children { get; set; } = [];
}
"Nav": {
  "type": "object",
  "properties": {
	"id": {
	  "type": "integer",
	  "format": "int64"
	},
	"children": {
	  "type": "array",
	  "items": {
		"$ref": "#/components/schemas/Nav"
	  }
	}
  }
},
"Nav2": {
  "type": "object",
  "properties": {
	"id": {
	  "type": "integer",
	  "format": "int64"
	},
	"children": {
	  "type": "array",
	  "items": {
		"$ref": "#/components/schemas/#/items"   // <--- this should refers to the Nav, but actually, 
                                                         // this Nav2 is the same as Nav, so it won't be generated
	  }
	}
  }
},

Exceptions (if any)

No response

.NET Version

9.0.3

Related issues

Recursive data models inside collections yield invalid schema references #59879
.NET 9 OpenAPI produces lots of duplicate schemas for the same object #58968

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