Skip to content

openapi3: preserve origin for a $ref to a schema under an arbitrary top-level key#21

Closed
reuvenharrison wants to merge 7 commits into
masterfrom
origin-arbitrary-key-ref
Closed

openapi3: preserve origin for a $ref to a schema under an arbitrary top-level key#21
reuvenharrison wants to merge 7 commits into
masterfrom
origin-arbitrary-key-ref

Conversation

@reuvenharrison

@reuvenharrison reuvenharrison commented Jul 12, 2026

Copy link
Copy Markdown

A $ref to a schema stored under an arbitrary top-level key in another file (e.g. ./schemas.yaml#/User, the Swagger-2-era "definitions bag") resolves through T.Extensions, a generic map that carries no origin, and the json round-trip that decodes it into the typed component drops origin entirely. With IncludeOrigin enabled the resolved schema then has a nil Origin, unlike the /components/schemas path.

Fix: retain each document.s origin tree when it is unmarshaled (keyed by the document, only under IncludeOrigin), and on that generic-map path (and only there) walk the retained tree down the ref fragment and apply the subtree, so the resolved object carries the same origins a typed resolution would, with the original file.s line numbers. No file is read or parsed twice. Best-effort: a missing tree or fragment leaves the object without origins, exactly as before. The common typed path is untouched.

Why it matters

A consumer that uses origins for source locations (error anchors, editor links, source slicing) gets the wrong location for any element behind this ref shape: the origin chain falls back to the referencing document, pointing at the $ref site in the root file instead of the schema's own file and line. Whole-file refs (./user.yaml) and components-structured refs already carry correct origins; this closes the remaining shape.

The same applies to a same-document ref to an arbitrary top-level key ($ref: .#/User.): it drills through the identical Extensions path, so it is covered too, including documents loaded from memory.

Repro test + fixtures included (TestOrigin_ExternalRefToArbitraryTopLevelKey); full openapi3 suite green.

@reuvenharrison
reuvenharrison marked this pull request as draft July 12, 2026 20:57
@reuvenharrison
reuvenharrison marked this pull request as ready for review July 12, 2026 21:03
@reuvenharrison
reuvenharrison force-pushed the origin-arbitrary-key-ref branch 2 times, most recently from 3a8a887 to 8d5d9f8 Compare July 13, 2026 19:41
reuvenharrison and others added 6 commits July 15, 2026 03:31
…op-level key

A $ref to a schema stored under an arbitrary top-level key in another file
(e.g. "./schemas.yaml#/User", the Swagger-2-era definitions bag) resolves
through T.Extensions, a generic map that carries no origin, and the json
round-trip that decodes it into the typed component drops origin entirely. With
IncludeOrigin enabled the resolved schema then has a nil Origin, unlike the
/components/schemas path.

Re-attach origins on that path: re-derive the component file's origin tree, walk
to the ref fragment, and apply the subtree, so the resolved object carries the
same origins a typed resolution would, with the original file's line numbers.
Best-effort; any failure leaves the object without origins, as before.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BqJA1X6suZYtR3tRbX8sLj
It is origin machinery, and marsh.go is expected to hold the only direct yaml
uses outside origin.go (the CI unmarshal gate pins that count).
…eading

attachOriginToResolved re-read and re-parsed the component file even though it
had been read once already, and with a custom ReadFromURIFunc the second read
could be a remote fetch. Retain each visited document's origin tree at
unmarshal time instead (keyed like visitedDocuments, only when IncludeOrigin is
set), and make the re-attachment a lookup plus tree walk. A test pins that
resolving the arbitrary-key $ref reads each file exactly once.
The URL-string key relied on resolveRefAndDocument returning the same string
the document was loaded under; true today, but a silent lookup miss away from
breaking. Key by the document pointer instead: visitedDocuments guarantees one
*T per document, and resolveComponent already holds it, so insert and lookup
cannot disagree. This also serves documents loaded from memory (LoadFromData
has no URL), so a same-document $ref to an arbitrary top-level key now gets
its origin too, with a test.
@reuvenharrison
reuvenharrison force-pushed the origin-arbitrary-key-ref branch from e8c7844 to 857f2f9 Compare July 15, 2026 04:50
Pin the key location (line, column, name, end span), a field origin, and a
nested property origin against the fixture layout, instead of non-zero checks.
@reuvenharrison

Copy link
Copy Markdown
Author

Merged upstream as getkin#1227; this fork copy is superseded.

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.

1 participant