Skip to content

fix: prevent duplicate __origin__ on YAML anchor alias expansion#1

Merged
reuvenharrison merged 1 commit intov3from
fix/origin-anchor-alias-duplicate-key
Mar 30, 2026
Merged

fix: prevent duplicate __origin__ on YAML anchor alias expansion#1
reuvenharrison merged 1 commit intov3from
fix/origin-anchor-alias-duplicate-key

Conversation

@reuvenharrison
Copy link
Copy Markdown
Collaborator

Summary

  • YAML anchors/aliases share the same *Node pointer for the anchor target
  • When origin tracking is enabled, addOriginInMap/addOriginInSeq appended __origin__ to nested mapping nodes once at the anchor definition, then again for every alias expansion that re-walked the same subtree
  • Result: mapping key "__origin__" already defined error on any spec using YAML anchors with nested objects

Fix

Skip origin injection while inside an alias expansion (aliasDepth > 0). The anchor's origin from its definition site is preserved; alias use sites don't inject again, avoiding mutation of the shared anchor node.

Two lines changed in decode.go:

if d.origin && d.aliasDepth == 0 {

Tests

Two regression tests added to origin_test.go:

Both tests were written first (failing), then the fix was applied.

Fixes oasdiff/oasdiff#821.

🤖 Generated with Claude Code

When a YAML alias (*anchor) is used as a map value or sequence element,
the decoder re-walks the anchor's subtree via d.unmarshal(n.Alias, ...).
This caused addOriginInMap/addOriginInSeq to append a second __origin__
entry to nested MappingNodes that were already annotated when the anchor
was first processed — producing a "mapping key already defined" error on
any spec that uses YAML anchors with nested objects.

Fix: skip origin injection while inside an alias expansion (aliasDepth > 0).
The anchor's origin from the definition site is preserved; alias use sites
do not get their own injection (which would mutate the shared anchor node).

Regression test for oasdiff/oasdiff#821.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.

v1.12.4 regression: fails to load specs with YAML anchors (__origin__ duplicate key)

1 participant