Recursive data models inside collections yield invalid schema references #59879
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
Is there an existing issue for this?
Describe the bug
The new Microsoft.AspNetCore.OpenApi package yields invalid schema references when using recursive data models inside collections.
Here is an example of a recursive model
Tree
because it contains a list of its own type in the propertyChildren
:Return this through an endpoint, and the resulting schema is correct.
Notice here how the
components.schemas.Tree.properties.children.items."$ref"
points back toTree
- so far so good. The trouble comes when I add the recursive model in a collection in another model:Two problems occur here. First of all, the
Tree
model insideTree.List
gets its own schema (calledTree2
in my case). The other problem is, the newcomponents.schemas.Tree2.properties.children.items."$ref"
looks to refer back to the list of the enclosingTrees
model:If I only had the
/trees/
endpoint, theTree
schema would be necessary to generate of course. However, here it already exists. The schema reference is incorrect and should just point toTree2
or ideallyTree
. I am not well-versed in the OpenAPI specification, so I determine it's "incorrect" because the Spectral Linter calls it out and Kiota is unable to parse this schema reference.The same thing happens if I put
Tree
inside a Dictionary. It generates this type of schema reference:Expected Behavior
Generate the schema without duplicate
Tree
definitions and with correct schema references:Steps To Reproduce
I have created a minimal API showing the behavior:
https://gist.github.com/thorhj/933f07533ff1c8206ed7ebefd4d91c6a
In it I have included an endpoint returning just the recursive model
Tree
(valid), the recursive model inside aList<Tree>
property (invalid) and the recursive data model inside aDictionary<int, Tree>
(invalid).Exceptions (if any)
No response
.NET Version
9.0.101
Anything else?
The text was updated successfully, but these errors were encountered: