Skip to content

Commit 582968d

Browse files
authored
fix: add support for $ref when pulling content types (#227)
* fix: add support for $ref when pulling content types * chore: changing the name of a test method so it makes sense
1 parent 4b6b669 commit 582968d

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

packages/tooling/__tests__/operation.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,45 @@ describe('#getContentType', () => {
6666
}).getContentType()
6767
).toBe('text/xml');
6868
});
69+
70+
it('should handle cases where the requestBody is a $ref', () => {
71+
const op = new Operation(
72+
{
73+
...petstore,
74+
...{
75+
components: {
76+
requestBodies: {
77+
payload: {
78+
required: true,
79+
content: {
80+
'multipart/form-data': {
81+
schema: {
82+
type: 'object',
83+
properties: {
84+
'Document file': {
85+
type: 'string',
86+
format: 'binary',
87+
},
88+
},
89+
},
90+
},
91+
},
92+
},
93+
},
94+
},
95+
},
96+
},
97+
'/body',
98+
'post',
99+
{
100+
requestBody: {
101+
$ref: '#/components/requestBodies/payload',
102+
},
103+
}
104+
);
105+
106+
expect(op.getContentType()).toBe('multipart/form-data');
107+
});
69108
});
70109

71110
describe('#getSecurity()', () => {

packages/tooling/src/operation.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,16 @@ class Operation {
1010
}
1111

1212
getContentType() {
13-
const types = (this.requestBody && this.requestBody.content && Object.keys(this.requestBody.content)) || [];
13+
let types = [];
14+
if (this.requestBody) {
15+
if ('$ref' in this.requestBody) {
16+
this.requestBody = findSchemaDefinition(this.requestBody.$ref, this.oas);
17+
}
18+
19+
if ('content' in this.requestBody) {
20+
types = Object.keys(this.requestBody.content);
21+
}
22+
}
1423

1524
let type = 'application/json';
1625
if (types && types.length) {

0 commit comments

Comments
 (0)