Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/domains/services/generator.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@
import { spinner } from '@clack/prompts';
import path from 'path';
import os from 'os';
import { pathToFileURL } from 'url';

Check warning on line 13 in src/domains/services/generator.service.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `node:url` over `url`.

See more on https://sonarcloud.io/project/issues?id=asyncapi_cli&issues=AZ2xA5D-vZY-2e6Qlaqh&open=AZ2xA5D-vZY-2e6Qlaqh&pullRequest=2129
import { yellow, magenta } from 'picocolors';
import { getErrorMessage } from '@utils/error-handler';

/**
* Options passed to the generator for code generation.
* `path` must be a string (document base URI) for @asyncapi/generator to resolve local $ref files.
*/
interface GeneratorRunOptions {
path?: Specification;
path?: string;
[key: string]: unknown;
}

Expand Down Expand Up @@ -72,6 +74,20 @@
}
}

/**
* Base URI for parsing, required so relative file $refs resolve next to the spec file (not cwd).
*/
private getGeneratorParseSource(spec: Specification): string | undefined {
const filePath = spec.getFilePath();
if (filePath !== undefined) {
const absolute = path.isAbsolute(filePath)
? filePath
: path.resolve(filePath);
return pathToFileURL(absolute).href;
}
return spec.getFileURL();
}

/**
* Generates code from an AsyncAPI specification using the specified template.
*
Expand Down Expand Up @@ -107,9 +123,12 @@
: { start: () => null, stop: (message: string) => logs.push(message) };
s.start('Generation in progress. Keep calm and wait a bit');
try {
const parseSource =
this.getGeneratorParseSource(asyncapi) ??
(typeof genOption.path === 'string' ? genOption.path : undefined);
await generator.generateFromString(asyncapi.text(), {
...genOption,
path: asyncapi,
...(parseSource !== undefined ? { path: parseSource } : {}),

Check warning on line 131 in src/domains/services/generator.service.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Unexpected negated condition.

See more on https://sonarcloud.io/project/issues?id=asyncapi_cli&issues=AZ2xA5D-vZY-2e6Qlaqi&open=AZ2xA5D-vZY-2e6Qlaqi&pullRequest=2129
});
} catch (err: unknown) {
s.stop('Generation failed');
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/generate-same-dir-ref/asyncapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
asyncapi: "2.6.0"
info:
title: Same-dir ref fixture
version: 1.0.0
channels:
test:
publish:
message:
$ref: "./messages.yaml#/messages/TestMessage"
4 changes: 4 additions & 0 deletions test/fixtures/generate-same-dir-ref/messages.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
messages:
TestMessage:
payload:
type: string
23 changes: 23 additions & 0 deletions test/integration/generate/fromTemplate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,27 @@ describe('template', () => {
}
);
});

describe('same-directory file $ref', () => {
test
.stdout()
.command([
'generate:fromTemplate',
'./test/fixtures/generate-same-dir-ref/asyncapi.yaml',
'@asyncapi/minimaltemplate',
'--output=./test/docs/same-dir-ref-out',
'--force-write',
nonInteractive,
])
.it(
'resolves relative file refs next to the spec file, not cwd (issue #1839)',
(ctx, done) => {
expect(ctx.stdout).to.contain(
'Check out your shiny new generated files at ./test/docs/same-dir-ref-out.\n\n'
);
cleanup('./test/docs/same-dir-ref-out');
done();
}
);
}).timeout(200000);
});
Loading