Skip to content

Commit

Permalink
fix: referenced header name is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanHotsiy committed Feb 12, 2018
1 parent e1b2065 commit 13165fb
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
24 changes: 24 additions & 0 deletions src/services/__tests__/fixtures/fields.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
}
43 changes: 43 additions & 0 deletions src/services/__tests__/models/FieldModel.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
});
7 changes: 3 additions & 4 deletions src/services/models/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 13165fb

Please sign in to comment.