diff --git a/src/services/__tests__/models/ApiInfo.test.ts b/src/services/__tests__/models/ApiInfo.test.ts new file mode 100644 index 0000000000..5adb6036b7 --- /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 294550122d..ceb38d0026 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 {