From 90ed717bb55699f5dc26f6bfd6b5f215b85d40ff Mon Sep 17 00:00:00 2001 From: Roman Hotsiy Date: Tue, 7 Aug 2018 11:27:41 +0300 Subject: [PATCH] fix: description is not rendered if doesn't containt markdown headings fixes #591 --- src/services/__tests__/models/ApiInfo.test.ts | 38 +++++++++++++++++++ src/services/models/ApiInfo.ts | 5 ++- 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 src/services/__tests__/models/ApiInfo.test.ts diff --git a/src/services/__tests__/models/ApiInfo.test.ts b/src/services/__tests__/models/ApiInfo.test.ts new file mode 100644 index 00000000..5adb6036 --- /dev/null +++ b/src/services/__tests__/models/ApiInfo.test.ts @@ -0,0 +1,38 @@ +import { ApiInfoModel } from '../../models/ApiInfo'; +import { OpenAPIParser } from '../../OpenAPIParser'; +import { RedocNormalizedOptions } from '../../RedocNormalizedOptions'; + +const opts = new RedocNormalizedOptions({}); +describe('Models', () => { + describe('ResponseModel', () => { + let parser: OpenAPIParser; + + beforeEach(() => { + parser = new OpenAPIParser({ openapi: '3.0.0' } as any, undefined, opts); + }); + + test('should correctly populate description field without md headings', () => { + parser.spec = { + openapi: '3.0.0', + info: { + description: 'Test description', + }, + } as any; + + const info = new ApiInfoModel(parser); + expect(info.description).toEqual('Test description'); + }); + + test('should correctly populate description up to the first md heading', () => { + parser.spec = { + openapi: '3.0.0', + info: { + description: 'Test description\nsome text\n## Heading\n test', + }, + } as any; + + const info = new ApiInfoModel(parser); + expect(info.description).toEqual('Test description\nsome text\n'); + }); + }); +}); diff --git a/src/services/models/ApiInfo.ts b/src/services/models/ApiInfo.ts index 29455012..ceb38d00 100644 --- a/src/services/models/ApiInfo.ts +++ b/src/services/models/ApiInfo.ts @@ -14,7 +14,10 @@ export class ApiInfoModel implements OpenAPIInfo { constructor(private parser: OpenAPIParser) { Object.assign(this, parser.spec.info); this.description = parser.spec.info.description || ''; - this.description = this.description.substring(0, this.description.search(/^##?\s+/m)); + const firstHeadingLinePos = this.description.search(/^##?\s+/m); + if (firstHeadingLinePos > -1) { + this.description = this.description.substring(0, firstHeadingLinePos); + } } get downloadLink(): string | undefined {