Skip to content

Detect $dynamicAnchor in OpenAPI 3.0 documents (DynamicAnchorIn30 rule) - #205

Merged
geemus merged 4 commits into
ota42y:masterfrom
takayamaki:pr18-dynamic-anchor
Sep 7, 2026
Merged

Detect $dynamicAnchor in OpenAPI 3.0 documents (DynamicAnchorIn30 rule)#205
geemus merged 4 commits into
ota42y:masterfrom
takayamaki:pr18-dynamic-anchor

Conversation

@takayamaki

Copy link
Copy Markdown
Contributor

Continuing the OpenAPI 3.1 work from #152.

$dynamicAnchor is the companion of $dynamicRef from #203.
$dynamicRef names an anchor to resolve against the dynamic scope,
and $dynamicAnchor is what declares such an anchor on a schema.
3.0 knows neither: its only referencing keyword is $ref, resolved statically.

This PR adds detection of that version mismatch.
A 3.0 document using $dynamicAnchor is relying on a keyword its declared version does not define,
and SpecValidator should be able to say so.

SpecValidator rule

DynamicAnchorIn30 reports a violation for each schema in a 3.0 document that uses $dynamicAnchor:

OpenAPIParser.load(
  'spec.yaml',
  strict_specification_version: :warn,
)
# [DynamicAnchorIn30] #/components/schemas/Node — `$dynamicAnchor` is a 3.1 addition (from JSON Schema 2020-12); 3.0 has no equivalent

Detection inspects raw_schema key presence,
so it fires whether or not anything references the anchor.

As with #203, this PR does not make $dynamicAnchor take part in reference resolution.
Dynamic resolution has to pick its target while the schema is being evaluated,
which likely means redesigning SchemaValidator in this gem.
Resolving it is a TODO beyond this series of PRs.

Closes the 3.1 divergence enumeration. Matching pattern to $dynamicRef:
raw_schema key detection, 3.0 only emits, parse stays permissive.
$dynamicAnchor on a 3.0 document warns and raises (JSON Schema 2020-12
dynamic resolution point with no 3.0 equivalent); the same keyword on a
3.1 document stays clean.
@takayamaki
takayamaki marked this pull request as ready for review August 27, 2026 23:57

@geemus geemus left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, thanks!

@geemus
geemus merged commit 636fd81 into ota42y:master Sep 7, 2026
14 checks passed
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.

2 participants