fix: resolve external $ref files relative to spec file location (#1839)#2077
Closed
DeekRoumy wants to merge 1 commit intoasyncapi:masterfrom
Closed
fix: resolve external $ref files relative to spec file location (#1839)#2077DeekRoumy wants to merge 1 commit intoasyncapi:masterfrom
DeekRoumy wants to merge 1 commit intoasyncapi:masterfrom
Conversation
…capi#1839) The `generate:fromTemplate` command failed to resolve external $ref files since v3.3.0 when the spec was located in a subdirectory. Relative references like `./schemas.yaml` were resolved against CWD instead of the spec file's directory. Root cause: GeneratorService passed the Specification object (not the file path string) as the `path` option to `generator.generateFromString()`. The generator uses this to determine the base directory for $ref resolution. Fix: - Pass `asyncapi.getSource()` (string) instead of `asyncapi` (object) - Fix `GeneratorRunOptions.path` type from `Specification` to `string` - Add integration test using existing external-refs fixture - Add comprehensive unit tests for the regression Closes asyncapi#1839
🦋 Changeset detectedLatest commit: 379d16a 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 |
|
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 as the spec are not resolved when the spec is in a subdirectory (regression since v3.3.0).Root Cause
When
GeneratorServicewas introduced, thepathoption passed togenerator.generateFromString()was accidentally changed from a string file path to aSpecificationobject. The AsyncAPI generator uses thispathvalue to determine the base directory for resolving relative$refpaths. Receiving an object instead of a string caused it to fall back to CWD, breaking resolution for specs in subdirectories.The one-line regression in
generator.service.ts:Fix
src/domains/services/generator.service.ts: Passasyncapi.getSource()(returns file path string or URL) instead of theasyncapiobjectGeneratorRunOptionsinterface: Correctedpathtype fromSpecificationtostring(matching what the generator actually expects)Tests Added
Unit tests (
test/unit/services/generator.service.test.ts) — new filegetSource()returns a string for file-based specsgetSource()returns a string URL for URL-based specspath.dirname(getSource())correctly resolves relative $refsIntegration test (
test/integration/generate/fromTemplate.test.ts)test/fixtures/external-refs/fixture (main.yaml references schemas.yaml in the same subdirectory)Comparison to Competing PRs
PRs #2046, #2056, and #2075 all make the same correct one-line fix. This PR is more complete because it:
path?: Specification→path?: string) in addition to the call siteHow to Reproduce the Bug (before fix)
Closes #1839