Skip to content

Fix local $ref references by using NewDocumentWithConfiguration#46

Merged
plutov merged 1 commit into
mainfrom
fix/local-references
Mar 15, 2026
Merged

Fix local $ref references by using NewDocumentWithConfiguration#46
plutov merged 1 commit into
mainfrom
fix/local-references

Conversation

@plutov

@plutov plutov commented Mar 15, 2026

Copy link
Copy Markdown
Owner

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, use libopenapi.NewDocumentWithConfiguration with BasePath set to the spec file's directory and AllowFileReferences enabled. This tells the rolodex how to resolve relative $ref paths. When reading from stdin, the default config (no local FS) is used as before.

  • oas_test.go: Extracted a newDocumentFromFile helper that applies the same DocumentConfiguration when loading test specs, and updated all test functions to use it. Added a new TestLocalReferences test.

  • examples/local-refs/: Added a test spec that uses a local $ref to a sibling file (pet.yaml), exercised by the new test.

Summary by CodeRabbit

  • New Features

    • Added command-line support for specifying OpenAPI spec file paths
    • Added capability to resolve external file references within specifications
    • Included example files demonstrating local reference usage
  • Tests

    • Added tests for external file reference handling in OpenAPI specifications

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>
@coderabbitai

coderabbitai Bot commented Mar 15, 2026

Copy link
Copy Markdown

Walkthrough

This 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

Cohort / File(s) Summary
Example Files
examples/local-refs/openapi.yaml, examples/local-refs/pet.yaml
New OpenAPI specification example demonstrating local file references; Pet schema is defined in separate YAML file and referenced via external file link.
Core Implementation
main.go
Added command-line argument handling for spec file path; replaced direct document creation with DocumentConfiguration-based approach that sets BasePath and enables file references.
Test Updates
oas_test.go
Introduced newDocumentFromFile helper function for consistent document creation from files; refactored existing tests to use the new helper; added TestLocalReferences test case to validate local reference functionality.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • #32: 3.0, 3.1, 3.2 #36: Modifies OpenAPI document creation codepath to use libopenapi's NewDocument and DocumentConfiguration, directly aligned with this PR's approach.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: fixing local $ref references by implementing NewDocumentWithConfiguration.
Linked Issues check ✅ Passed Code changes implement all requirements from issue #44: using NewDocumentWithConfiguration with BasePath and file references enabled for local $ref resolution.
Out of Scope Changes check ✅ Passed All changes directly support fixing local $ref resolution: configuration updates in main.go, test helper in oas_test.go, and example files for testing.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/local-references
📝 Coding Plan
  • Generate coding plan for human review comments

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Tip

You can enable review details to help with troubleshooting, context usage and more.

Enable the reviews.review_details setting to include review details such as the model used, the time taken for each step and more in the review comments.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
oas_test.go (1)

357-363: Strengthen TestLocalReferences assertions 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

📥 Commits

Reviewing files that changed from the base of the PR and between 40ff61e and ecdc113.

📒 Files selected for processing (4)
  • examples/local-refs/openapi.yaml
  • examples/local-refs/pet.yaml
  • main.go
  • oas_test.go

Comment thread main.go
Comment on lines +37 to +40
absPath, _ := filepath.Abs(specFile)
config.BasePath = filepath.Dir(absPath)
config.SpecFilePath = absPath
config.AllowFileReferences = true

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 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 -36

Repository: 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.

@plutov plutov merged commit 0db6ba3 into main Mar 15, 2026
3 checks passed
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.

Local references don't work

1 participant