Skip to content

Commit

Permalink
fix: add support for $ref when pulling content types (#227)
Browse files Browse the repository at this point in the history
* fix: add support for $ref when pulling content types

* chore: changing the name of a test method so it makes sense
  • Loading branch information
erunion committed Jul 17, 2020
1 parent 4b6b669 commit 582968d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
39 changes: 39 additions & 0 deletions packages/tooling/__tests__/operation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,45 @@ describe('#getContentType', () => {
}).getContentType()
).toBe('text/xml');
});

it('should handle cases where the requestBody is a $ref', () => {
const op = new Operation(
{
...petstore,
...{
components: {
requestBodies: {
payload: {
required: true,
content: {
'multipart/form-data': {
schema: {
type: 'object',
properties: {
'Document file': {
type: 'string',
format: 'binary',
},
},
},
},
},
},
},
},
},
},
'/body',
'post',
{
requestBody: {
$ref: '#/components/requestBodies/payload',
},
}
);

expect(op.getContentType()).toBe('multipart/form-data');
});
});

describe('#getSecurity()', () => {
Expand Down
11 changes: 10 additions & 1 deletion packages/tooling/src/operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@ class Operation {
}

getContentType() {
const types = (this.requestBody && this.requestBody.content && Object.keys(this.requestBody.content)) || [];
let types = [];
if (this.requestBody) {
if ('$ref' in this.requestBody) {
this.requestBody = findSchemaDefinition(this.requestBody.$ref, this.oas);
}

if ('content' in this.requestBody) {
types = Object.keys(this.requestBody.content);
}
}

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

0 comments on commit 582968d

Please sign in to comment.