Skip to content

Commit 215a5da

Browse files
authored
Merge pull request #516 from postmanlabs/release/3.2.0
Released v3.2.0
2 parents 1777f81 + 6049afd commit 215a5da

26 files changed

+2040
-4755
lines changed

.travis.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
language: node_js
2+
dist: 'xenial'
23
node_js:
34
- "8"
4-
- "9"
55
- "10"
6-
- "11"
76
- "12"
8-
- "node"
97
- "lts/*"

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# OpenAPI-Postman Changelog
22

3+
#### v3.2.0 (May 02, 2022)
4+
* Fixed some of critical and high level severity vulnerabilities.
5+
* Fixed issue [#10752](https://github.com/postmanlabs/postman-app-support/issues/10752) where deepObject style parameters were not generated correctly.
6+
* Fixed issue [#485](https://github.com/postmanlabs/openapi-to-postman/issues/485) where validateTransaction() returns result where mismatch path for transaction are incorrect.
7+
* Fixed issue [#485](https://github.com/postmanlabs/openapi-to-postman/issues/485) where validateTransaction() returns result where mismatch path for transaction are incorrect.
8+
39
#### v3.1.0 (March 04, 2022)
410
* Removed usage of schema resolution cache to avoid incorrect resolution.
511
* Fixed issue where newly converted collection had mismatches from validateTransaction() API.

lib/ajValidation/ajvValidation.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
const { formatDataPath } = require('../common/schemaUtilsCommon');
1+
const { formatDataPath,
2+
formatSchemaPathFromAJVErrorToConvertToDataPath,
3+
isTypeValue } = require('../common/schemaUtilsCommon');
24

35
var _ = require('lodash');
46
const IGNORED_KEYWORDS = ['propertyNames', 'const', 'additionalItems', 'dependencies'],
@@ -97,6 +99,14 @@ function validateSchema (schema, valueToUse, options = {}, jsonSchemaDialect) {
9799
isPmVariable(dataPath === '' ? valueToUse : _.get(valueToUse, dataPath))) {
98100
return false;
99101
}
102+
103+
if (validationError.keyword === 'type' || validationError.keyword === 'format') {
104+
let schemaDataPath = formatDataPath(formatSchemaPathFromAJVErrorToConvertToDataPath(validationError.schemaPath)),
105+
schemaToUse = schemaDataPath ? _.get(schema, schemaDataPath) : schema,
106+
valueToValidate = dataPath ? _.get(valueToUse, dataPath) : valueToUse;
107+
return !isTypeValue(valueToValidate, schemaToUse);
108+
}
109+
100110
return true;
101111
});
102112

@@ -110,5 +120,6 @@ module.exports = {
110120
validateSchema,
111121
getLocalDraft,
112122
getAjvValidator,
113-
getDraftToUse
123+
getDraftToUse,
124+
isTypeValue
114125
};

lib/common/schemaUtilsCommon.js

Lines changed: 248 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,207 @@
22
* This file contains util functions that are common between versions
33
*/
44

5-
const parse = require('../parse.js');
5+
const parse = require('../parse.js'),
6+
_ = require('lodash'),
7+
typesMap = {
8+
integer: {
9+
int32: '<integer>',
10+
int64: '<long>'
11+
},
12+
number: {
13+
float: '<float>',
14+
double: '<double>'
15+
},
16+
string: {
17+
byte: '<byte>',
18+
binary: '<binary>',
19+
date: '<date>',
20+
'date-time': '<dateTime>',
21+
password: '<password>'
22+
},
23+
boolean: '<boolean>',
24+
array: '<array>',
25+
object: '<object>'
26+
},
27+
28+
/* eslint-disable arrow-body-style */
29+
schemaTypeToJsValidator = {
30+
'string': (d) => typeof d === 'string',
31+
'number': (d) => !isNaN(d),
32+
'integer': (d) => !isNaN(d) && Number.isInteger(Number(d)),
33+
'boolean': (d) => _.isBoolean(d) || d === 'true' || d === 'false',
34+
'array': (d) => Array.isArray(d),
35+
'object': (d) => typeof d === 'object' && !Array.isArray(d)
36+
};
37+
38+
/**
39+
* Remove the # character from the beginning of a schema path
40+
* @param {string} schemaPath - a defined schemaPath
41+
* @returns {string} - the schema path with # removed
42+
*/
43+
function removeSharpAndSlashFromFirstPosition(schemaPath) {
44+
return schemaPath[0] === '#' ? schemaPath.slice(2) : schemaPath;
45+
}
46+
47+
/**
48+
* Remove the defined word from the last position of a schema path
49+
* @param {string} schemaPath - a defined schemaPath
50+
* @param {string} word - word to remove
51+
* @returns {string} - the schema path with type removed
52+
*/
53+
function removeWordFromLastPosition(schemaPath, word) {
54+
let splittedDataPath = schemaPath.split('/');
55+
if (splittedDataPath[splittedDataPath.length - 1] === word) {
56+
splittedDataPath.splice(-1);
57+
}
58+
return splittedDataPath.join('/');
59+
}
60+
61+
/**
62+
* Checks if value is the representation of its type like:
63+
* "<integer>"
64+
*
65+
* @param {string} value - Value to check for
66+
* @param {string} type - The type in the schemna
67+
* @returns {Boolean} the value is the representation of its type
68+
*/
69+
function compareType(value, type) {
70+
return value === '<' + type + '>';
71+
}
72+
73+
/**
74+
* Checks if value is the representation of its type like:
75+
* "<integer>"
76+
* Works in array types
77+
* @param {string} value - Value to check for
78+
* @param {*} types - The types in the schemna
79+
* @returns {Boolean} the value is the representation of its type
80+
*/
81+
function isTypeValueArrayCheck(value, types) {
82+
return types.find((type) => {
83+
return compareType(value, type);
84+
}) !== undefined;
85+
}
86+
87+
/**
88+
* Checks if value is the representation of its type like:
89+
* "<integer>"
90+
* Works in array types
91+
* @param {string} value - Value to check for
92+
* @param {*} types - The types in the schemna
93+
* @returns {Boolean} the value is the representation of its type
94+
*/
95+
function checkValueOnlyTypes(value, types) {
96+
return Array.isArray(types) ? isTypeValueArrayCheck(value, types) : compareType(value, types);
97+
}
98+
99+
/**
100+
* Checks if value is postman variable or not
101+
*
102+
* @param {string} type - type to look for
103+
* @param {string} format - format from schema
104+
* @returns {Boolean} postman variable or not
105+
*/
106+
function getDefaultFromTypeAndFormat(type, format) {
107+
return typesMap[type][format];
108+
}
109+
110+
111+
/**
112+
* Checks if value is the representation of its type like:
113+
* "<integer>"
114+
* Works in array types
115+
* @param {string} value - Value to check for
116+
* @param {*} types - The types in the schema
117+
* @param {*} format - format from the schema
118+
* @returns {Boolean} the value is the representation of its type
119+
*/
120+
function checkValueTypesAndFormat(value, types, format) {
121+
let typesNotInMapp = [],
122+
typesArray = Array.isArray(types) ? types : [types],
123+
found = typesArray.find((type) => {
124+
let defaultValue;
125+
if (typesMap.hasOwnProperty(type)) {
126+
defaultValue = getDefaultFromTypeAndFormat(type, format);
127+
128+
// in case the format is a custom format (email, hostname etc.)
129+
// https://swagger.io/docs/specification/data-models/data-types/#string
130+
if (!defaultValue && format) {
131+
defaultValue = '<' + format + '>';
132+
}
133+
}
134+
else {
135+
typesNotInMapp.push(type);
136+
}
137+
return defaultValue === value;
138+
});
139+
140+
if (found) {
141+
return true;
142+
}
143+
144+
found = typesNotInMapp.find((type) => {
145+
let defaultValue;
146+
defaultValue = '<' + type + (format ? ('-' + format) : '') + '>';
147+
return defaultValue === value;
148+
});
149+
150+
return found !== undefined;
151+
}
152+
153+
/**
154+
* Checks if value is equal to the defined default
155+
* Works in array types
156+
* @param {string} value - Value to check for
157+
* @param {*} definedDefault - The defined default value of the schema
158+
* @returns {Boolean} wheter value is equal to the defined or not
159+
*/
160+
function checkValueEqualsDefault(value, definedDefault) {
161+
return value === definedDefault;
162+
}
163+
164+
/**
165+
* Checks if value is the representation of its type like:
166+
* "<integer>"
167+
* Works in array types
168+
* @param {string} value - Value to check for
169+
* @param {*} schema - The schema portion used in validation
170+
* @returns {Boolean} the value is the representation of its type
171+
*/
172+
function isTypeValue(value, schema) {
173+
if (schema.hasOwnProperty('type') && schema.hasOwnProperty('default')) {
174+
const isDefault = checkValueEqualsDefault(value, schema.default);
175+
if (isDefault) {
176+
return true;
177+
}
178+
}
179+
if (schema.hasOwnProperty('type') && !schema.hasOwnProperty('format')) {
180+
return checkValueOnlyTypes(value, schema.type);
181+
}
182+
if (schema.hasOwnProperty('type') && schema.hasOwnProperty('format')) {
183+
return checkValueTypesAndFormat(value, schema.type, schema.format);
184+
}
185+
}
186+
187+
/**
188+
* Checks if value is correcta according to schema
189+
* If the value should be numeric, it tries to convert and then validate
190+
* also validates if the value is a correct representation in the form of
191+
* <long> or <integer> etc for integers format 32 or format 64
192+
* @param {string} value - Value to check for
193+
* @param {*} schema - The schema portion used in validation
194+
* @returns {Boolean} the value is the representation of its type
195+
*/
196+
function checkIsCorrectType(value, schema) {
197+
if (schema.hasOwnProperty('type') &&
198+
typeof schemaTypeToJsValidator[schema.type] === 'function') {
199+
const isCorrectType = schemaTypeToJsValidator[schema.type](value);
200+
if (isCorrectType) {
201+
return true;
202+
}
203+
}
204+
return isTypeValue(value, schema);
205+
}
6206

7207
module.exports = {
8208

@@ -54,15 +254,21 @@ module.exports = {
54254
isANumber = (value) => {
55255
return !isNaN(value);
56256
},
57-
formattedElements = splittedDataPath.map((element) => {
257+
formattedElements = splittedDataPath.map((element, index) => {
58258
if (element !== '' && isANumber(element)) {
59259
return `[${element}]`;
60260
}
61-
return element;
261+
if (element === '' || element[0] === '.') {
262+
return element;
263+
}
264+
if (index === 0 && !initialDotIfExist) {
265+
return `${element}`;
266+
}
267+
return `.${element}`;
62268
}),
63269
formattedDataPath = formattedElements.join('');
64270

65-
return `${initialDotIfExist}${formattedDataPath}`;
271+
return `${formattedDataPath}`;
66272
},
67273

68274
handleExclusiveMaximum: function(schema, max) {
@@ -101,5 +307,43 @@ module.exports = {
101307
}
102308
}
103309
return min;
310+
},
311+
312+
/**
313+
* Removes initial "#/" from a schema path and the last "/type" segment
314+
* @param {string} schemaPath - The OAS 3.x specification specified in either YAML or JSON
315+
* @returns {string} - The schemaPath with initial #/ and last "/type" removed
316+
*/
317+
formatSchemaPathFromAJVErrorToConvertToDataPath: function (schemaPath) {
318+
return removeWordFromLastPosition(removeWordFromLastPosition(removeSharpAndSlashFromFirstPosition(schemaPath),
319+
'type'), 'format');
320+
},
321+
322+
typesMap,
323+
324+
/**
325+
* Checks if value is the representation of its type like:
326+
* "<integer>"
327+
* Works in array types
328+
* @param {string} value - Value to check for
329+
* @param {*} schema - The schema portion used in validation
330+
* @returns {Boolean} the value is the representation of its type
331+
*/
332+
isTypeValue,
333+
334+
/**
335+
* Checks if value is correcta according to schema
336+
* If the value should be numeric, it tries to convert and then validate
337+
* also validates if the value is a correct representation in the form of
338+
* <long> or <integer> etc for integers format 32 or format 64
339+
* @param {string} value - Value to check for
340+
* @param {*} schema - The schema portion used in validation
341+
* @returns {Boolean} the value is the representation of its type
342+
*/
343+
checkIsCorrectType,
344+
345+
346+
isKnownType: function(schema) {
347+
return typeof schemaTypeToJsValidator[schema.type] === 'function';
104348
}
105349
};

0 commit comments

Comments
 (0)