diff --git a/README.md b/README.md index 625931ee43..8163836663 100644 --- a/README.md +++ b/README.md @@ -213,6 +213,8 @@ You can use all of the following options with the standalone version of the { const { license = { identifier: null } } = new ApiInfoModel(parser); expect(license.identifier).toEqual('MIT'); }); + + test('should correctly populate default download file name', () => { + parser.spec = { + openapi: '3.0.0', + info: { + description: 'Test description', + }, + } as any; + + const info = new ApiInfoModel(parser); + expect(info.downloadFileName).toEqual('openapi.json'); + }); + + test('should correctly populate default download file is undefined when using specUrl', () => { + parser = new OpenAPIParser( + { + openapi: '3.0.0', + info: { + description: 'Test description', + }, + } as any, + '/demo/openapi.yaml', + opts, + ); + + const info = new ApiInfoModel(parser); + expect(info.downloadFileName).toEqual(undefined); + }); + + test('should correctly populate download file name', () => { + parser.spec = { + info: { + description: 'Test description', + }, + } as any; + + const opts = new RedocNormalizedOptions({ + downloadFileName: 'test.yaml', + }); + + const info = new ApiInfoModel(parser, opts); + expect(info.downloadFileName).toEqual('test.yaml'); + }); + + test('should correctly populate download link', () => { + parser.spec = { + openapi: '3.0.0', + info: { + description: 'Test description', + }, + } as any; + + const opts = new RedocNormalizedOptions({ + downloadDefinitionUrl: 'https:test.com/filename.yaml', + }); + const info = new ApiInfoModel(parser, opts); + expect(info.downloadLink).toEqual('https:test.com/filename.yaml'); + }); + + test('should correctly populate download link and download file name', () => { + parser.spec = { + openapi: '3.0.0', + info: { + description: 'Test description', + }, + } as any; + + const opts = new RedocNormalizedOptions({ + downloadDefinitionUrl: 'https:test.com/filename.yaml', + downloadFileName: 'test.yaml', + }); + const info = new ApiInfoModel(parser, opts); + expect(info.downloadLink).toEqual('https:test.com/filename.yaml'); + expect(info.downloadFileName).toEqual('test.yaml'); + }); }); }); diff --git a/src/services/models/ApiInfo.ts b/src/services/models/ApiInfo.ts index 517538db0b..0c4d91ebd2 100644 --- a/src/services/models/ApiInfo.ts +++ b/src/services/models/ApiInfo.ts @@ -1,6 +1,7 @@ import { OpenAPIContact, OpenAPIInfo, OpenAPILicense } from '../../types'; import { IS_BROWSER } from '../../utils/'; import { OpenAPIParser } from '../OpenAPIParser'; +import { RedocNormalizedOptions } from '../RedocNormalizedOptions'; export class ApiInfoModel implements OpenAPIInfo { title: string; @@ -15,7 +16,10 @@ export class ApiInfoModel implements OpenAPIInfo { downloadLink?: string; downloadFileName?: string; - constructor(private parser: OpenAPIParser) { + constructor( + private parser: OpenAPIParser, + private options: RedocNormalizedOptions = new RedocNormalizedOptions({}), + ) { Object.assign(this, parser.spec.info); this.description = parser.spec.info.description || ''; this.summary = parser.spec.info.summary || ''; @@ -30,6 +34,10 @@ export class ApiInfoModel implements OpenAPIInfo { } private getDownloadLink(): string | undefined { + if (this.options.downloadDefinitionUrl) { + return this.options.downloadDefinitionUrl; + } + if (this.parser.specUrl) { return this.parser.specUrl; } @@ -43,9 +51,9 @@ export class ApiInfoModel implements OpenAPIInfo { } private getDownloadFileName(): string | undefined { - if (!this.parser.specUrl) { - return 'swagger.json'; + if (!this.parser.specUrl && !this.options.downloadDefinitionUrl) { + return this.options.downloadFileName || 'openapi.json'; } - return undefined; + return this.options.downloadFileName; } }