From e810aa59fa074db4830490bfbad93da80145eb52 Mon Sep 17 00:00:00 2001 From: HuiNeng <3650306360@qq.com> Date: Sun, 22 Mar 2026 15:53:50 +0800 Subject: [PATCH] fix: resolve external files relative to spec file location Fixes #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/fix-external-ref-resolution.md | 9 +++++++++ src/domains/services/generator.service.ts | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 .changeset/fix-external-ref-resolution.md diff --git a/.changeset/fix-external-ref-resolution.md b/.changeset/fix-external-ref-resolution.md new file mode 100644 index 000000000..9b133445f --- /dev/null +++ b/.changeset/fix-external-ref-resolution.md @@ -0,0 +1,9 @@ +--- +"@asyncapi/cli": patch +--- + +fix: resolve external $ref files relative to spec file location + +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 `$ref` 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. \ No newline at end of file diff --git a/src/domains/services/generator.service.ts b/src/domains/services/generator.service.ts index b24cda8d6..272683e64 100644 --- a/src/domains/services/generator.service.ts +++ b/src/domains/services/generator.service.ts @@ -17,7 +17,7 @@ import { getErrorMessage } from '@utils/error-handler'; * Options passed to the generator for code generation. */ interface GeneratorRunOptions { - path?: Specification; + path?: string; [key: string]: unknown; } @@ -109,7 +109,7 @@ export class GeneratorService extends BaseService { try { await generator.generateFromString(asyncapi.text(), { ...genOption, - path: asyncapi, + path: asyncapi.getSource(), }); } catch (err: unknown) { s.stop('Generation failed');