@@ -64,7 +64,6 @@ const _ = require('lodash'),
64
64
'accept' ,
65
65
'authorization'
66
66
] ,
67
- DEFAULT_SCHEMA_UTILS = require ( '../lib/30XUtils/schemaUtils30X' ) ,
68
67
69
68
OAS_NOT_SUPPORTED = '<Error: Not supported in OAS>' ,
70
69
@@ -133,26 +132,30 @@ function shouldAddDeprecatedOperation (operation, options) {
133
132
* removes things that might make schemaFaker crash
134
133
* @param {Object } context - Required context from related SchemaPack function
135
134
* @param {* } oldSchema the schema to fake
136
- * @param {string } resolveTo The desired JSON-generation mechanism (schema: prefer using the JSONschema to
137
135
* generate a fake object, example: use specified examples as-is). Default: schema
138
136
* @param {* } resolveFor - resolve refs for flow validation/conversion (value to be one of VALIDATION/CONVERSION)
139
137
* @param {string } parameterSourceOption Specifies whether the schema being faked is from a request or response.
140
138
* @param {* } components list of predefined components (with schemas)
141
139
* @param {string } schemaFormat default or xml
142
140
* @param {object } schemaCache - object storing schemaFaker and schemaResolution caches
143
- * @param {object } options - a standard list of options that's globally passed around. Check options.js for more.
144
141
* @returns {object } fakedObject
145
142
*/
146
- function safeSchemaFaker ( context , oldSchema , resolveTo , resolveFor , parameterSourceOption , components ,
147
- schemaFormat , schemaCache , options ) {
148
- var prop , key , resolvedSchema , fakedSchema ,
149
- schemaFakerCache = _ . get ( schemaCache , 'schemaFakerCache' , { } ) ;
150
- let concreteUtils = components && components . hasOwnProperty ( 'concreteUtils' ) ?
151
- components . concreteUtils :
152
- DEFAULT_SCHEMA_UTILS ;
153
- const indentCharacter = options . indentCharacter ;
143
+ function safeSchemaFaker ( context , oldSchema , resolveFor , parameterSourceOption , components ,
144
+ schemaFormat , schemaCache ) {
145
+ let prop , key , resolvedSchema , fakedSchema ,
146
+ schemaFakerCache = _ . get ( schemaCache , 'schemaFakerCache' , { } ) ,
147
+ concreteUtils = context . concreteUtils ;
154
148
155
- resolvedSchema = resolveSchema ( context , oldSchema , 0 , PROCESSING_TYPE . VALIDATION ) ;
149
+ const options = context . computedOptions ,
150
+ resolveTo = _ . get ( options , 'parametersResolution' , 'example' ) ,
151
+ indentCharacter = options . indentCharacter ;
152
+
153
+ /**
154
+ * Schema is cloned here as resolveSchema() when called for CONVERSION use cases, will mutate schema in certain way.
155
+ * i.e. For array it'll add maxItems = 2. This should be avoided as we'll again be needing non-mutated schema
156
+ * in further VALIDATION use cases as needed.
157
+ */
158
+ resolvedSchema = resolveSchema ( context , _ . cloneDeep ( oldSchema ) , 0 , _ . toLower ( PROCESSING_TYPE . CONVERSION ) ) ;
156
159
157
160
resolvedSchema = concreteUtils . fixExamplesByVersion ( resolvedSchema ) ;
158
161
key = JSON . stringify ( resolvedSchema ) ;
@@ -173,7 +176,6 @@ function safeSchemaFaker (context, oldSchema, resolveTo, resolveFor, parameterSo
173
176
174
177
if ( resolveFor === PROCESSING_TYPE . VALIDATION ) {
175
178
schemaFaker . option ( {
176
- useDefaultValue : false ,
177
179
avoidExampleItemsLength : false
178
180
} ) ;
179
181
}
@@ -1426,8 +1428,8 @@ function checkValueAgainstSchema (context, property, jsonPathPrefix, txnParamNam
1426
1428
mismatchObj . suggestedFix = {
1427
1429
key : txnParamName ,
1428
1430
actualValue : valueToUse ,
1429
- suggestedValue : safeSchemaFaker ( context , openApiSchemaObj || { } , 'example' , PROCESSING_TYPE . VALIDATION ,
1430
- parameterSourceOption , components , SCHEMA_FORMATS . DEFAULT , schemaCache , options . includeDeprecated )
1431
+ suggestedValue : safeSchemaFaker ( context , schema || { } , PROCESSING_TYPE . VALIDATION ,
1432
+ parameterSourceOption , components , SCHEMA_FORMATS . DEFAULT , schemaCache )
1431
1433
} ;
1432
1434
}
1433
1435
@@ -1442,8 +1444,8 @@ function checkValueAgainstSchema (context, property, jsonPathPrefix, txnParamNam
1442
1444
if ( ! _ . isEmpty ( filteredValidationError ) ) {
1443
1445
let mismatchObj ,
1444
1446
suggestedValue ,
1445
- fakedValue = safeSchemaFaker ( context , openApiSchemaObj || { } , 'example' , PROCESSING_TYPE . VALIDATION ,
1446
- parameterSourceOption , components , SCHEMA_FORMATS . DEFAULT , schemaCache , options ) ;
1447
+ fakedValue = safeSchemaFaker ( context , schema || { } , PROCESSING_TYPE . VALIDATION ,
1448
+ parameterSourceOption , components , SCHEMA_FORMATS . DEFAULT , schemaCache ) ;
1447
1449
1448
1450
// Show detailed validation mismatches for only request/response body
1449
1451
if ( options . detailedBlobValidation && needJsonMatching ) {
@@ -1698,13 +1700,15 @@ function checkPathVariables (context, matchedPathData, transactionPathPrefix, sc
1698
1700
} ;
1699
1701
1700
1702
if ( options . suggestAvailableFixes ) {
1703
+ const resolvedSchema = resolveSchema ( context , pathVar . schema , 0 , PROCESSING_TYPE . VALIDATION ) ;
1704
+
1701
1705
mismatchObj . suggestedFix = {
1702
1706
key : pathVar . name ,
1703
1707
actualValue,
1704
1708
suggestedValue : {
1705
1709
key : pathVar . name ,
1706
- value : safeSchemaFaker ( context , pathVar . schema || { } , 'example' , PROCESSING_TYPE . VALIDATION ,
1707
- PARAMETER_SOURCE . REQUEST , components , SCHEMA_FORMATS . DEFAULT , schemaCache , options ) ,
1710
+ value : safeSchemaFaker ( context , resolvedSchema || { } , PROCESSING_TYPE . VALIDATION ,
1711
+ PARAMETER_SOURCE . REQUEST , components , SCHEMA_FORMATS . DEFAULT , schemaCache ) ,
1708
1712
description : getParameterDescription ( pathVar )
1709
1713
}
1710
1714
} ;
@@ -1836,13 +1840,15 @@ function checkQueryParams (context, queryParams, transactionPathPrefix, schemaPa
1836
1840
} ;
1837
1841
1838
1842
if ( options . suggestAvailableFixes ) {
1843
+ const resolvedSchema = resolveSchema ( context , qp . schema , 0 , PROCESSING_TYPE . VALIDATION ) ;
1844
+
1839
1845
mismatchObj . suggestedFix = {
1840
1846
key : qp . name ,
1841
1847
actualValue : null ,
1842
1848
suggestedValue : {
1843
1849
key : qp . name ,
1844
- value : safeSchemaFaker ( context , qp . schema || { } , 'example' , PROCESSING_TYPE . VALIDATION ,
1845
- PARAMETER_SOURCE . REQUEST , components , SCHEMA_FORMATS . DEFAULT , schemaCache , options ) ,
1850
+ value : safeSchemaFaker ( context , resolvedSchema || { } , PROCESSING_TYPE . VALIDATION ,
1851
+ PARAMETER_SOURCE . REQUEST , components , SCHEMA_FORMATS . DEFAULT , schemaCache ) ,
1846
1852
description : getParameterDescription ( qp )
1847
1853
}
1848
1854
} ;
@@ -1958,13 +1964,15 @@ function checkRequestHeaders (context, headers, transactionPathPrefix, schemaPat
1958
1964
} ;
1959
1965
1960
1966
if ( options . suggestAvailableFixes ) {
1967
+ const resolvedSchema = resolveSchema ( context , header . schema , 0 , PROCESSING_TYPE . VALIDATION ) ;
1968
+
1961
1969
mismatchObj . suggestedFix = {
1962
1970
key : header . name ,
1963
1971
actualValue : null ,
1964
1972
suggestedValue : {
1965
1973
key : header . name ,
1966
- value : safeSchemaFaker ( context , header . schema || { } , 'example' , PROCESSING_TYPE . VALIDATION ,
1967
- PARAMETER_SOURCE . REQUEST , components , SCHEMA_FORMATS . DEFAULT , schemaCache , options ) ,
1974
+ value : safeSchemaFaker ( context , resolvedSchema || { } , PROCESSING_TYPE . VALIDATION ,
1975
+ PARAMETER_SOURCE . REQUEST , components , SCHEMA_FORMATS . DEFAULT , schemaCache ) ,
1968
1976
description : getParameterDescription ( header )
1969
1977
}
1970
1978
} ;
@@ -2076,13 +2084,15 @@ function checkResponseHeaders (context, schemaResponse, headers, transactionPath
2076
2084
} ;
2077
2085
2078
2086
if ( options . suggestAvailableFixes ) {
2087
+ const resolvedSchema = resolveSchema ( context , header . schema , 0 , PROCESSING_TYPE . VALIDATION ) ;
2088
+
2079
2089
mismatchObj . suggestedFix = {
2080
2090
key : header . name ,
2081
2091
actualValue : null ,
2082
2092
suggestedValue : {
2083
2093
key : header . name ,
2084
- value : safeSchemaFaker ( context , header . schema || { } , 'example' , PROCESSING_TYPE . VALIDATION ,
2085
- PARAMETER_SOURCE . REQUEST , components , SCHEMA_FORMATS . DEFAULT , schemaCache , options ) ,
2094
+ value : safeSchemaFaker ( context , resolvedSchema || { } , PROCESSING_TYPE . VALIDATION ,
2095
+ PARAMETER_SOURCE . REQUEST , components , SCHEMA_FORMATS . DEFAULT , schemaCache ) ,
2086
2096
description : getParameterDescription ( header )
2087
2097
}
2088
2098
} ;
@@ -2246,13 +2256,15 @@ function checkRequestBody (context, requestBody, transactionPathPrefix, schemaPa
2246
2256
} ;
2247
2257
2248
2258
if ( options . suggestAvailableFixes ) {
2259
+ const resolvedSchema = resolveSchema ( context , uParam . schema , 0 , PROCESSING_TYPE . VALIDATION ) ;
2260
+
2249
2261
mismatchObj . suggestedFix = {
2250
2262
key : uParam . name ,
2251
2263
actualValue : null ,
2252
2264
suggestedValue : {
2253
2265
key : uParam . name ,
2254
- value : safeSchemaFaker ( context , uParam . schema || { } , 'example' , PROCESSING_TYPE . VALIDATION ,
2255
- PARAMETER_SOURCE . REQUEST , components , SCHEMA_FORMATS . DEFAULT , schemaCache , options ) ,
2266
+ value : safeSchemaFaker ( context , resolvedSchema || { } , PROCESSING_TYPE . VALIDATION ,
2267
+ PARAMETER_SOURCE . REQUEST , components , SCHEMA_FORMATS . DEFAULT , schemaCache ) ,
2256
2268
description : getParameterDescription ( uParam )
2257
2269
}
2258
2270
} ;
0 commit comments