feat: add $dynamicRef and $dynamicAnchor support (OpenAPI 3.1 / JSON Schema 2020-12)#2824
Open
aqeelat wants to merge 2 commits into
Open
feat: add $dynamicRef and $dynamicAnchor support (OpenAPI 3.1 / JSON Schema 2020-12)#2824aqeelat wants to merge 2 commits into
aqeelat wants to merge 2 commits into
Conversation
Implements JSON Schema Draft 2020-12 / OpenAPI 3.1 $dynamicRef and $dynamicAnchor keywords, enabling generic/template schema patterns like PaginatedResponse<T> and recursive type hierarchies. Closes openapi-ts#2029 Changes: - Add $dynamicRef and $dynamicAnchor to SchemaObject type - Add dynamicAnchors to TransformNodeOptions for scope propagation - Add collectDynamicAnchors(), resolveDynamicAnchor(), schemaContainsDynamicRef() utilities - Handle $dynamicRef resolution with parent scope override semantics - Handle $dynamicAnchor self-registration for recursive types - Handle $ref concretization when resolved target contains $dynamicRef - Parent $defs anchors propagate to child $dynamicRef in non-$ref schemas - 336 tests passing (43 new: 27 unit, 16 utility direct, 3 integration)
👷 Deploy request for openapi-ts pending review.Visit the deploys page to approve it
|
🦋 Changeset detectedLatest commit: 5c312ff The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #2029
Summary
Implements
$dynamicRefand$dynamicAnchorsupport (JSON Schema Draft 2020-12 / OpenAPI 3.1), enabling generic/template schema patterns likePaginatedResponse<T>and recursive type hierarchies.What it does
When a schema uses
$refto a template with$dynamicRefplaceholders, and the caller provides$defswith$dynamicAnchoroverrides, the generated TypeScript types are concretized — each override produces a distinct type with the correct substituted types instead ofunknown.Example input:
Output:
Supported patterns
$ref+$defsPaginatedResponse<T>$dynamicAnchoron schemaBaseCategory { children: Self[] }LocalizedCategoryoverrideschildren: LocalizedCategory[]ShelterFolderwithfolderType+resourceType$dynamicRefinsideallOf/oneOf/anyOfApiEnvelope > PaginatedResponse > PetImplementation
SchemaObject— Added$dynamicRef?: stringand$dynamicAnchor?: stringTransformNodeOptions— AddeddynamicAnchorsfor scope propagation through recursive transformscollectDynamicAnchors()— Collects$dynamicAnchorentries from$defs, stripping the anchor keywordresolveDynamicAnchor()— Resolves anchor name: checks parent scope first (override), then local$defs(fallback)schemaContainsDynamicRef()— Guards$refconcretization to only inline when the target actually uses$dynamicRef$refhandler — When$defsprovide dynamic anchors AND the resolved target contains$dynamicRef, concretizes inline with merged anchors$dynamicRefhandler — Resolves to override type, returnsoapiRef()for$refoverrides (prevents infinite recursion), transforms inline for schema overrides$dynamicAnchorhandler — Self-registers schema as anchor for recursive type generation$defspropagation — Non-$refschemas with$defsanchors pass them to child transformsTesting
dynamic-ref.test.tscovering every branch in the transform logicutils.test.tsforschemaContainsDynamicRef,collectDynamicAnchors,resolveDynamicAnchorindex.test.tswith full OpenAPI documents (paginated response, recursive allOf, multiple anchors with oneOf)Files changed
src/types.ts$dynamicRef,$dynamicAnchortoSchemaObject; adddynamicAnchorstoTransformNodeOptionssrc/lib/utils.tscollectDynamicAnchors(),resolveDynamicAnchor(),schemaContainsDynamicRef()src/transform/schema-object.ts$dynamicRefhandler,$dynamicAnchorself-registration,$refconcretization with dynamic anchors, parent$defspropagationtest/transform/schema-object/dynamic-ref.test.tstest/lib/utils.test.tstest/index.test.ts