fix: resolve external $ref files relative to spec file location#2056
fix: resolve external $ref files relative to spec file location#2056chengyixu wants to merge 2 commits intoasyncapi:masterfrom
Conversation
…capi#1839) When the CLI architecture was refactored to use GeneratorService, the `path` option passed to `generator.generateFromString()` was changed from a string file path to a Specification object. The generator's parser checks `typeof oldOptions.path === 'string'` to set the source path for resolving relative $ref references. Since a Specification object is not a string, the source path was never set, causing external file references to fail when the spec is in a subdirectory. The fix uses `asyncapi.getSource()` which returns the file path string (or URL) from the Specification object, restoring correct $ref resolution behavior. Closes asyncapi#1839 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: d4e1d37 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
|
Hi @AayushSaini101 @Shurtu-gal! Pinging for review on this PR. Compared to the other fix for the same issue (#2046, opened a day earlier), this PR includes:
All CI checks are green (SonarCloud Quality Gate passed, Lint PR title passed). Happy to address any feedback! |
|
Hi @asyncapi/cli-maintainers! This PR fixes external $ref resolution relative to spec file location. All CI checks are passing. Happy to adjust anything based on your feedback! |
|
Hi! Just a friendly check-in on this resolution fix — CI is still passing. Happy to rebase or address any feedback for merge. Thanks! |
|
Hi @asyncapi/cli-maintainers! This PR has been open since 2026-03-18 and all CI checks are passing. It fixes external |
|
Hi @asyncapi/cli-maintainers! Following up on this external $ref fix — resolves file path resolution relative to spec file location (issue #1839, part of bounty program 2026-04). This PR includes integration tests with real fixture files and a unit test, making it more comprehensive than the competing PR. All CI checks passing, SonarCloud green. Would appreciate a review to settle which PR addresses this bounty issue. Thanks! |



Summary
Fixes #1839 — External
$reffiles in the same directory are not resolved since the CLI architecture refactoring.Root Cause
When the CLI was refactored to use
GeneratorService, thepathoption passed togenerator.generateFromString()was changed from a string file path to a Specification object:The
@asyncapi/generator's parser callsconvertOldOptionsToNew()which checkstypeof oldOptions.path === 'string'to set thesourcepath for resolving relative$refreferences. Since aSpecificationobject is not a string, the source path was never set, causing the@asyncapi/parser(and downstream@stoplight/json-ref-readers) to resolve relative paths against the CWD instead of the spec file's directory.Fix
Use
asyncapi.getSource()which returns the file path string (or URL) from the Specification object:Also updated the
GeneratorRunOptionsinterface to correctly typepathasstringinstead ofSpecification.Changes
src/domains/services/generator.service.ts: Passasyncapi.getSource()instead of theSpecificationobject aspath; fixGeneratorRunOptions.pathtype fromSpecificationtostringtest/integration/generate/fromTemplate.test.ts: Add integration test for external$refresolution from a subdirectorytest/unit/services/generator.service.test.ts: Add unit test verifyingSpecification.getSource()returns a string pathTest plan
$refin subdirectory (./test/fixtures/external-refs/main.yaml)$refand runasyncapi generate fromTemplate🤖 Generated with Claude Code