feat(frontend): resolve external $ref via a pluggable Loader - #10
Merged
Conversation
Add Options.Loader func(ctx, *url.URL) ([]byte, error) so cross-document $refs resolve. Reference resolution becomes a fixed-point worklist that fetches each unknown target document on demand, integrates its nodes into the shared registry, and only runs SCC analysis once every document is loaded — so transitive and cross-document recursion classify correctly. A nil Loader preserves the single-document graceful-degrade behavior. Closes #3 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #10 +/- ##
==========================================
- Coverage 75.54% 75.42% -0.12%
==========================================
Files 42 42
Lines 3051 3142 +91
==========================================
+ Hits 2305 2370 +65
- Misses 613 629 +16
- Partials 133 143 +10 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Summary
Closes #3. Only in-document
$refs resolved before; external/remote document refs degraded to unresolved-ref diagnostics. This adds an optionalLoaderonOptionsso cross-document schemas resolve.The Loader receives the resolved absolute target URI (fragment removed) and returns raw bytes — the frontend does its own libopenapi parsing, staying decoupled from libopenapi's rolodex (which
stripRefsdeliberately bypasses). Callers supply the transport (filesystem, HTTP, in-memory). Per issue #3 the signature followsgoogle/jsonschema-go'sResolveOptions.Loader, withcontext.Contextadded for cancellation.How it works
Reference resolution becomes a fixed-point worklist (
resolve.go):resolvePassresolves every ref that can be resolved now and collects the distinct external target documents that are absent from the registry and not yet attempted. A present resource with a missing fragment is a genuine dangling ref, not a fetch candidate.convertSchema-d into the sameconvState/Registry, seeding its retrieval URI as a resource (a declared root$idadditionally registers itself). Its own refs join the worklist.finalizeUnresolvedrecords any still-dangling refs (folding in loader errors), andanalyzeSCCsruns once — so transitive loads and cross-document cycles classify guarded/unguarded correctly.Each document is fetched at most once (
loadedset), which also bounds the worklist and breaks document cycles.API threading
frontend.Load(ctx, data, baseURI)kept unchanged (all existing callers/tests untouched); newLoadWithLoader(ctx, data, baseURI, loader)carries the loader.Compileuses the latter;FromLibOpenAPIpasses nil.Base-URI policy
Seed the retrieval URI as the loaded document's base; a declared root
$idadditionally registers its own resource. Refs addressing a document by an$idthat differs from its fetch URL are a known limitation (not reconciled here).Tests
internal/frontend/external_test.go(in-memory loader): pointer-fragment + whole-resource external refs, transitive load (root→a→b), cross-document cycle (terminates + classifies Guarded), nil-loader graceful degrade, loader-error diagnostic, load-once-per-document caching, missing-fragment-not-refetched. Plus end-to-endCompiletests with/withoutOptions.Loaderinschemacompiler_test.go.Verification
go build ./...,go test ./...(incl. conformance),go test -race ./internal/frontend/ .all greengolangci-lint run ./...→ 0 issues🤖 Generated with Claude Code