Skip to content

Commit

Permalink
feat: exposing openApi 3 servers to allow multiple hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruno de Queiroz committed Sep 19, 2024
1 parent d17e4e5 commit e41794f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
10 changes: 3 additions & 7 deletions packages/cli/src/swagger/specGenerator3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ export class SpecGenerator3 extends SpecGenerator {
if (this.config.license) {
info.license = { name: this.config.license };
}

if (this.config.contact) {
info.contact = this.config.contact;
}
Expand Down Expand Up @@ -144,12 +143,9 @@ export class SpecGenerator3 extends SpecGenerator {
private buildServers() {
const basePath = normalisePath(this.config.basePath as string, '/', undefined, false);
const scheme = this.config.schemes ? this.config.schemes[0] : 'https';
const url = this.config.host ? `${scheme}://${this.config.host}${basePath}` : basePath;
return [
{
url,
} as Swagger.Server,
];
const hosts = this.config.servers ? this.config.servers : this.config.host ? [this.config.host!] : undefined;
const convertHost = (host: string) => ({ url: `${scheme}://${host}${basePath}` });
return (hosts?.map(convertHost) || [{ url: basePath }]) as Swagger.Server[];
}

private buildSchema() {
Expand Down
7 changes: 7 additions & 0 deletions packages/runtime/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ export interface SpecConfig {
*/
host?: string;

/**
* API servers, expressTemplate.g. [production.api.com, staging.api.com]
*
* Only available with the specVersion 3
*/
servers?: string[];

/**
* Base-name of swagger.json or swagger.yaml.
*
Expand Down
14 changes: 13 additions & 1 deletion tests/unit/swagger/schemaDetails3.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ describe('Definition generation for OpenAPI 3.0.0', () => {
const metadataPost = new MetadataGenerator('./fixtures/controllers/postController.ts').Generate();

const defaultOptions: ExtendedSpecConfig = getDefaultExtendedOptions();
const optionsWithServers = Object.assign<object, ExtendedSpecConfig, Partial<ExtendedSpecConfig>>({}, defaultOptions, {
servers: ['localhost:3000', 'staging.api.com'],
});
const optionsWithNoAdditional = Object.assign<object, ExtendedSpecConfig, Partial<ExtendedSpecConfig>>({}, defaultOptions, {
noImplicitAdditionalProperties: 'silently-remove-extras',
});
Expand All @@ -29,13 +32,17 @@ describe('Definition generation for OpenAPI 3.0.0', () => {
/**
* If you want to add another spec here go for it. The reason why we use a string literal is so that tests below won't have "magic string" errors when expected test results differ based on the name of the spec you're testing.
*/
specName: 'specDefault' | 'specWithNoImplicitExtras' | 'specWithXEnumVarnames' | 'specWithOperationIdTemplate';
specName: 'specDefault' | 'specWithServers' | 'specWithNoImplicitExtras' | 'specWithXEnumVarnames' | 'specWithOperationIdTemplate';
}

const specDefault: SpecAndName = {
spec: new SpecGenerator3(metadataGet, defaultOptions).GetSpec(),
specName: 'specDefault',
};
const specWithServers: SpecAndName = {
spec: new SpecGenerator3(metadataGet, optionsWithServers).GetSpec(),
specName: 'specWithServers',
};
const specWithNoImplicitExtras: SpecAndName = {
spec: new SpecGenerator3(metadataGet, optionsWithNoAdditional).GetSpec(),
specName: 'specWithNoImplicitExtras',
Expand Down Expand Up @@ -85,6 +92,11 @@ describe('Definition generation for OpenAPI 3.0.0', () => {
expect(specDefault.spec.servers[0].url).to.match(/localhost:3000/);
});

it('should replace the parent hosts element', () => {
expect(specWithServers.spec.servers[0].url).to.match(/localhost:3000/);
expect(specWithServers.spec.servers[1].url).to.match(/staging\.api\.com/);
});

it('should replace the parent basePath element', () => {
expect(specDefault.spec).to.not.have.property('basePath');
expect(specDefault.spec.servers[0].url).to.match(/\/v1/);
Expand Down

0 comments on commit e41794f

Please sign in to comment.