Skip to content

Commit

Permalink
Fix to convert "format:binary "to type:"file
Browse files Browse the repository at this point in the history
  • Loading branch information
thim81 committed Jul 16, 2024
1 parent fec6268 commit c753ff0
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
9 changes: 7 additions & 2 deletions libV2/schemaUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,11 +479,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 @@ -576,6 +576,11 @@ let QUERYPARAM = 'query',
return;
}

// Set type to binary
if (property.format === 'binary') {
property.type = 'binary';
}

if (
property.format === 'decimal' ||
property.format === 'byte' ||
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();
});
});
});

0 comments on commit c753ff0

Please sign in to comment.