/
traverse.js
177 lines (152 loc) · 5.1 KB
/
traverse.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
import { _samplers } from './openapi-sampler';
import { allOfSample } from './allOf';
import { inferType } from './infer';
import { getResultForCircular, mergeDeep, popSchemaStack } from './utils';
import JsonPointer from 'json-pointer';
let $refCache = {};
// for circular JS references we use additional array and not object as we need to compare entire schemas and not strings
let seenSchemasStack = [];
export function clearCache() {
$refCache = {};
seenSchemasStack = [];
}
function inferExample(schema) {
let example;
if (schema.const !== undefined) {
example = schema.const;
} else if (schema.examples !== undefined && schema.examples.length) {
example = schema.examples[0];
} else if (schema.enum !== undefined && schema.enum.length) {
example = schema.enum[0];
} else if (schema.default !== undefined) {
example = schema.default;
}
return example;
}
function tryInferExample(schema) {
const example = inferExample(schema);
// case when we don't infer example from schema but take from `const`, `examples`, `default` or `enum` keywords
if (example !== undefined) {
return {
value: example,
readOnly: schema.readOnly,
writeOnly: schema.writeOnly,
type: null,
};
}
return;
}
export function traverse(schema, options, spec, context) {
// checking circular JS references by checking context
// because context is passed only when traversing through nested objects happens
if (context) {
if (seenSchemasStack.includes(schema)) return getResultForCircular(inferType(schema));
seenSchemasStack.push(schema);
}
if (context && context.depth > options.maxSampleDepth) {
popSchemaStack(seenSchemasStack, context);
return getResultForCircular(inferType(schema));
}
if (schema.$ref) {
if (!spec) {
throw new Error('Your schema contains $ref. You must provide full specification in the third parameter.');
}
let ref = decodeURIComponent(schema.$ref);
if (ref.startsWith('#')) {
ref = ref.substring(1);
}
const referenced = JsonPointer.get(spec, ref);
let result;
if ($refCache[ref] !== true) {
$refCache[ref] = true;
result = traverse(referenced, options, spec, context);
$refCache[ref] = false;
} else {
const referencedType = inferType(referenced);
result = getResultForCircular(referencedType);
}
popSchemaStack(seenSchemasStack, context);
return result;
}
if (schema.example !== undefined) {
popSchemaStack(seenSchemasStack, context);
return {
value: schema.example,
readOnly: schema.readOnly,
writeOnly: schema.writeOnly,
type: schema.type,
};
}
if (schema.allOf !== undefined) {
popSchemaStack(seenSchemasStack, context);
return tryInferExample(schema) || allOfSample(
{ ...schema, allOf: undefined },
schema.allOf,
options,
spec,
context,
);
}
if (schema.oneOf && schema.oneOf.length) {
if (schema.anyOf) {
if (!options.quiet) console.warn('oneOf and anyOf are not supported on the same level. Skipping anyOf');
}
popSchemaStack(seenSchemasStack, context);
// Make sure to pass down readOnly and writeOnly annotations from the parent
const firstOneOf = Object.assign({
readOnly: schema.readOnly,
writeOnly: schema.writeOnly
}, schema.oneOf[0]);
return traverseOneOrAnyOf(schema, firstOneOf)
}
if (schema.anyOf && schema.anyOf.length) {
popSchemaStack(seenSchemasStack, context);
// Make sure to pass down readOnly and writeOnly annotations from the parent
const firstAnyOf = Object.assign({
readOnly: schema.readOnly,
writeOnly: schema.writeOnly
}, schema.anyOf[0]);
return traverseOneOrAnyOf(schema, firstAnyOf)
}
if (schema.if && schema.then) {
popSchemaStack(seenSchemasStack, context);
const { if: ifSchema, then, ...rest } = schema;
return traverse(mergeDeep(rest, ifSchema, then), options, spec, context);
}
let example = inferExample(schema);
let type = null;
if (example === undefined) {
example = null;
type = schema.type;
if (Array.isArray(type) && schema.type.length > 0) {
type = schema.type[0];
}
if (!type) {
type = inferType(schema);
}
let sampler = _samplers[type];
if (sampler) {
example = sampler(schema, options, spec, context);
}
}
popSchemaStack(seenSchemasStack, context);
return {
value: example,
readOnly: schema.readOnly,
writeOnly: schema.writeOnly,
type: type
};
function traverseOneOrAnyOf(schema, selectedSubSchema) {
const inferred = tryInferExample(schema);
if (inferred !== undefined) {
return inferred;
}
const localExample = traverse({...schema, oneOf: undefined, anyOf: undefined }, options, spec, context);
const subSchemaExample = traverse(selectedSubSchema, options, spec, context);
if (typeof localExample.value === 'object' && typeof subSchemaExample.value === 'object') {
const mergedExample = mergeDeep(localExample.value, subSchemaExample.value);
return {...subSchemaExample, value: mergedExample };
}
return subSchemaExample;
}
}