Fix local $ref references by using NewDocumentWithConfiguration#46
Conversation
When a file path is provided, configure libopenapi's DocumentConfiguration with BasePath and SpecFilePath so the rolodex can resolve local $ref references (e.g. "$ref": "components/product/productmodel.yaml"). Previously, NewDocument was used without any configuration, causing the rolodex to fail with 'no file systems configured' when encountering relative file references. Fixes #44 Amp-Thread-ID: https://ampcode.com/threads/T-019cf338-f8ff-74a9-9c82-711d6476a568 Co-authored-by: Amp <amp@ampcode.com>
WalkthroughThis PR adds support for local file references in OpenAPI specifications. New example files demonstrate local references, main.go now accepts a spec file path argument and configures documents with base path settings to enable reference resolution, and tests are updated to use DocumentConfiguration for proper file handling. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
📝 Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment Tip You can enable review details to help with troubleshooting, context usage and more.Enable the |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
oas_test.go (1)
357-363: StrengthenTestLocalReferencesassertions to validate actual dereferencing.Lines [357] and [361] only check non-empty endpoints/components. Consider asserting presence of schema content originating from
pet.yaml(not just collection size), so unresolved refs can’t pass this test.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@oas_test.go` around lines 357 - 363, In TestLocalReferences, don't just assert non-empty model.endpoints and model.components; instead assert that the dereferenced content from pet.yaml is present — e.g. check model.components contains the expected schema key (like "Pet") and that its schema object includes expected properties (such as "id" or "name"), and/or that model.endpoints reference that schema; update the assertions around model.components and model.endpoints to verify those specific names/fields so unresolved $ref placeholders will fail the test.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@main.go`:
- Around line 37-40: The call to filepath.Abs(specFile) currently ignores the
returned error which can leave config.BasePath and config.SpecFilePath
incorrect; modify the code around the absPath, _ := filepath.Abs(specFile) line
to capture the error (e.g., absPath, err := filepath.Abs(specFile)), check err
and handle it appropriately (return the error or log and exit) before assigning
config.BasePath and config.SpecFilePath, ensuring config.AllowFileReferences is
only relied upon after a successful absolute path resolution.
---
Nitpick comments:
In `@oas_test.go`:
- Around line 357-363: In TestLocalReferences, don't just assert non-empty
model.endpoints and model.components; instead assert that the dereferenced
content from pet.yaml is present — e.g. check model.components contains the
expected schema key (like "Pet") and that its schema object includes expected
properties (such as "id" or "name"), and/or that model.endpoints reference that
schema; update the assertions around model.components and model.endpoints to
verify those specific names/fields so unresolved $ref placeholders will fail the
test.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 357f3837-0c10-4934-9bd9-4a07030aaff6
📒 Files selected for processing (4)
examples/local-refs/openapi.yamlexamples/local-refs/pet.yamlmain.gooas_test.go
| absPath, _ := filepath.Abs(specFile) | ||
| config.BasePath = filepath.Dir(absPath) | ||
| config.SpecFilePath = absPath | ||
| config.AllowFileReferences = true |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Verify whether filepath.Abs errors are being ignored in Go files.
rg -nP --type go '\w+\s*,\s*_\s*:=\s*filepath\.Abs\('Repository: plutov/oq
Length of output: 102
🏁 Script executed:
cat -n main.go | sed -n '25,50p'Repository: plutov/oq
Length of output: 857
🏁 Script executed:
# Check how specFile is obtained/validated
cat -n main.go | head -36Repository: plutov/oq
Length of output: 956
Handle filepath.Abs error explicitly.
The error from filepath.Abs at line 37 is discarded. If the call fails, config.BasePath becomes "." and config.SpecFilePath becomes empty, causing relative file references to resolve incorrectly when AllowFileReferences is enabled.
Proposed fix
- absPath, _ := filepath.Abs(specFile)
+ absPath, err := filepath.Abs(specFile)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "Error resolving absolute path: %v\n", err)
+ os.Exit(1)
+ }
config.BasePath = filepath.Dir(absPath)
config.SpecFilePath = absPath
config.AllowFileReferences = true🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@main.go` around lines 37 - 40, The call to filepath.Abs(specFile) currently
ignores the returned error which can leave config.BasePath and
config.SpecFilePath incorrect; modify the code around the absPath, _ :=
filepath.Abs(specFile) line to capture the error (e.g., absPath, err :=
filepath.Abs(specFile)), check err and handle it appropriately (return the error
or log and exit) before assigning config.BasePath and config.SpecFilePath,
ensuring config.AllowFileReferences is only relied upon after a successful
absolute path resolution.
Summary
Fixes #44 — Specifications with local references (e.g.
"$ref": "components/product/productmodel.yaml") now work correctly.Changes
main.go: When a file path is provided as an argument, uselibopenapi.NewDocumentWithConfigurationwithBasePathset to the spec file's directory andAllowFileReferencesenabled. This tells the rolodex how to resolve relative$refpaths. When reading from stdin, the default config (no local FS) is used as before.oas_test.go: Extracted anewDocumentFromFilehelper that applies the sameDocumentConfigurationwhen loading test specs, and updated all test functions to use it. Added a newTestLocalReferencestest.examples/local-refs/: Added a test spec that uses a local$refto a sibling file (pet.yaml), exercised by the new test.Summary by CodeRabbit
New Features
Tests