Skip to content

Commit 918ad68

Browse files
authored
Merge pull request #1682 from bruno-de-queiroz/master
feat: exposing openApi 3 servers to allow multiple hosts
2 parents 666fec3 + 2fff0db commit 918ad68

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

packages/cli/src/swagger/specGenerator3.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ export class SpecGenerator3 extends SpecGenerator {
6767
if (this.config.license) {
6868
info.license = { name: this.config.license };
6969
}
70-
7170
if (this.config.contact) {
7271
info.contact = this.config.contact;
7372
}
@@ -144,12 +143,9 @@ export class SpecGenerator3 extends SpecGenerator {
144143
private buildServers() {
145144
const basePath = normalisePath(this.config.basePath as string, '/', undefined, false);
146145
const scheme = this.config.schemes ? this.config.schemes[0] : 'https';
147-
const url = this.config.host ? `${scheme}://${this.config.host}${basePath}` : basePath;
148-
return [
149-
{
150-
url,
151-
} as Swagger.Server,
152-
];
146+
const hosts = this.config.servers ? this.config.servers : this.config.host ? [this.config.host!] : undefined;
147+
const convertHost = (host: string) => ({ url: `${scheme}://${host}${basePath}` });
148+
return (hosts?.map(convertHost) || [{ url: basePath }]) as Swagger.Server[];
153149
}
154150

155151
private buildSchema() {

packages/runtime/src/config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ export interface SpecConfig {
7878
*/
7979
host?: string;
8080

81+
/**
82+
* API servers, expressTemplate.g. [production.api.com, staging.api.com]
83+
*
84+
* Only available with the specVersion 3
85+
*/
86+
servers?: string[];
87+
8188
/**
8289
* Base-name of swagger.json or swagger.yaml.
8390
*

tests/unit/swagger/schemaDetails3.spec.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ describe('Definition generation for OpenAPI 3.0.0', () => {
1414
const metadataPost = new MetadataGenerator('./fixtures/controllers/postController.ts').Generate();
1515

1616
const defaultOptions: ExtendedSpecConfig = getDefaultExtendedOptions();
17+
const optionsWithServers = Object.assign<object, ExtendedSpecConfig, Partial<ExtendedSpecConfig>>({}, defaultOptions, {
18+
servers: ['localhost:3000', 'staging.api.com'],
19+
});
1720
const optionsWithNoAdditional = Object.assign<object, ExtendedSpecConfig, Partial<ExtendedSpecConfig>>({}, defaultOptions, {
1821
noImplicitAdditionalProperties: 'silently-remove-extras',
1922
});
@@ -29,13 +32,17 @@ describe('Definition generation for OpenAPI 3.0.0', () => {
2932
/**
3033
* 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.
3134
*/
32-
specName: 'specDefault' | 'specWithNoImplicitExtras' | 'specWithXEnumVarnames' | 'specWithOperationIdTemplate';
35+
specName: 'specDefault' | 'specWithServers' | 'specWithNoImplicitExtras' | 'specWithXEnumVarnames' | 'specWithOperationIdTemplate';
3336
}
3437

3538
const specDefault: SpecAndName = {
3639
spec: new SpecGenerator3(metadataGet, defaultOptions).GetSpec(),
3740
specName: 'specDefault',
3841
};
42+
const specWithServers: SpecAndName = {
43+
spec: new SpecGenerator3(metadataGet, optionsWithServers).GetSpec(),
44+
specName: 'specWithServers',
45+
};
3946
const specWithNoImplicitExtras: SpecAndName = {
4047
spec: new SpecGenerator3(metadataGet, optionsWithNoAdditional).GetSpec(),
4148
specName: 'specWithNoImplicitExtras',
@@ -85,6 +92,11 @@ describe('Definition generation for OpenAPI 3.0.0', () => {
8592
expect(specDefault.spec.servers[0].url).to.match(/localhost:3000/);
8693
});
8794

95+
it('should replace the parent hosts element', () => {
96+
expect(specWithServers.spec.servers[0].url).to.match(/localhost:3000/);
97+
expect(specWithServers.spec.servers[1].url).to.match(/staging\.api\.com/);
98+
});
99+
88100
it('should replace the parent basePath element', () => {
89101
expect(specDefault.spec).to.not.have.property('basePath');
90102
expect(specDefault.spec.servers[0].url).to.match(/\/v1/);

0 commit comments

Comments
 (0)