fix: resolve external $ref files relative to spec file location#2056
Open
chengyixu wants to merge 1 commit intoasyncapi:masterfrom
Open
fix: resolve external $ref files relative to spec file location#2056chengyixu wants to merge 1 commit intoasyncapi:masterfrom
chengyixu wants to merge 1 commit 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>
2 tasks
|
|
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
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