-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathderef.js
460 lines (420 loc) · 18.2 KB
/
deref.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
const _ = require('lodash'),
mergeAllOf = require('json-schema-merge-allof'),
{ typesMap } = require('./common/schemaUtilsCommon'),
{ isLocalRef } = require('./jsonPointer'),
PARAMETER_SOURCE = {
REQUEST: 'REQUEST',
RESPONSE: 'RESPONSE'
},
SCHEMA_TYPES = {
array: 'array',
boolean: 'boolean',
integer: 'integer',
number: 'number',
object: 'object',
string: 'string'
},
// All formats supported by both ajv and json-schema-faker
SUPPORTED_FORMATS = [
'date', 'time', 'date-time',
'uri', 'uri-reference', 'uri-template',
'email',
'hostname',
'ipv4', 'ipv6',
'regex',
'uuid',
'json-pointer',
'int64',
'float',
'double'
],
DEFAULT_SCHEMA_UTILS = require('./30XUtils/schemaUtils30X'),
traverseUtility = require('traverse'),
PROPERTIES_TO_ASSIGN_ON_CASCADE = ['type', 'nullable'],
CIRCULAR_REF_KEY = '$circularRef',
{ isCircularReference } = require('./utils');
/**
* @param {*} currentNode - the object from which you're trying to find references
* @param {*} seenRef References that are repeated. Used to identify circular references.
* @returns {boolean} - Whether the object has circular references
*/
function hasReference(currentNode, seenRef) {
let hasRef = false;
traverseUtility(currentNode).forEach(function (property) {
if (property) {
let hasReferenceTypeKey;
hasReferenceTypeKey = Object.keys(property)
.find(
(key) => {
return key === '$ref';
}
);
if (hasReferenceTypeKey && seenRef[property.$ref]) {
hasRef = true;
}
}
});
return hasRef;
}
module.exports = {
/**
* @param {*} rootObject - the object from which you're trying to read a property
* @param {*} pathArray - each element in this array a property of the previous object
* @param {*} defValue - what to return if the required path is not found
* @returns {*} - required property value
* @description - this is similar to _.get(rootObject, pathArray.join('.')), but also works for cases where
* there's a . in the property name
*/
_getEscaped: function (rootObject, pathArray, defValue) {
if (!(pathArray instanceof Array)) {
return null;
}
if (!rootObject) {
return defValue;
}
if (_.isEmpty(pathArray)) {
return rootObject;
}
return this._getEscaped(rootObject[pathArray.shift()], pathArray, defValue);
},
/**
* Creates a schema that's a union of all input schemas (only type: object is supported)
*
* @param {array} schemaArr - array of schemas, all of which must be valid in the returned object
* @param {string} parameterSourceOption tells that the schema object is of request or response
* @param {*} components components in openapi spec.
* @param {object} schemaResolutionCache stores already resolved references
* @param {*} resolveFor - resolve refs for validation/conversion (value to be one of VALIDATION/CONVERSION)
* @param {string} resolveTo The desired JSON-generation mechanism (schema: prefer using the JSONschema to
generate a fake object, example: use specified examples as-is). Default: schema
* @param {*} stack counter which keeps a tab on nested schemas
* @param {*} seenRef References that are repeated. Used to identify circular references.
* @param {*} stackLimit Depth to which the schema should be resolved.
* @returns {*} schema - schema that adheres to all individual schemas in schemaArr
*/
resolveAllOf: function (schemaArr, parameterSourceOption, components, schemaResolutionCache,
resolveFor = 'CONVERSION', resolveTo = 'schema', stack = 0, seenRef, stackLimit) {
if (!(schemaArr instanceof Array)) {
return null;
}
if (schemaArr.length === 1) {
// for just one entry in allOf, don't need to enforce type: object restriction
return this.resolveRefs(schemaArr[0], parameterSourceOption, components, schemaResolutionCache, resolveFor,
resolveTo, stack, seenRef, stackLimit);
}
try {
return mergeAllOf({
allOf: schemaArr.map((schema) => {
return this.resolveRefs(schema, parameterSourceOption, components, schemaResolutionCache, resolveFor,
resolveTo, stack, seenRef, stackLimit, true);
})
}, {
resolvers: {
// for keywords in OpenAPI schema that are not standard defined JSON schema keywords, use default resolver
defaultResolver: (compacted) => { return compacted[0]; }
}
});
}
catch (e) {
console.warn('Error while resolving allOf schema: ', e);
return { value: '<Error: Could not resolve allOf schema' };
}
},
/**
* Resolves references to components for a given schema.
* @param {*} schema (openapi) to resolve references.
* @param {string} parameterSourceOption tells that the schema object is of request or response
* @param {*} components components in openapi spec.
* @param {object} schemaResolutionCache stores already resolved references - more structure detail below
* {'schema reference key': {
* maxStack {Integer} : Defined as how deep of nesting level we reached while resolving schema that's being cached
* resLevel {Integer} : Defined as nesting level at which schema that's being cached was resolved
* schema {Object} : resolved schema that will be cached
* }}
* @param {*} resolveFor - resolve refs for validation/conversion (value to be one of VALIDATION/CONVERSION)
* @param {string} resolveTo The desired JSON-generation mechanism (schema: prefer using the JSONschema to
generate a fake object, example: use specified examples as-is). Default: schema
* @param {*} stack counter which keeps a tab on nested schemas
* @param {*} seenRef - References that are repeated. Used to identify circular references.
* @param {*} stackLimit Depth to which the schema should be resolved.
* @returns {*} schema satisfying JSON-schema-faker.
*/
resolveRefs: function (schema, parameterSourceOption, components, schemaResolutionCache,
resolveFor = 'CONVERSION', resolveTo = 'schema', stack = 0, seenRef = {}, stackLimit = 8, isAllOf = false) {
var resolvedSchema, prop, splitRef,
ERR_TOO_MANY_LEVELS = '<Error: Too many levels of nesting to fake this schema>';
let concreteUtils = components && components.hasOwnProperty('concreteUtils') ?
components.concreteUtils :
DEFAULT_SCHEMA_UTILS;
stack++;
schemaResolutionCache = schemaResolutionCache || {};
if (stack > stackLimit) {
return { value: ERR_TOO_MANY_LEVELS };
}
if (!schema) {
return { value: '<Error: Schema not found>' };
}
if (schema.anyOf) {
if (resolveFor === 'CONVERSION') {
return this.resolveRefs(schema.anyOf[0], parameterSourceOption, components, schemaResolutionCache, resolveFor,
resolveTo, stack, _.cloneDeep(seenRef), stackLimit);
}
return { anyOf: _.map(schema.anyOf, (schemaElement) => {
PROPERTIES_TO_ASSIGN_ON_CASCADE.forEach((prop) => {
if (_.isNil(schemaElement[prop]) && !_.isNil(schema[prop])) {
schemaElement[prop] = schema[prop];
}
});
return this.resolveRefs(schemaElement, parameterSourceOption, components, schemaResolutionCache, resolveFor,
resolveTo, stack, _.cloneDeep(seenRef), stackLimit);
}) };
}
if (schema.oneOf) {
if (resolveFor === 'CONVERSION') {
return this.resolveRefs(schema.oneOf[0], parameterSourceOption, components, schemaResolutionCache, resolveFor,
resolveTo, stack, _.cloneDeep(seenRef), stackLimit);
}
return { oneOf: _.map(schema.oneOf, (schemaElement) => {
PROPERTIES_TO_ASSIGN_ON_CASCADE.forEach((prop) => {
if (_.isNil(schemaElement[prop]) && !_.isNil(schema[prop])) {
schemaElement[prop] = schema[prop];
}
});
return this.resolveRefs(schemaElement, parameterSourceOption, components, schemaResolutionCache,
resolveFor, resolveTo, stack, _.cloneDeep(seenRef), stackLimit);
}) };
}
if (schema.allOf) {
return this.resolveAllOf(schema.allOf, parameterSourceOption, components, schemaResolutionCache, resolveFor,
resolveTo, stack, _.cloneDeep(seenRef), stackLimit);
}
if (schema.hasOwnProperty(CIRCULAR_REF_KEY)) {
return schema;
}
if (schema.$ref && _.isFunction(schema.$ref.split)) {
let refKey = schema.$ref,
outerProperties = concreteUtils.getOuterPropsIfIsSupported(schema);
// points to an existing location
// .split will return [#, components, schemas, schemaName]
splitRef = refKey.split('/');
if (splitRef.length < 4) {
// not throwing an error. We didn't find the reference - generate a dummy value
return { value: 'reference ' + schema.$ref + ' not found in the OpenAPI spec' };
}
// something like #/components/schemas/PaginationEnvelope/properties/page
// will be resolved - we don't care about anything after the components part
// splitRef.slice(1) will return ['components', 'schemas', 'PaginationEnvelope', 'properties', 'page']
// not using _.get here because that fails if there's a . in the property name (Pagination.Envelope, for example)
splitRef = splitRef.slice(1).map((elem) => {
// https://swagger.io/docs/specification/using-ref#escape
// since / is the default delimiter, slashes are escaped with ~1
return decodeURIComponent(
elem
.replace(/~1/g, '/')
.replace(/~0/g, '~')
);
});
resolvedSchema = this._getEscaped(components, splitRef);
// if this reference is seen before, ignore and move on.
if (seenRef[refKey] && hasReference(resolvedSchema, seenRef)) {
return { value: '<Circular reference to ' + refKey + ' detected>' };
}
// add to seen array if not encountered before.
seenRef[refKey] = stack;
if (outerProperties) {
resolvedSchema = concreteUtils.addOuterPropsToRefSchemaIfIsSupported(resolvedSchema, outerProperties);
}
if (resolvedSchema) {
let refResolvedSchema = this.resolveRefs(resolvedSchema, parameterSourceOption,
components, schemaResolutionCache, resolveFor, resolveTo, stack, _.cloneDeep(seenRef), stackLimit);
return refResolvedSchema;
}
return { value: 'reference ' + schema.$ref + ' not found in the OpenAPI spec' };
}
if (
concreteUtils.compareTypes(schema.type, SCHEMA_TYPES.object) ||
schema.hasOwnProperty('properties') ||
(schema.hasOwnProperty('additionalProperties') && !schema.hasOwnProperty('type'))
) {
// go through all props
schema.type = SCHEMA_TYPES.object;
if (_.has(schema, 'properties') || _.has(schema, 'additionalProperties')) {
let tempSchema = _.omit(schema, ['properties', 'additionalProperties']);
// shallow cloning schema object except properties object
if (_.has(schema, 'additionalProperties')) {
if (_.isBoolean(schema.additionalProperties)) {
tempSchema.additionalProperties = schema.additionalProperties;
}
else {
tempSchema.additionalProperties = this.resolveRefs(schema.additionalProperties, parameterSourceOption,
components, schemaResolutionCache, resolveFor, resolveTo, stack, _.cloneDeep(seenRef), stackLimit);
}
}
!_.isEmpty(schema.properties) && (tempSchema.properties = {});
for (prop in schema.properties) {
if (schema.properties.hasOwnProperty(prop)) {
/* eslint-disable max-depth */
// handling OAS readOnly and writeOnly properties in schema
// Related Doc - https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#schemaObject
let property = schema.properties[prop];
if (!property) {
continue;
}
if (property.readOnly && parameterSourceOption === PARAMETER_SOURCE.REQUEST) {
// remove property from required as it'll not be present in resolved schema
if (_.includes(tempSchema.required, prop)) {
_.remove(tempSchema.required, _.matches(prop));
}
continue;
}
else if (property.writeOnly && parameterSourceOption === PARAMETER_SOURCE.RESPONSE) {
// remove property from required as it'll not be present in resolved schema
if (_.includes(tempSchema.required, prop)) {
_.remove(tempSchema.required, _.matches(prop));
}
continue;
}
/* eslint-enable */
tempSchema.properties[prop] = _.isEmpty(property) ?
{} :
this.resolveRefs(property, parameterSourceOption, components,
schemaResolutionCache, resolveFor, resolveTo, stack, _.cloneDeep(seenRef), stackLimit);
}
}
return tempSchema;
}
// Override deefault value to appropriate type representation for parameter resolution to schema
if (resolveFor === 'CONVERSION' && resolveTo === 'schema') {
schema.default = typesMap.object;
}
}
else if (concreteUtils.compareTypes(schema.type, SCHEMA_TYPES.array) && schema.items) {
/*
For VALIDATION - keep minItems and maxItems properties defined by user in schema as is
FOR CONVERSION -
Json schema faker fakes exactly maxItems # of elements in array
Hence keeping maxItems as minimum and valid as possible for schema faking (to lessen faked items)
We have enforced limit to maxItems as 100, set by Json schema faker option
*/
if (resolveFor === 'CONVERSION') {
// Override minItems to default (2) if no minItems present
if (!_.has(schema, 'minItems') && _.has(schema, 'maxItems') && schema.maxItems >= 2) {
schema.minItems = 2;
}
// Override maxItems to minItems if minItems is available
if (_.has(schema, 'minItems') && schema.minItems > 0) {
schema.maxItems = schema.minItems;
}
// If no maxItems is defined than override with default (2)
!_.has(schema, 'maxItems') && (schema.maxItems = 2);
}
// have to create a shallow clone of schema object,
// so that the original schema.items object will not change
// without this, schemas with circular references aren't faked correctly
let tempSchema = _.omit(schema, ['items', 'additionalProperties']);
tempSchema.items = this.resolveRefs(schema.items, parameterSourceOption,
components, schemaResolutionCache, resolveFor, resolveTo, stack, _.cloneDeep(seenRef), stackLimit);
return tempSchema;
}
else if (!schema.hasOwnProperty('default')) {
if (schema.hasOwnProperty('type')) {
// Override default value to schema for CONVERSION only for parmeter resolution set to schema
if (resolveFor === 'CONVERSION' && resolveTo === 'schema') {
if (!schema.hasOwnProperty('format')) {
schema.default = '<' + schema.type + '>';
}
else if (typesMap.hasOwnProperty(schema.type)) {
schema.default = typesMap[schema.type][schema.format];
// in case the format is a custom format (email, hostname etc.)
// https://swagger.io/docs/specification/data-models/data-types/#string
if (!schema.default && schema.format) {
schema.default = '<' + schema.format + '>';
}
}
else {
schema.default = '<' + schema.type + (schema.format ? ('-' + schema.format) : '') + '>';
}
}
}
else if (schema.enum && schema.enum.length > 0) {
return {
type: (typeof (schema.enum[0])),
value: schema.enum[0]
};
}
else if (isAllOf) {
return schema;
}
else {
return {
type: SCHEMA_TYPES.object
};
}
if (!schema.type) {
schema.type = SCHEMA_TYPES.string;
}
// Discard format if not supported by both json-schema-faker and ajv or pattern is also defined
if (!_.includes(SUPPORTED_FORMATS, schema.format) || (schema.pattern && schema.format)) {
return _.omit(schema, 'format');
}
}
return schema;
},
/**
* Take a $ref and a spec and return the referenced value
* @param {object} spec the parsed spec object
* @param {string} reference the $ref value to dereference
* @returns {object} the dereferenced $ref value
*/
dereferenceElement: function (spec, reference) {
let splitRef = reference.split('/'),
resolvedContent;
splitRef = splitRef.slice(1).map((elem) => {
// https://swagger.io/docs/specification/using-ref#escape
// since / is the default delimiter, slashes are escaped with ~1
return decodeURIComponent(
elem
.replace(/~1/g, '/')
.replace(/~0/g, '~')
);
});
resolvedContent = this._getEscaped(spec, splitRef);
return resolvedContent;
},
/**
* Dereferences the values referenced from out of components/schemas element
* @param {object} spec The parsed specification
* @param {object} constraintRegexp The regexp to match against the $ref element's value
* @returns {object} The specification with the values referenced from other place than components/schemas
*/
dereferenceByConstraint: function(spec, constraintRegexp) {
let dereferencedSpec = _.cloneDeep(spec),
that = this,
seenContents = {};
traverseUtility(dereferencedSpec).forEach(function (property) {
if (
typeof property === 'object' &&
_.size(property) === 1 &&
property.hasOwnProperty('$ref') &&
isLocalRef(property, '$ref') &&
(
property.$ref.match(constraintRegexp)
)
) {
const contentFromRef = seenContents[property.$ref];
if (isCircularReference(this, contentFromRef)) {
this.update({ [CIRCULAR_REF_KEY]: '<Circular reference to ' + property.$ref + ' detected>' });
}
else {
const dereferencedContent = that.dereferenceElement(
dereferencedSpec,
property.$ref
);
seenContents[property.$ref] = dereferencedContent;
this.update(dereferencedContent);
}
}
});
return dereferencedSpec;
}
};