Repository navigation
[OpenAPI] Invalid schema references generated #63090
Description
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
To validate the changes made in #63052 I put the OpenAPI schema into editor.swagger.io to see if the schema for JSON Patch had any validation errors (it does not).
However, amongst a few schema errors that are found in the document that aren't just artifacts of testing, I found two that appear to be actual issues:
Semantic error at components.schemas.Category.properties.tags.$ref - $refs must reference a valid location in the documentfromLine 1362 in f91de94
"$ref": "#/components/schemas/Category/properties/parent/properties/tags" Semantic error at components.schemas.Item.properties.name.$ref - $refs must reference a valid location in the documentfromLine 1427 in f91de94
"$ref": "#/components/schemas/Root/properties/item1/properties/name"
These correlate to these parts of the code:
public IEnumerable<Tag> Tags { get; set; } = []; public string[] Name { get; set; } = null!;
Both of these properties are arrays and appear to generate a circular reference to themselves via another object.
- Points to the
tagsproperty of theparentproperty of itself, but that's also aCategory, so it's just a circular reference to itself. - Points to the
nameproperty of theitem1property ofRoot, but that's also anItem, so again creates a circular reference to itself.
In both cases it looks like the schema properties should be an array of items which reference Tag or Item respectively.
Expected Behavior
The schemas do not generate validation errors.
Steps To Reproduce
Enter the contents of OpenApiDocumentLocalizationTests.VerifyOpenApiDocumentIsInvariant.verified.txt into editor.swagger.io.
Exceptions (if any)
No response
.NET Version
10.0.100-preview.7.25322.101
Anything else?
No response
Activity
MihuBot commentedon Aug 4, 2025
Possible related and/or duplicate issues:
[OpenAPI] Validate OpenAPI documents
[OpenApi] Test schema reference validity
martincostello commentedon Aug 4, 2025
Have opened #63092 to add some validation and fix some issues, plus #63095 as a draft of a test that detects this issue.
martincostello commentedon Aug 4, 2025
This might be a runtime issue, as it looks like the reference ID gets assigned when calling
JsonSchemaExporter.GetJsonSchemaAsNode()here:aspnetcore/src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs
Lines 348 to 349 in d6f0d4b
That then gets used here:
aspnetcore/src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs
Lines 222 to 223 in d6f0d4b
martincostello commentedon Aug 4, 2025
Actually I don't think it is, as the reference there correctly points to the inlined schema that's generated. Looks like it needs to get fixed-up somehow to point to be a valid pointer into the OpenAPI document.
captainsafia commentedon Aug 4, 2025
@martincostello Yep -- I had roughly root caused this as the reason for bugs like #61194 (comment). I've been marinating on a fix though since "re-rooting" the relative references that STJ creates to the OpenAPI document is trickier for relative references that are two-layers deep.
[OpenAPI] Validate OpenAPI documents
[OpenApi] Test schema reference validity
[OpenAPI] Validate OpenAPI documents
[OpenAPI] Validate OpenAPI documents
[OpenApi] Test schema reference validity
martincostello commentedon Aug 15, 2025
Looks like #63256 fixed this. #63092 should now pass and help detect any recurrence in the future.
[OpenAPI] Validate OpenAPI documents (#63092)