diff --git a/libV2/schemaUtils.js b/libV2/schemaUtils.js index bb41bbfa..ddc63591 100644 --- a/libV2/schemaUtils.js +++ b/libV2/schemaUtils.js @@ -45,6 +45,7 @@ const schemaFaker = require('../assets/json-schema-faker'), 'ipv4', 'ipv6', 'regex', 'uuid', + 'binary', 'json-pointer', 'int64', 'float', @@ -479,11 +480,11 @@ let QUERYPARAM = 'query', * @param {Object} context - Global context * @param {Object} schema - Schema that is to be resolved * @param {Number} [stack] - Current recursion depth - * @param {String} resolveFor - For which action this resoltion is to be done + * @param {String} resolveFor - For which action this resolution is to be done * @param {Object} seenRef - Map of all the references that have been resolved * @todo: Explore using a directed graph/tree for maintaining seen ref * - * @returns {Object} Returns the object that staisfies the schema + * @returns {Object} Returns the object that satisfies the schema */ resolveSchema = (context, schema, stack = 0, resolveFor = CONVERSION, seenRef = {}) => { if (!schema) { @@ -579,7 +580,6 @@ let QUERYPARAM = 'query', if ( property.format === 'decimal' || property.format === 'byte' || - property.format === 'binary' || property.format === 'password' || property.format === 'unix-time' ) { @@ -843,7 +843,6 @@ let QUERYPARAM = 'query', for (const prop in resolvedSchema.properties) { if (resolvedSchema.properties.hasOwnProperty(prop)) { if ( - resolvedSchema.properties[prop].format === 'binary' || resolvedSchema.properties[prop].format === 'byte' || resolvedSchema.properties[prop].format === 'decimal' ) { @@ -1358,7 +1357,6 @@ let QUERYPARAM = 'query', } if ( - requestBodySchema.properties[prop].format === 'binary' || requestBodySchema.properties[prop].format === 'byte' || requestBodySchema.properties[prop].format === 'decimal' ) { @@ -1518,7 +1516,7 @@ let QUERYPARAM = 'query', // TODO: Add handling for headers from encoding - if (paramSchema && paramSchema.type === 'binary') { + if (paramSchema && paramSchema.type === 'string' && paramSchema.format === 'binary') { param = { key, value: '', diff --git a/libV2/validationUtils.js b/libV2/validationUtils.js index f5f43942..65248fe6 100644 --- a/libV2/validationUtils.js +++ b/libV2/validationUtils.js @@ -85,7 +85,8 @@ schemaFaker.option({ maxItems: 20, // limit on maximum number of items faked for (type: array) useDefaultValue: true, ignoreMissingRefs: true, - avoidExampleItemsLength: true // option to avoid validating type array schema example's minItems and maxItems props. + avoidExampleItemsLength: true, // option to avoid validating type array schema example's minItems and maxItems props. + failOnInvalidFormat: false }); /** diff --git a/test/data/valid_openapi/form-binary-file.json b/test/data/valid_openapi/form-binary-file.json new file mode 100644 index 00000000..9b674793 --- /dev/null +++ b/test/data/valid_openapi/form-binary-file.json @@ -0,0 +1,37 @@ +{ + "openapi": "3.0.3", + "info": { + "title": "Form Data - Binary - OpenAPI 3.0", + "version": "1.0.0" + }, + "paths": { + "/uploadImage": { + "post": { + "summary": "uploads an image", + "description": "", + "operationId": "uploadFile", + "requestBody": { + "required": true, + "content": { + "multipart/form-data": { + "schema": { + "properties": { + "inputfile": { + "type": "string", + "format": "binary", + "description": "The file to be uploaded." + } + } + } + } + } + }, + "responses": { + "200": { + "description": "successful operation" + } + } + } + } + } +} diff --git a/test/unit/convertV2.test.js b/test/unit/convertV2.test.js index 8666139e..1fe5e006 100644 --- a/test/unit/convertV2.test.js +++ b/test/unit/convertV2.test.js @@ -110,7 +110,8 @@ const expect = require('chai').expect, multiExampleResponseCodeMatching = path.join(__dirname, VALID_OPENAPI_PATH, '/multiExampleResponseCodeMatching.json'), duplicateCollectionVars = - path.join(__dirname, VALID_OPENAPI_PATH, '/duplicateCollectionVars.json'); + path.join(__dirname, VALID_OPENAPI_PATH, '/duplicateCollectionVars.json'), + issue795 = path.join(__dirname, VALID_OPENAPI_PATH, '/form-binary-file.json'); describe('The convert v2 Function', function() { @@ -2819,4 +2820,25 @@ describe('The convert v2 Function', function() { done(); }); }); + + it('[Github #795] Should properly convert format binary to form data', function (done) { + var openapi = fs.readFileSync(issue795, 'utf8'), + reqBody, formData; + Converter.convertV2({ type: 'string', data: openapi }, { + requestNameSource: 'Fallback', + indentCharacter: 'Space', + collapseFolders: true, + optimizeConversion: true, + parametersResolution: 'schema' + }, (err, conversionResult) => { + + reqBody = conversionResult.output[0].data.item[0].item[0].request.body; + formData = reqBody.formdata[0]; + + expect(err).to.be.null; + expect(conversionResult.result).to.equal(true); + expect(formData.type).to.equal('file'); + done(); + }); + }); }); diff --git a/test/unit/faker.test.js b/test/unit/faker.test.js index 465e4ae6..4e326c21 100644 --- a/test/unit/faker.test.js +++ b/test/unit/faker.test.js @@ -13,7 +13,8 @@ describe('JSON SCHEMA FAKER TESTS', function () { useDefaultValue: true, useExamplesValue: true, ignoreMissingRefs: true, - avoidExampleItemsLength: false + avoidExampleItemsLength: false, + failOnInvalidFormat: false }); }); @@ -27,7 +28,8 @@ describe('JSON SCHEMA FAKER TESTS', function () { maxItems: 20, useDefaultValue: true, ignoreMissingRefs: true, - avoidExampleItemsLength: true + avoidExampleItemsLength: true, + failOnInvalidFormat: false }); });