Skip to content

Commit adebab9

Browse files
authored
Merge pull request #805 from thim81/develop-795-binary-format
Fix to convert "format:binary" to "type:"file
2 parents fec6268 + 5a8a889 commit adebab9

File tree

5 files changed

+70
-10
lines changed

5 files changed

+70
-10
lines changed

libV2/schemaUtils.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const schemaFaker = require('../assets/json-schema-faker'),
4545
'ipv4', 'ipv6',
4646
'regex',
4747
'uuid',
48+
'binary',
4849
'json-pointer',
4950
'int64',
5051
'float',
@@ -479,11 +480,11 @@ let QUERYPARAM = 'query',
479480
* @param {Object} context - Global context
480481
* @param {Object} schema - Schema that is to be resolved
481482
* @param {Number} [stack] - Current recursion depth
482-
* @param {String} resolveFor - For which action this resoltion is to be done
483+
* @param {String} resolveFor - For which action this resolution is to be done
483484
* @param {Object} seenRef - Map of all the references that have been resolved
484485
* @todo: Explore using a directed graph/tree for maintaining seen ref
485486
*
486-
* @returns {Object} Returns the object that staisfies the schema
487+
* @returns {Object} Returns the object that satisfies the schema
487488
*/
488489
resolveSchema = (context, schema, stack = 0, resolveFor = CONVERSION, seenRef = {}) => {
489490
if (!schema) {
@@ -579,7 +580,6 @@ let QUERYPARAM = 'query',
579580
if (
580581
property.format === 'decimal' ||
581582
property.format === 'byte' ||
582-
property.format === 'binary' ||
583583
property.format === 'password' ||
584584
property.format === 'unix-time'
585585
) {
@@ -843,7 +843,6 @@ let QUERYPARAM = 'query',
843843
for (const prop in resolvedSchema.properties) {
844844
if (resolvedSchema.properties.hasOwnProperty(prop)) {
845845
if (
846-
resolvedSchema.properties[prop].format === 'binary' ||
847846
resolvedSchema.properties[prop].format === 'byte' ||
848847
resolvedSchema.properties[prop].format === 'decimal'
849848
) {
@@ -1358,7 +1357,6 @@ let QUERYPARAM = 'query',
13581357
}
13591358

13601359
if (
1361-
requestBodySchema.properties[prop].format === 'binary' ||
13621360
requestBodySchema.properties[prop].format === 'byte' ||
13631361
requestBodySchema.properties[prop].format === 'decimal'
13641362
) {
@@ -1518,7 +1516,7 @@ let QUERYPARAM = 'query',
15181516

15191517
// TODO: Add handling for headers from encoding
15201518

1521-
if (paramSchema && paramSchema.type === 'binary') {
1519+
if (paramSchema && paramSchema.type === 'string' && paramSchema.format === 'binary') {
15221520
param = {
15231521
key,
15241522
value: '',

libV2/validationUtils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ schemaFaker.option({
8585
maxItems: 20, // limit on maximum number of items faked for (type: array)
8686
useDefaultValue: true,
8787
ignoreMissingRefs: true,
88-
avoidExampleItemsLength: true // option to avoid validating type array schema example's minItems and maxItems props.
88+
avoidExampleItemsLength: true, // option to avoid validating type array schema example's minItems and maxItems props.
89+
failOnInvalidFormat: false
8990
});
9091

9192
/**
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"openapi": "3.0.3",
3+
"info": {
4+
"title": "Form Data - Binary - OpenAPI 3.0",
5+
"version": "1.0.0"
6+
},
7+
"paths": {
8+
"/uploadImage": {
9+
"post": {
10+
"summary": "uploads an image",
11+
"description": "",
12+
"operationId": "uploadFile",
13+
"requestBody": {
14+
"required": true,
15+
"content": {
16+
"multipart/form-data": {
17+
"schema": {
18+
"properties": {
19+
"inputfile": {
20+
"type": "string",
21+
"format": "binary",
22+
"description": "The file to be uploaded."
23+
}
24+
}
25+
}
26+
}
27+
}
28+
},
29+
"responses": {
30+
"200": {
31+
"description": "successful operation"
32+
}
33+
}
34+
}
35+
}
36+
}
37+
}

test/unit/convertV2.test.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ const expect = require('chai').expect,
110110
multiExampleResponseCodeMatching =
111111
path.join(__dirname, VALID_OPENAPI_PATH, '/multiExampleResponseCodeMatching.json'),
112112
duplicateCollectionVars =
113-
path.join(__dirname, VALID_OPENAPI_PATH, '/duplicateCollectionVars.json');
113+
path.join(__dirname, VALID_OPENAPI_PATH, '/duplicateCollectionVars.json'),
114+
issue795 = path.join(__dirname, VALID_OPENAPI_PATH, '/form-binary-file.json');
114115

115116

116117
describe('The convert v2 Function', function() {
@@ -2819,4 +2820,25 @@ describe('The convert v2 Function', function() {
28192820
done();
28202821
});
28212822
});
2823+
2824+
it('[Github #795] Should properly convert format binary to form data', function (done) {
2825+
var openapi = fs.readFileSync(issue795, 'utf8'),
2826+
reqBody, formData;
2827+
Converter.convertV2({ type: 'string', data: openapi }, {
2828+
requestNameSource: 'Fallback',
2829+
indentCharacter: 'Space',
2830+
collapseFolders: true,
2831+
optimizeConversion: true,
2832+
parametersResolution: 'schema'
2833+
}, (err, conversionResult) => {
2834+
2835+
reqBody = conversionResult.output[0].data.item[0].item[0].request.body;
2836+
formData = reqBody.formdata[0];
2837+
2838+
expect(err).to.be.null;
2839+
expect(conversionResult.result).to.equal(true);
2840+
expect(formData.type).to.equal('file');
2841+
done();
2842+
});
2843+
});
28222844
});

test/unit/faker.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ describe('JSON SCHEMA FAKER TESTS', function () {
1313
useDefaultValue: true,
1414
useExamplesValue: true,
1515
ignoreMissingRefs: true,
16-
avoidExampleItemsLength: false
16+
avoidExampleItemsLength: false,
17+
failOnInvalidFormat: false
1718
});
1819
});
1920

@@ -27,7 +28,8 @@ describe('JSON SCHEMA FAKER TESTS', function () {
2728
maxItems: 20,
2829
useDefaultValue: true,
2930
ignoreMissingRefs: true,
30-
avoidExampleItemsLength: true
31+
avoidExampleItemsLength: true,
32+
failOnInvalidFormat: false
3133
});
3234
});
3335

0 commit comments

Comments
 (0)