Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
Add support for $ref lookups in ResponseSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
domharrington committed Dec 19, 2018
1 parent 3d4a79e commit c150cca
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
40 changes: 39 additions & 1 deletion packages/api-explorer/__tests__/ResponseSchema.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,45 @@ test('should contain ResponseSchemaBody element if $ref exist for "application/x
};

const responseSchema = shallow(<ResponseSchema {...testProps} />);
expect(responseSchema.text()).toContain('ResponseSchemaBody');
expect(responseSchema.find('ResponseSchemaBody').length).toBe(1);
});

test('should allow $ref lookup at the responses object level', () => {
const testOas = new Oas({
components: {
responses: {
Response: {
content: {
'application/json': {
schema: {
type: 'string',
},
},
},
},
},
},
paths: {
'/ref-responses': {
get: {
responses: {
200: {
$ref: '#/components/responses/Response',
},
},
},
},
},
});

const responseSchema = shallow(
<ResponseSchema
{...props}
oas={testOas}
operation={testOas.operation('/ref-responses', 'get')}
/>,
);
expect(responseSchema.find('ResponseSchemaBody').length).toBe(1);
});

test('should change selectedStatus in component', () => {
Expand Down
19 changes: 9 additions & 10 deletions packages/api-explorer/src/ResponseSchema.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ class ResponseSchema extends React.Component {
}

getSchema(operation) {
const content = this.getContent(operation);
const oas = this.props.oas;
const content = this.getContent(operation, oas);

if (!content) return null;

const oas = this.props.oas;

const firstContentType = Object.keys(content)[0];

if (
Expand All @@ -42,14 +41,14 @@ class ResponseSchema extends React.Component {
return null;
}

getContent(operation) {
getContent(operation, oas) {
const status = this.state.selectedStatus;
return (
operation &&
operation.responses &&
operation.responses[status] &&
operation.responses[status].content
);
const response = operation && operation.responses && operation.responses[status];

if (!response) return false;

if (response.$ref) return findSchemaDefinition(response.$ref, oas).content;
return response.content;
}

changeHandler(e) {
Expand Down

0 comments on commit c150cca

Please sign in to comment.