fix: referenced header name is empty

This commit is contained in:
Roman Hotsiy 2018-02-12 09:59:16 +02:00
parent e1b2065b2e
commit 13165fb8fc
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
3 changed files with 70 additions and 4 deletions

View File

@ -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"
}
}
}
}
}

View File

@ -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');
});
});
});

View File

@ -22,13 +22,12 @@ export class FieldModel {
constructor(
parser: OpenAPIParser,
infoOrRef: Referenced<OpenAPIParameter>,
infoOrRef: Referenced<OpenAPIParameter> & { name?: string },
pointer: string,
options: RedocNormalizedOptions,
) {
const info = parser.deref(infoOrRef);
this.name = info.name;
const info = parser.deref<OpenAPIParameter>(infoOrRef);
this.name = infoOrRef.name || info.name;
this.in = info.in;
this.required = !!info.required;
const schemaPointer = (parser.isRef(infoOrRef) ? infoOrRef.$ref : pointer) + '/schema';