swagger-api / swagger-api/swagger-ui
Component schema not rendering in SwaggerUI when specified using $ref
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 29k
- Forks
- 9.3k
- Avg merge
- 2d 23h
- Merged PRs (30d)
- 25
Description
Q&A (please complete the following information)
- OS: macOS Version 13.3.1 (a) (22E772610a)
- Browser: 42Crunch OpenAPI Editor, SwaggerUI preview
- Version: 4.17.0
- Method of installation: VS Code .dmg > Vs Code Extensions
- Swagger-UI version:
"swagger-ui-react": "^4.15.5" - Swagger/OpenAPI version: 3.0.2
Content & configuration
Example Swagger/OpenAPI definition:
{
"openapi":"3.0.2",
"info": {
"title":"API Title",
"version":"1.0"
},
"servers": [
{"url":"https://api.server.test/v1"}
],
"components": {
"schemas": {
"collectionTemplate": {
"type": "object",
"properties": {
"type": {
"enum": ["collection"]
},
"items": {
"type": "array",
"items": {
"type": "object"
}
}
}
},
"timestampTemplate": {
"type": "object",
"properties": {
"timestamp": {
"enum": ["timestamp"]
}
}
},
"item1": {
"allOf": [
{
"$ref": "#/components/schemas/timestampTemplate"
},
{
"type": "object",
"properties": {
"item1Prop": {
"type": "string"
}
}
}
]
},
"item2": {
"type": "object",
"properties": {
"item2Prop": {
"enum": ["item2Prop"]
},
"item1Copy": {
"$ref": "#/components/schemas/item1"
}
}
},
"item2CollectionNotRendered": {
"allOf": [
{
"$ref": "#/components/schemas/collectionTemplate"
},
{
"type": "object",
"properties": {
"items": {
"type": "array",
"items": {
"$ref": "#/components/schemas/item2"
}
}
}
}
]
},
"item2CollectionRendered": {
"allOf": [
{
"$ref": "#/components/schemas/collectionTemplate"
},
{
"type": "object",
"properties": {
"items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"item2Prop": {
"enum": ["item2Prop"]
},
"item1Copy": {
"$ref": "#/components/schemas/item1"
}
}
}
}
}
}
]
}
}
},
"paths": {
"/notrendered": {
"get": {
"description": "",
"parameters": [],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/item2CollectionNotRendered"
}
}
}
}
}
}
},
"/rendered": {
"get": {
"description": "",
"parameters": [],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/item2CollectionRendered"
}
}
}
}
}
}
}
}
}
Swagger-UI configuration options:
// n/a
Describe the bug you're encountering
One component schema (item2 in example) includes another (item1 in example) as a sub-object in its definition. The item2 schema resolves correctly in the "Schemas" section of the preview, but the reference to item1 defaults to "string" rather than displaying the sub-object in the route response example textbox when added as a $ref. Adding the item2 schema directly rather than referencing it produces the expected result.
To reproduce...
Steps to reproduce the behavior:
- Go to https://editor.swagger.io
- Copy the example definition into the editor
- Compare example text for responses of /notrendered and /rendered
- Compare schemas for item2Collection* > items > item1Copy of item2CollectionNotRendered and item2CollectionRendered in the Schemas section.
Expected behavior
The response example text for item2CollectionNotRendered and item2CollectionRenderd to match.
Screenshots
Additional context or thoughts
Directed here by extension project: 42Crunch/vscode-openapi#222
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the supplied OpenAPI definition in editor.swagger.io and compare the /notrendered and /rendered response examples, focusing on item2Collection* > items > item1Copy. Trace the Swagger UI schema-resolution and response-example rendering entry points; done means both response examples render the referenced item1 sub-object consistently.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- documentation, frontend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 42/100