feat: enhance $ref resolution for OpenAPI 3.1.0
fixes: #10584
Description
This PR fixes rendering and sample generation for JSON Schema 2020-12 $ref-based schemas in Swagger UI where schemas self-reference. It addresses three related problems:
- The UI previously displayed array-of-$ref as a generic array
instead of a friendly shorthand or the referenced model title. - A prior attempt to inline/append the referenced schema caused infinite recursion when using the "Expand all" action for circular/self-referential schemas. The PR restores identity-aware behavior so circular detection remains effective.
- Request/response example generation fell back to primitives (for example "friends": ["string"]) This PR improves sample generation so examples reflect the referenced object type
File changes
-
src\core\plugins\json-schema-2020-12\fn.js - Object type label display / type inference (makeGetType/getType)
-
src\core\plugins\json-schema-2020-12-samples\fn\get-json-sample-schema.js - Attempt to resolve top-level local $ref before calling the sample generator so nested array items that reference an object schema produce object samples
-
src\core\plugins\json-schema-2020-12\components\JSONSchema\JSONSchema.jsx, src\core\plugins\json-schema-2020-12\components\keywords$ref.jsx, src\core\plugins\json-schema-2020-12\hoc.jsx UI rendering for the $ref keyword in Schema
Note: A portion of the code changes and the PR description were produced with the help of an AI coding assistant.
Motivation and Context
How Has This Been Tested?
Locally tested. Added screenshot
Screenshots:
Tested with the yaml file -
openapi: 3.1.0
info:
title: test
description: test
version: 1.0.0
servers:
- url: http://localhost:5000
description: test
paths:
/api/user:
get:
summary: get user
responses:
'200':
description: ok
content:
application/json:
schema:
$ref: '#/components/schemas/User'
components:
schemas:
User:
type: object
properties:
id:
type: integer
name:
type: string
friends:
type: array
items:
type: object
$ref: '#/components/schemas/User'
required:
- id
Checklist
My PR contains...
- [ ] No code changes (
src/is unmodified: changes to documentation, CI, metadata, etc.) - [ ] Dependency changes (any modification to dependencies in
package.json) - [ ] Bug fixes (non-breaking change which fixes an issue)
- [x] Improvements (misc. changes to existing features)
- [ ] Features (non-breaking change which adds functionality)
My changes...
- [ ] are breaking changes to a public API (config options, System API, major UI change, etc).
- [ ] are breaking changes to a private API (Redux, component props, utility functions, etc.).
- [ ] are breaking changes to a developer API (npm script behavior changes, new dev system dependencies, etc).
- [x] are not breaking changes.
Documentation
- [x] My changes do not require a change to the project documentation.
- [ ] My changes require a change to the project documentation.
- [ ] If yes to above: I have updated the documentation accordingly.
Automated tests
- [ ] My changes can not or do not need to be tested.
- [ ] My changes can and should be tested by unit and/or integration tests.
- [ ] If yes to above: I have added tests to cover my changes.
- [ ] If yes to above: I have taken care to cover edge cases in my tests.
- [ ] All new and existing tests passed.