From 582968d5794d89fa86619b2c19645ef85e4f35f0 Mon Sep 17 00:00:00 2001 From: Jon Ursenbach Date: Fri, 17 Jul 2020 12:10:40 -0700 Subject: [PATCH] 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 --- packages/tooling/__tests__/operation.test.js | 39 ++++++++++++++++++++ packages/tooling/src/operation.js | 11 +++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/packages/tooling/__tests__/operation.test.js b/packages/tooling/__tests__/operation.test.js index 0f252880..2fb65540 100644 --- a/packages/tooling/__tests__/operation.test.js +++ b/packages/tooling/__tests__/operation.test.js @@ -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()', () => { diff --git a/packages/tooling/src/operation.js b/packages/tooling/src/operation.js index 5d83a419..78e86e99 100644 --- a/packages/tooling/src/operation.js +++ b/packages/tooling/src/operation.js @@ -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) {