Skip to content

Commit

Permalink
Merge pull request #805 from thim81/develop-795-binary-format
Browse files Browse the repository at this point in the history
Fix to convert "format:binary" to "type:"file
  • Loading branch information
VShingala authored Jul 19, 2024
2 parents fec6268 + 5a8a889 commit adebab9
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 10 deletions.
10 changes: 4 additions & 6 deletions libV2/schemaUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const schemaFaker = require('../assets/json-schema-faker'),
'ipv4', 'ipv6',
'regex',
'uuid',
'binary',
'json-pointer',
'int64',
'float',
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -579,7 +580,6 @@ let QUERYPARAM = 'query',
if (
property.format === 'decimal' ||
property.format === 'byte' ||
property.format === 'binary' ||
property.format === 'password' ||
property.format === 'unix-time'
) {
Expand Down Expand Up @@ -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'
) {
Expand Down Expand Up @@ -1358,7 +1357,6 @@ let QUERYPARAM = 'query',
}

if (
requestBodySchema.properties[prop].format === 'binary' ||
requestBodySchema.properties[prop].format === 'byte' ||
requestBodySchema.properties[prop].format === 'decimal'
) {
Expand Down Expand Up @@ -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: '',
Expand Down
3 changes: 2 additions & 1 deletion libV2/validationUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});

/**
Expand Down
37 changes: 37 additions & 0 deletions test/data/valid_openapi/form-binary-file.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
}
}
24 changes: 23 additions & 1 deletion test/unit/convertV2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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();
});
});
});
6 changes: 4 additions & 2 deletions test/unit/faker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ describe('JSON SCHEMA FAKER TESTS', function () {
useDefaultValue: true,
useExamplesValue: true,
ignoreMissingRefs: true,
avoidExampleItemsLength: false
avoidExampleItemsLength: false,
failOnInvalidFormat: false
});
});

Expand All @@ -27,7 +28,8 @@ describe('JSON SCHEMA FAKER TESTS', function () {
maxItems: 20,
useDefaultValue: true,
ignoreMissingRefs: true,
avoidExampleItemsLength: true
avoidExampleItemsLength: true,
failOnInvalidFormat: false
});
});

Expand Down

0 comments on commit adebab9

Please sign in to comment.