Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add support for $ref when pulling content types #227

Merged
merged 2 commits into from
Jul 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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