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

File tree

2 files changed

+48
-11
lines changed

2 files changed

+48
-11
lines changed

packages/api-explorer/__tests__/ResponseSchema.test.jsx

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,45 @@ test('should contain ResponseSchemaBody element if $ref exist for "application/x
149149
};
150150

151151
const responseSchema = shallow(<ResponseSchema {...testProps} />);
152-
expect(responseSchema.text()).toContain('ResponseSchemaBody');
152+
expect(responseSchema.find('ResponseSchemaBody').length).toBe(1);
153+
});
154+
155+
test('should allow $ref lookup at the responses object level', () => {
156+
const testOas = new Oas({
157+
components: {
158+
responses: {
159+
Response: {
160+
content: {
161+
'application/json': {
162+
schema: {
163+
type: 'string',
164+
},
165+
},
166+
},
167+
},
168+
},
169+
},
170+
paths: {
171+
'/ref-responses': {
172+
get: {
173+
responses: {
174+
200: {
175+
$ref: '#/components/responses/Response',
176+
},
177+
},
178+
},
179+
},
180+
},
181+
});
182+
183+
const responseSchema = shallow(
184+
<ResponseSchema
185+
{...props}
186+
oas={testOas}
187+
operation={testOas.operation('/ref-responses', 'get')}
188+
/>,
189+
);
190+
expect(responseSchema.find('ResponseSchemaBody').length).toBe(1);
153191
});
154192

155193
test('should change selectedStatus in component', () => {

packages/api-explorer/src/ResponseSchema.jsx

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@ class ResponseSchema extends React.Component {
1919
}
2020

2121
getSchema(operation) {
22-
const content = this.getContent(operation);
22+
const oas = this.props.oas;
23+
const content = this.getContent(operation, oas);
2324

2425
if (!content) return null;
2526

26-
const oas = this.props.oas;
27-
2827
const firstContentType = Object.keys(content)[0];
2928

3029
if (
@@ -42,14 +41,14 @@ class ResponseSchema extends React.Component {
4241
return null;
4342
}
4443

45-
getContent(operation) {
44+
getContent(operation, oas) {
4645
const status = this.state.selectedStatus;
47-
return (
48-
operation &&
49-
operation.responses &&
50-
operation.responses[status] &&
51-
operation.responses[status].content
52-
);
46+
const response = operation && operation.responses && operation.responses[status];
47+
48+
if (!response) return false;
49+
50+
if (response.$ref) return findSchemaDefinition(response.$ref, oas).content;
51+
return response.content;
5352
}
5453

5554
changeHandler(e) {

0 commit comments

Comments
 (0)