Skip to content

fix: resolve external ref files relative to spec file location#2075

Closed
HuiNeng6 wants to merge 1 commit intoasyncapi:masterfrom
HuiNeng6:fix/issue-1839-external-ref-resolution
Closed

fix: resolve external ref files relative to spec file location#2075
HuiNeng6 wants to merge 1 commit intoasyncapi:masterfrom
HuiNeng6:fix/issue-1839-external-ref-resolution

Conversation

@HuiNeng6
Copy link

Summary

Fixes #1839 — External $ref files in the same directory are not resolved since the CLI architecture refactoring.

Root Cause

When the CLI was refactored to use GeneratorService, the path option passed to generator.generateFromString() was changed from a string file path to a Specification object:

// Before (working):
await generator.generateFromString(specification.text(), {...genOption, path: asyncapi});
// `asyncapi` was a string like "./src/contract/asyncapi.yaml"

// After (broken - current code):
await generator.generateFromString(asyncapi.text(), {
  ...genOption,
  path: asyncapi,  // Specification object, not a string!
});

The @asyncapi/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 relative $ref paths to be resolved 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:

await generator.generateFromString(asyncapi.text(), {
  ...genOption,
  path: asyncapi.getSource(),  // Returns string file path or URL
});

Also updated the GeneratorRunOptions interface to correctly type path as string instead of Specification.

Changes

  • src/domains/services/generator.service.ts: Pass asyncapi.getSource() instead of the Specification object as path; fix GeneratorRunOptions.path type from Specification to string

Test plan

  • Existing integration tests pass (no regressions)
  • Manual test: create an AsyncAPI spec in a subdirectory referencing other YAML files via $ref and run asyncapi generate fromTemplate

Note: This PR is similar to #2046 and #2056 but provides an independent fix for the same issue. The fix logic is identical and correct.

Fixes asyncapi#1839

When the CLI was refactored to use GeneratorService, the path option
passed to generator.generateFromString() was accidentally changed from
a string file path to a Specification object. This caused relative
 paths to be resolved against CWD instead of the spec file's
directory.

This fix passes asyncapi.getSource() (which returns the file path
string or URL) instead of the Specification object, restoring the
correct behavior for resolving external file references.
@changeset-bot
Copy link

changeset-bot bot commented Mar 22, 2026

🦋 Changeset detected

Latest commit: e810aa5

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@asyncapi/cli Patch

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

@sonarqubecloud
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[BUG] external files in same directory are not resolved via $ref anymore, since v3.3.0

2 participants