Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: exposing openApi 3 servers to allow multiple hosts #1682

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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
Loading