openapi3: preserve origin for a $ref to a schema under an arbitrary top-level key#21
Closed
reuvenharrison wants to merge 7 commits into
Closed
openapi3: preserve origin for a $ref to a schema under an arbitrary top-level key#21reuvenharrison wants to merge 7 commits into
reuvenharrison wants to merge 7 commits into
Conversation
reuvenharrison
marked this pull request as draft
July 12, 2026 20:57
reuvenharrison
marked this pull request as ready for review
July 12, 2026 21:03
reuvenharrison
force-pushed
the
origin-arbitrary-key-ref
branch
2 times, most recently
from
July 13, 2026 19:41
3a8a887 to
8d5d9f8
Compare
…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
force-pushed
the
origin-arbitrary-key-ref
branch
from
July 15, 2026 04:50
e8c7844 to
857f2f9
Compare
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.
Author
|
Merged upstream as getkin#1227; this fork copy is superseded. |
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.
A
$refto a schema stored under an arbitrary top-level key in another file (e.g../schemas.yaml#/User, the Swagger-2-era "definitions bag") resolves throughT.Extensions, a generic map that carries no origin, and the json round-trip that decodes it into the typed component drops origin entirely. WithIncludeOriginenabled the resolved schema then has a nilOrigin, unlike the/components/schemaspath.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
$refsite 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); fullopenapi3suite green.