From 4dd52b33b13322eaa0c20c6df4ea582073f90a24 Mon Sep 17 00:00:00 2001 From: Jon Ursenbach Date: Thu, 13 Aug 2020 14:51:36 -0700 Subject: [PATCH] feat: adding some new isMultipart and isJson methods on the operation (#248) --- packages/tooling/__tests__/operation.test.js | 58 ++++++++++++++++++++ packages/tooling/src/operation.js | 32 +++++++++-- 2 files changed, 86 insertions(+), 4 deletions(-) diff --git a/packages/tooling/__tests__/operation.test.js b/packages/tooling/__tests__/operation.test.js index 2fb65540..5782f940 100644 --- a/packages/tooling/__tests__/operation.test.js +++ b/packages/tooling/__tests__/operation.test.js @@ -107,6 +107,64 @@ describe('#getContentType', () => { }); }); +describe('#isMultipart', () => { + it.each([['multipart/mixed'], ['multipart/related'], ['multipart/form-data'], ['multipart/alternative']])( + 'should identify `%s` as multipart', + contentType => { + const op = new Operation(petstore, '/multipart', 'get', { + requestBody: { + content: { + [contentType]: { + schema: { + type: 'object', + properties: { + documentFile: { + type: 'string', + format: 'binary', + }, + }, + }, + }, + }, + }, + }); + + expect(op.getContentType()).toBe(contentType); + expect(op.isMultipart()).toBe(true); + expect(op.isJson()).toBe(false); + } + ); +}); + +describe('#isJson', () => { + it.each([ + ['application/json'], + ['application/x-json'], + ['text/json'], + ['text/x-json'], + ['application/vnd.github.v3.star+json'], + ])('should identify `%s` as json', contentType => { + const op = new Operation(petstore, '/json', 'get', { + requestBody: { + content: { + [contentType]: { + schema: { + type: 'array', + items: { + type: 'string', + }, + }, + }, + }, + }, + }); + + expect(op.getContentType()).toBe(contentType); + expect(op.isJson()).toBe(true); + expect(op.isMultipart()).toBe(false); + }); +}); + describe('#getSecurity()', () => { const security = [{ auth: [] }]; diff --git a/packages/tooling/src/operation.js b/packages/tooling/src/operation.js index 78e86e99..6b91a7bf 100644 --- a/packages/tooling/src/operation.js +++ b/packages/tooling/src/operation.js @@ -1,6 +1,12 @@ /* eslint-disable no-underscore-dangle */ const findSchemaDefinition = require('./lib/find-schema-definition'); +function matchesMimeType(arr, contentType) { + return arr.some(function (type) { + return contentType.indexOf(type) > -1; + }); +} + class Operation { constructor(oas, path, method, operation) { Object.assign(this, operation); @@ -10,6 +16,10 @@ class Operation { } getContentType() { + if (typeof this.contentType !== 'undefined') { + return this.contentType; + } + let types = []; if (this.requestBody) { if ('$ref' in this.requestBody) { @@ -21,19 +31,33 @@ class Operation { } } - let type = 'application/json'; + this.contentType = 'application/json'; if (types && types.length) { - type = types[0]; + this.contentType = types[0]; } // Favor JSON if it exists types.forEach(t => { if (t.match(/json/)) { - type = t; + this.contentType = t; } }); - return type; + return this.contentType; + } + + isMultipart() { + return matchesMimeType( + ['multipart/mixed', 'multipart/related', 'multipart/form-data', 'multipart/alternative'], + this.getContentType() + ); + } + + isJson() { + return matchesMimeType( + ['application/json', 'application/x-json', 'text/json', 'text/x-json', '+json'], + this.getContentType() + ); } getSecurity() {