From 13165fb8fca5e5f82cf0d710c5f1d932fd838aef Mon Sep 17 00:00:00 2001 From: Roman Hotsiy Date: Mon, 12 Feb 2018 09:59:16 +0200 Subject: [PATCH] fix: referenced header name is empty --- src/services/__tests__/fixtures/fields.json | 24 +++++++++++ .../__tests__/models/FieldModel.test.ts | 43 +++++++++++++++++++ src/services/models/Field.ts | 7 ++- 3 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 src/services/__tests__/fixtures/fields.json create mode 100644 src/services/__tests__/models/FieldModel.test.ts diff --git a/src/services/__tests__/fixtures/fields.json b/src/services/__tests__/fixtures/fields.json new file mode 100644 index 00000000..266a030b --- /dev/null +++ b/src/services/__tests__/fixtures/fields.json @@ -0,0 +1,24 @@ +{ + "openapi": "3.0.0", + "info": { + "version": "1.0", + "title": "Foo" + }, + "components": { + "parameters": { + "testParam": { + "in": "path", + "name": "test_name", + "schema": { "type": "string" } + } + }, + "headers": { + "testHeader": { + "description": "The response content language", + "schema": { + "type": "string" + } + } + } + } +} \ No newline at end of file diff --git a/src/services/__tests__/models/FieldModel.test.ts b/src/services/__tests__/models/FieldModel.test.ts new file mode 100644 index 00000000..cc0e8631 --- /dev/null +++ b/src/services/__tests__/models/FieldModel.test.ts @@ -0,0 +1,43 @@ +import { FieldModel } from '../../models/Field'; +import { OpenAPIParser } from '../../OpenAPIParser'; +import { RedocNormalizedOptions } from '../../RedocNormalizedOptions'; + +const opts = new RedocNormalizedOptions({}); + +describe('Models', () => { + describe('FieldModel', () => { + let parser; + const spec = require('../fixtures/fields.json'); + parser = new OpenAPIParser(spec, undefined, opts); + + test('basic field details', () => { + const field = new FieldModel( + parser, + { + $ref: '#/components/parameters/testParam', + }, + '#/components/parameters/testParam', + opts, + ); + + expect(field.name).toEqual('test_name'); + expect(field.in).toEqual('path'); + expect(field.required).toEqual(false); + expect(field.schema.type).toEqual('string'); + }); + + test('field name should populated from name even if $ref (headers)', () => { + const field = new FieldModel( + parser, + { + $ref: '#/components/headers/testHeader', + name: 'Test-Header', + }, + '#/components/headers/testHeader', + opts, + ); + + expect(field.name).toEqual('Test-Header'); + }); + }); +}); diff --git a/src/services/models/Field.ts b/src/services/models/Field.ts index 8b2f29a2..cec5ab5c 100644 --- a/src/services/models/Field.ts +++ b/src/services/models/Field.ts @@ -22,13 +22,12 @@ export class FieldModel { constructor( parser: OpenAPIParser, - infoOrRef: Referenced, + infoOrRef: Referenced & { name?: string }, pointer: string, options: RedocNormalizedOptions, ) { - const info = parser.deref(infoOrRef); - - this.name = info.name; + const info = parser.deref(infoOrRef); + this.name = infoOrRef.name || info.name; this.in = info.in; this.required = !!info.required; const schemaPointer = (parser.isRef(infoOrRef) ? infoOrRef.$ref : pointer) + '/schema';