diff --git a/.changeset/fix-ref-resolution.md b/.changeset/fix-ref-resolution.md new file mode 100644 index 000000000..ee4d19e2c --- /dev/null +++ b/.changeset/fix-ref-resolution.md @@ -0,0 +1,5 @@ +--- +"@asyncapi/cli": patch +--- + +Fix bug where external files in the same directory were not correctly resolved via `$ref`. diff --git a/src/domains/models/SpecificationFile.ts b/src/domains/models/SpecificationFile.ts index 5370f6e67..96848bdd8 100644 --- a/src/domains/models/SpecificationFile.ts +++ b/src/domains/models/SpecificationFile.ts @@ -81,12 +81,13 @@ export class Specification { static async fromFile(filepath: string) { let spec; + const absolutePath = path.resolve(process.cwd(), filepath); try { - spec = await readFile(filepath, { encoding: 'utf8' }); + spec = await readFile(absolutePath, { encoding: 'utf8' }); } catch { - throw new ErrorLoadingSpec('file', filepath); + throw new ErrorLoadingSpec('file', absolutePath); } - return new Specification(spec, { filepath }); + return new Specification(spec, { filepath: absolutePath }); } static async fromURL(URLpath: string) { diff --git a/src/domains/services/generator.service.ts b/src/domains/services/generator.service.ts index b24cda8d6..8f9a4669e 100644 --- a/src/domains/services/generator.service.ts +++ b/src/domains/services/generator.service.ts @@ -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');