Skip to content

fix(parser-v3): preserve $dynamicRef/$dynamicAnchor and fix $id scope leak in OAS 3.1 dereferencer#2332

Open
aqeelat wants to merge 1 commit into
swagger-api:masterfrom
aqeelat:fix/oas31-dynamicref-id-resolution
Open

fix(parser-v3): preserve $dynamicRef/$dynamicAnchor and fix $id scope leak in OAS 3.1 dereferencer#2332
aqeelat wants to merge 1 commit into
swagger-api:masterfrom
aqeelat:fix/oas31-dynamicref-id-resolution

Conversation

@aqeelat
Copy link
Copy Markdown

@aqeelat aqeelat commented May 12, 2026

Summary

Fixes the OAS 3.1 dereferencer to handle specs using $dynamicRef, $dynamicAnchor, $defs, and $id without crashing or losing data. Closes #2331.

Four changes, all in modules/swagger-parser-v3/:

  1. ReferenceVisitor.resolveSchemaRef() — Bypass inheritedIds (the $id chain) for #/components/ and #/definitions/ refs so a schema's $id cannot hijack document-level resolution. Also clear inheritedIds during traversal of resolved components to prevent $id scope from leaking into nested refs.

  2. ReferenceVisitor.findAnchor() — Match $dynamicAnchor in addition to $anchor so anchor-style fragments resolve correctly.

  3. OpenAPI31Traverser.traverseSchema() — Traverse $defs sub-schemas from extensions so nested refs and anchors inside $defs are available to consumers.

  4. OpenAPI31Traverser.mergeSchemas() — Copy $dynamicRef and $dynamicAnchor from source to target so these fields survive the merge cycle.

Test plan

  • 8 tests in OpenAPIV3ParserDynamicRefTest covering:
    • $id + $ref to #/components/ (no crash)
    • $id scope leak into nested document-level refs
    • $id scope leak into nested file refs
    • $dynamicRef/$dynamicAnchor preservation after resolution
    • $defs traversal and preservation
    • Recursive $dynamicRef override pattern
    • mergeSchemas field preservation
mvn test -pl modules/swagger-parser-v3 -Dtest=OpenAPIV3ParserDynamicRefTest

Full module suite passes (588 tests, 0 failures):

mvn test -pl modules/swagger-parser-v3

… leak in OAS 3.1 dereferencer

Four changes to the OAS 3.1 dereferencer to handle specs using
$dynamicRef, $dynamicAnchor, $defs, and $id without crashing
or losing data:

1. resolveSchemaRef(): bypass inheritedIds for #/components/ and
   #/definitions/ refs so a schema's $id cannot hijack document-level
   resolution. Also clear inheritedIds during traversal of resolved
   components to prevent $id scope from leaking into nested refs.

2. findAnchor(): match $dynamicAnchor in addition to $anchor so
   anchor-style fragments resolve correctly.

3. traverseSchema(): traverse $defs sub-schemas from extensions so
   nested refs and anchors inside $defs are available to consumers.

4. mergeSchemas(): copy $dynamicRef and $dynamicAnchor from source
   to target so these fields survive the merge cycle.

Closes swagger-api#2331
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: OAS 3.1 dereferencer crashes on schemas with + , loses / during resolution

1 participant