openapi-explorer 2.1.656 → 2.1.658
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser/openapi-explorer.min.js +2 -2
- package/dist/es/components/api-request.js +58 -140
- package/dist/es/components/api-response.js +9 -34
- package/dist/es/components/json-tree.js +4 -18
- package/dist/es/components/request-form-table.js +13 -36
- package/dist/es/components/schema-table.js +28 -42
- package/dist/es/components/schema-tree.js +31 -61
- package/dist/es/components/syntax-highlighter.js +7 -26
- package/dist/es/components/tag-input.js +2 -14
- package/dist/es/openapi-explorer-oauth-handler.js +0 -2
- package/dist/es/openapi-explorer.js +62 -174
- package/dist/es/react.js +4 -4
- package/dist/es/styles/input-styles.js +1 -1
- package/dist/es/styles/schema-styles.js +1 -1
- package/dist/es/templates/advance-search-template.js +1 -5
- package/dist/es/templates/callback-template.js +2 -2
- package/dist/es/templates/code-samples-template.js +1 -3
- package/dist/es/templates/components-template.js +41 -4
- package/dist/es/templates/endpoint-template.js +6 -17
- package/dist/es/templates/expanded-endpoint-template.js +4 -7
- package/dist/es/templates/focused-endpoint-template.js +0 -10
- package/dist/es/templates/mainBodyTemplate.js +3 -2
- package/dist/es/templates/navbar-template.js +9 -12
- package/dist/es/templates/overview-template.js +1 -1
- package/dist/es/templates/security-scheme-template.js +12 -73
- package/dist/es/templates/server-template.js +1 -8
- package/dist/es/utils/color-utils.js +2 -21
- package/dist/es/utils/common-utils.js +3 -20
- package/dist/es/utils/schema-utils.js +35 -132
- package/dist/es/utils/spec-parser.js +35 -120
- package/dist/es/utils/theme.js +3 -6
- package/dist/es/utils/xml/xml.js +1 -40
- package/dist/lib/components/api-request.js +58 -157
- package/dist/lib/components/api-response.js +9 -54
- package/dist/lib/components/json-tree.js +4 -27
- package/dist/lib/components/request-form-table.js +14 -42
- package/dist/lib/components/schema-table.js +28 -52
- package/dist/lib/components/schema-tree.js +31 -72
- package/dist/lib/components/syntax-highlighter.js +6 -49
- package/dist/lib/components/tag-input.js +2 -18
- package/dist/lib/languages/en.js +2 -3
- package/dist/lib/languages/fr.js +2 -3
- package/dist/lib/languages/index.js +0 -6
- package/dist/lib/openapi-explorer-oauth-handler.js +0 -6
- package/dist/lib/openapi-explorer.js +61 -197
- package/dist/lib/react.js +4 -5
- package/dist/lib/styles/advanced-search-styles.js +1 -5
- package/dist/lib/styles/api-request-styles.js +1 -5
- package/dist/lib/styles/border-styles.js +1 -5
- package/dist/lib/styles/endpoint-styles.js +1 -5
- package/dist/lib/styles/flex-styles.js +1 -5
- package/dist/lib/styles/font-styles.js +1 -5
- package/dist/lib/styles/info-styles.js +1 -5
- package/dist/lib/styles/input-styles.js +1 -5
- package/dist/lib/styles/key-frame-styles.js +1 -5
- package/dist/lib/styles/nav-styles.js +1 -5
- package/dist/lib/styles/prism-styles.js +1 -5
- package/dist/lib/styles/schema-styles.js +1 -5
- package/dist/lib/styles/tab-styles.js +1 -5
- package/dist/lib/styles/table-styles.js +1 -5
- package/dist/lib/styles/tag-input-styles.js +1 -5
- package/dist/lib/templates/advance-search-template.js +0 -6
- package/dist/lib/templates/callback-template.js +1 -3
- package/dist/lib/templates/code-samples-template.js +0 -4
- package/dist/lib/templates/components-template.js +43 -9
- package/dist/lib/templates/endpoint-template.js +6 -29
- package/dist/lib/templates/expanded-endpoint-template.js +3 -17
- package/dist/lib/templates/focused-endpoint-template.js +0 -19
- package/dist/lib/templates/mainBodyTemplate.js +2 -13
- package/dist/lib/templates/navbar-template.js +9 -20
- package/dist/lib/templates/overview-template.js +0 -5
- package/dist/lib/templates/security-scheme-template.js +12 -79
- package/dist/lib/templates/server-template.js +1 -12
- package/dist/lib/utils/color-utils.js +4 -25
- package/dist/lib/utils/common-utils.js +3 -33
- package/dist/lib/utils/schema-utils.js +33 -141
- package/dist/lib/utils/spec-parser.js +35 -128
- package/dist/lib/utils/theme.js +3 -16
- package/dist/lib/utils/xml/xml.js +1 -42
- package/package.json +2 -2
@@ -1,24 +1,22 @@
|
|
1
1
|
import cloneDeep from 'lodash.clonedeep';
|
2
2
|
import merge from 'lodash.merge';
|
3
3
|
import RandExp from 'randexp';
|
4
|
-
import xmlFormatter from './xml/xml.js';
|
4
|
+
import xmlFormatter from './xml/xml.js';
|
5
5
|
|
6
|
+
// When the type is not known for a property set the displayed type to be this:
|
6
7
|
const IS_MISSING_TYPE_INFO_TYPE = '';
|
7
8
|
const EXAMPLE_VALUE_FOR_MISSING_TYPE = '';
|
8
|
-
/* Generates an schema object containing type and constraint info */
|
9
9
|
|
10
|
+
/* Generates an schema object containing type and constraint info */
|
10
11
|
export function getTypeInfo(schema, options = {
|
11
12
|
includeNulls: false
|
12
13
|
}) {
|
13
14
|
var _schema$items, _schema$const;
|
14
|
-
|
15
15
|
if (!schema) {
|
16
16
|
return undefined;
|
17
17
|
}
|
18
|
-
|
19
18
|
let dataType = IS_MISSING_TYPE_INFO_TYPE;
|
20
19
|
let format = schema.format || ((_schema$items = schema.items) === null || _schema$items === void 0 ? void 0 : _schema$items.format) || '';
|
21
|
-
|
22
20
|
if (schema.circularReference) {
|
23
21
|
dataType = `{recursive: ${schema.circularReference.name}} `;
|
24
22
|
} else if (schema.type) {
|
@@ -27,16 +25,13 @@ export function getTypeInfo(schema, options = {
|
|
27
25
|
['string', 'number'].forEach(type => {
|
28
26
|
dataType = dataType.replace(type, typeof schema.const !== 'undefined' && 'const' || schema.enum && `${type} enum` || schema.format || type);
|
29
27
|
});
|
30
|
-
|
31
28
|
if (schema.nullable && options.includeNulls) {
|
32
29
|
dataType += '┃null';
|
33
30
|
}
|
34
|
-
|
35
31
|
if (dataType.includes('┃null') && schema.format) {
|
36
32
|
format += '┃null';
|
37
33
|
}
|
38
34
|
}
|
39
|
-
|
40
35
|
const info = {
|
41
36
|
type: dataType,
|
42
37
|
format,
|
@@ -52,38 +47,30 @@ export function getTypeInfo(schema, options = {
|
|
52
47
|
allowedValues: (_schema$const = schema.const) !== null && _schema$const !== void 0 ? _schema$const : Array.isArray(schema.enum) ? schema.enum.join('┃') : '',
|
53
48
|
arrayType: ''
|
54
49
|
};
|
55
|
-
|
56
50
|
if (dataType === 'array' && schema.items) {
|
57
51
|
var _ref, _schema$items$default, _schema$const2;
|
58
|
-
|
59
52
|
const arrayItemType = schema.items.type;
|
60
53
|
const arrayItemDefault = (_ref = (_schema$items$default = schema.items.default) !== null && _schema$items$default !== void 0 ? _schema$items$default : schema.default) !== null && _ref !== void 0 ? _ref : '';
|
61
54
|
info.arrayType = `${schema.type} of ${Array.isArray(arrayItemType) ? arrayItemType.join('') : arrayItemType}`;
|
62
55
|
info.default = arrayItemDefault;
|
63
56
|
info.allowedValues = (_schema$const2 = schema.const) !== null && _schema$const2 !== void 0 ? _schema$const2 : Array.isArray(schema.items.enum) ? schema.items.enum.join('┃') : '';
|
64
57
|
}
|
65
|
-
|
66
58
|
if (schema.uniqueItems) {
|
67
59
|
info.constraints.push('Requires unique items');
|
68
60
|
}
|
69
|
-
|
70
61
|
if (dataType.match(/integer|number/g)) {
|
71
62
|
var _schema$minimum, _schema$maximum;
|
72
|
-
|
73
63
|
const minimum = (_schema$minimum = schema.minimum) !== null && _schema$minimum !== void 0 ? _schema$minimum : schema.exclusiveMinimum;
|
74
64
|
const maximum = (_schema$maximum = schema.maximum) !== null && _schema$maximum !== void 0 ? _schema$maximum : schema.exclusiveMaximum;
|
75
65
|
const leftBound = schema.minimum !== undefined ? '[' : '(';
|
76
66
|
const rightBound = schema.maximum !== undefined ? ']' : ')';
|
77
|
-
|
78
67
|
if (typeof minimum === 'number' || typeof maximum === 'number') {
|
79
68
|
info.constraints.push(`Range: ${leftBound}${minimum !== null && minimum !== void 0 ? minimum : ''},${maximum !== null && maximum !== void 0 ? maximum : ''}${rightBound}`);
|
80
69
|
}
|
81
|
-
|
82
70
|
if (schema.multipleOf !== undefined) {
|
83
71
|
info.constraints.push(`Multiples: ${schema.multipleOf}`);
|
84
72
|
}
|
85
73
|
}
|
86
|
-
|
87
74
|
if (dataType.match(/string/g)) {
|
88
75
|
if (schema.minLength !== undefined && schema.maxLength !== undefined) {
|
89
76
|
info.constraints.push(`Min length: ${schema.minLength}, Max length: ${schema.maxLength}`);
|
@@ -93,7 +80,6 @@ export function getTypeInfo(schema, options = {
|
|
93
80
|
info.constraints.push(`Max length: ${schema.maxLength}`);
|
94
81
|
}
|
95
82
|
}
|
96
|
-
|
97
83
|
info.html = JSON.stringify({
|
98
84
|
type: info.type,
|
99
85
|
format: info.format,
|
@@ -112,39 +98,29 @@ export function getTypeInfo(schema, options = {
|
|
112
98
|
}
|
113
99
|
export function getSampleValueByType(schemaObj, fallbackPropertyName, skipExampleIds) {
|
114
100
|
var _Object$values$0$valu, _Object$values$, _schemaObj$type;
|
115
|
-
|
116
101
|
const example = Array.isArray(schemaObj.examples) ? schemaObj.examples[0] : (_Object$values$0$valu = (_Object$values$ = Object.values(schemaObj.examples || {})[0]) === null || _Object$values$ === void 0 ? void 0 : _Object$values$.value) !== null && _Object$values$0$valu !== void 0 ? _Object$values$0$valu : schemaObj.example;
|
117
|
-
|
118
102
|
if (skipExampleIds && typeof example === 'string' && fallbackPropertyName.match(/id$/i)) {
|
119
103
|
return '';
|
120
104
|
}
|
121
|
-
|
122
105
|
if (typeof example !== 'undefined') {
|
123
106
|
return example;
|
124
107
|
}
|
125
|
-
|
126
108
|
if (schemaObj.default) {
|
127
109
|
return schemaObj.default;
|
128
110
|
}
|
129
|
-
|
130
111
|
if (Object.keys(schemaObj).length === 0) {
|
131
112
|
return EXAMPLE_VALUE_FOR_MISSING_TYPE;
|
132
113
|
}
|
133
|
-
|
134
114
|
if (schemaObj.circularReference) {
|
135
115
|
return schemaObj.$ref;
|
136
116
|
}
|
137
|
-
|
138
117
|
const typeValue = Array.isArray(schemaObj.type) ? schemaObj.type.filter(t => t !== 'null')[0] : (_schemaObj$type = schemaObj.type) !== null && _schemaObj$type !== void 0 ? _schemaObj$type : '';
|
139
|
-
|
140
118
|
if (typeof schemaObj.const !== 'undefined') {
|
141
119
|
return schemaObj.const;
|
142
120
|
}
|
143
|
-
|
144
121
|
if (schemaObj.enum) {
|
145
122
|
return schemaObj.enum[0];
|
146
123
|
}
|
147
|
-
|
148
124
|
if (typeValue.match(/^integer|^number/g)) {
|
149
125
|
const multipleOf = Number.isNaN(Number(schemaObj.multipleOf)) ? undefined : Number(schemaObj.multipleOf);
|
150
126
|
const maximum = Number.isNaN(Number(schemaObj.maximum)) ? undefined : Number(schemaObj.maximum);
|
@@ -152,94 +128,74 @@ export function getSampleValueByType(schemaObj, fallbackPropertyName, skipExampl
|
|
152
128
|
const finalVal = multipleOf ? multipleOf >= minimumPossibleVal ? multipleOf : minimumPossibleVal % multipleOf === 0 ? minimumPossibleVal : Math.ceil(minimumPossibleVal / multipleOf) * multipleOf : minimumPossibleVal;
|
153
129
|
return finalVal;
|
154
130
|
}
|
155
|
-
|
156
131
|
if (typeValue.match(/^boolean/g)) {
|
157
132
|
return false;
|
158
133
|
}
|
159
|
-
|
160
134
|
if (typeValue.match(/^null/g)) {
|
161
135
|
return null;
|
162
136
|
}
|
163
|
-
|
164
137
|
if (skipExampleIds && typeValue.match(/^string/g) && fallbackPropertyName.match(/id$/i)) {
|
165
138
|
return '';
|
166
139
|
}
|
167
|
-
|
168
140
|
if (typeValue.match(/^string/g)) {
|
169
141
|
if (schemaObj.pattern) {
|
170
142
|
const examplePattern = schemaObj.pattern.replace(/[+*](?![^\][]*[\]])/g, '{8}').replace(/\{\d*,(\d+)?\}/g, '{8}');
|
171
|
-
|
172
143
|
try {
|
173
144
|
return new RandExp(examplePattern).gen() || fallbackPropertyName || 'string';
|
174
145
|
} catch (error) {
|
175
146
|
return fallbackPropertyName || 'string';
|
176
147
|
}
|
177
148
|
}
|
178
|
-
|
179
149
|
if (schemaObj.format) {
|
180
150
|
switch (schemaObj.format.toLowerCase()) {
|
181
151
|
case 'url':
|
182
152
|
return 'https://example.com';
|
183
|
-
|
184
153
|
case 'uri':
|
185
154
|
return 'urn:namespace:type:example/resource';
|
186
|
-
|
187
155
|
case 'date':
|
188
156
|
return new Date().toISOString().split('T')[0];
|
189
|
-
|
190
157
|
case 'time':
|
191
158
|
return new Date().toISOString().split('T')[1];
|
192
|
-
|
193
159
|
case 'date-time':
|
194
160
|
return new Date().toISOString();
|
195
|
-
|
196
161
|
case 'duration':
|
197
162
|
return 'P3Y6M4DT12H30M5S';
|
198
163
|
// P=Period 3-Years 6-Months 4-Days 12-Hours 30-Minutes 5-Seconds
|
199
|
-
|
200
164
|
case 'email':
|
201
165
|
case 'idn-email':
|
202
166
|
return 'user@example.com';
|
203
|
-
|
204
167
|
case 'hostname':
|
205
168
|
case 'idn-hostname':
|
206
169
|
return 'www.example.com';
|
207
|
-
|
208
170
|
case 'ipv4':
|
209
171
|
return '192.168.0.1';
|
210
|
-
|
211
172
|
case 'ipv6':
|
212
173
|
return '2001:0db8:5b96:0000:0000:426f:8e17:642a';
|
213
|
-
|
214
174
|
case 'uuid':
|
215
175
|
return '4e0ba220-9575-11eb-a8b3-0242ac130003';
|
216
|
-
|
217
176
|
case 'byte':
|
218
177
|
// Byte type is actually a base64 encoded string: https://spec.openapis.org/oas/v3.0.0#data-types
|
219
178
|
return Buffer.from('example').toString('base64');
|
220
|
-
|
221
179
|
default:
|
222
180
|
return schemaObj.format;
|
223
181
|
}
|
224
182
|
} else {
|
225
183
|
return fallbackPropertyName || 'string';
|
226
184
|
}
|
227
|
-
}
|
228
|
-
|
229
|
-
|
185
|
+
}
|
186
|
+
// If type cannot be determined
|
230
187
|
return EXAMPLE_VALUE_FOR_MISSING_TYPE;
|
231
188
|
}
|
232
|
-
|
233
189
|
function duplicateExampleWithNewPropertyValues(objectExamples, propertyName, propertyValues) {
|
234
190
|
// Limit max number of property examples to 2 and limit the max number of examples to 10
|
235
191
|
return objectExamples.reduce((exampleList, example) => {
|
236
|
-
const examplesFromPropertyValues = propertyValues.slice(0, 2).map(value => ({
|
192
|
+
const examplesFromPropertyValues = propertyValues.slice(0, 2).map(value => ({
|
193
|
+
...cloneDeep(example),
|
237
194
|
[propertyName]: value
|
238
195
|
}));
|
239
196
|
return exampleList.concat(...examplesFromPropertyValues);
|
240
197
|
}, []).slice(0, 10);
|
241
198
|
}
|
242
|
-
|
243
199
|
export function getExampleValuesFromSchema(schema, config = {}) {
|
244
200
|
// Wrap the top level so that the recursive object can treat it as a normal property and we'll hit the 'object' below, otherwise we'll never create the top level.
|
245
201
|
if (config.xml) {
|
@@ -250,29 +206,26 @@ export function getExampleValuesFromSchema(schema, config = {}) {
|
|
250
206
|
} : schema, config);
|
251
207
|
return xmlResult.map(example => example[0]);
|
252
208
|
}
|
253
|
-
|
254
209
|
return getExampleValuesFromSchemaRecursive(schema, config);
|
255
|
-
}
|
210
|
+
}
|
256
211
|
|
212
|
+
// TODO: Support getting the `summary` from the examples object or the `title` from the schema object
|
257
213
|
function getExampleValuesFromSchemaRecursive(rawSchema, config = {}) {
|
258
214
|
var _rawSchema$items, _rawSchema$items$xml, _rawSchema$xml;
|
259
|
-
|
260
215
|
if (!rawSchema) {
|
261
216
|
return [];
|
262
|
-
}
|
263
|
-
|
217
|
+
}
|
264
218
|
|
219
|
+
// XML Support
|
265
220
|
const xmlAttributes = {};
|
266
221
|
const xmlTagProperties = [];
|
267
222
|
const {
|
268
223
|
prefix,
|
269
224
|
namespace
|
270
225
|
} = rawSchema.xml || {};
|
271
|
-
|
272
226
|
if (namespace) {
|
273
227
|
xmlAttributes[prefix ? `xmlns:${prefix}` : 'xmlns'] = namespace;
|
274
228
|
}
|
275
|
-
|
276
229
|
const nodeName = (rawSchema === null || rawSchema === void 0 ? void 0 : (_rawSchema$items = rawSchema.items) === null || _rawSchema$items === void 0 ? void 0 : (_rawSchema$items$xml = _rawSchema$items.xml) === null || _rawSchema$items$xml === void 0 ? void 0 : _rawSchema$items$xml.name) || (rawSchema === null || rawSchema === void 0 ? void 0 : (_rawSchema$xml = rawSchema.xml) === null || _rawSchema$xml === void 0 ? void 0 : _rawSchema$xml.name) || config.propertyName || 'root';
|
277
230
|
const overridePropertyName = prefix ? `${prefix}:${nodeName}` : nodeName;
|
278
231
|
const {
|
@@ -281,39 +234,31 @@ function getExampleValuesFromSchemaRecursive(rawSchema, config = {}) {
|
|
281
234
|
anyOf,
|
282
235
|
...schema
|
283
236
|
} = rawSchema;
|
284
|
-
|
285
237
|
if (allOf) {
|
286
238
|
const mergedAllOf = merge({}, ...allOf, schema);
|
287
239
|
return getExampleValuesFromSchemaRecursive(mergedAllOf, config);
|
288
240
|
}
|
289
|
-
|
290
241
|
if (oneOf || anyOf) {
|
291
242
|
const examples = (oneOf || anyOf).map(s => getExampleValuesFromSchemaRecursive(merge({}, schema, s), config)).flat(1);
|
292
|
-
|
293
243
|
const hash = value => {
|
294
244
|
if (typeof value === 'object') {
|
295
245
|
return JSON.stringify(value);
|
296
246
|
}
|
297
|
-
|
298
247
|
return value;
|
299
248
|
};
|
300
|
-
|
301
249
|
const uniqueExamples = examples.reduce((acc, e) => {
|
302
250
|
acc[hash(e)] = e;
|
303
251
|
return acc;
|
304
252
|
}, {});
|
305
253
|
return Object.values(uniqueExamples);
|
306
254
|
}
|
307
|
-
|
308
255
|
return getSimpleValueResult(schema, config, namespace, prefix, xmlAttributes, xmlTagProperties, overridePropertyName);
|
309
256
|
}
|
310
|
-
|
311
257
|
function getSimpleValueResult(schema, config, namespace, prefix, xmlAttributes, xmlTagProperties, overridePropertyName) {
|
312
258
|
if (schema.type === 'array' || schema.items) {
|
313
259
|
if (!config.xml) {
|
314
260
|
return [getExampleValuesFromSchemaRecursive(schema.items || {}, config)];
|
315
261
|
}
|
316
|
-
|
317
262
|
if (!schema.xml || !schema.xml.wrapped) {
|
318
263
|
const arrayExamples = getExampleValuesFromSchemaRecursive(schema.items || {}, config);
|
319
264
|
xmlTagProperties.push({
|
@@ -323,8 +268,8 @@ function getSimpleValueResult(schema, config, namespace, prefix, xmlAttributes,
|
|
323
268
|
});
|
324
269
|
return [xmlTagProperties];
|
325
270
|
}
|
326
|
-
|
327
|
-
|
271
|
+
const arrayExamples = getExampleValuesFromSchemaRecursive(schema.items || {}, {
|
272
|
+
...config,
|
328
273
|
propertyName: overridePropertyName
|
329
274
|
});
|
330
275
|
xmlTagProperties.push({
|
@@ -334,36 +279,29 @@ function getSimpleValueResult(schema, config, namespace, prefix, xmlAttributes,
|
|
334
279
|
});
|
335
280
|
return [xmlTagProperties];
|
336
281
|
}
|
337
|
-
|
338
282
|
if (schema.type === 'object' || schema.properties) {
|
339
283
|
let objectExamples = [{}];
|
340
284
|
Object.keys(schema.properties || {}).forEach(propertyName => {
|
341
285
|
const innerSchema = schema.properties[propertyName];
|
342
|
-
|
343
286
|
if (innerSchema.deprecated) {
|
344
287
|
return;
|
345
288
|
}
|
346
|
-
|
347
289
|
if (innerSchema.readOnly && !config.includeReadOnly) {
|
348
290
|
return;
|
349
291
|
}
|
350
|
-
|
351
292
|
if (innerSchema.writeOnly && !config.includeWriteOnly) {
|
352
293
|
return;
|
353
294
|
}
|
354
|
-
|
355
|
-
|
295
|
+
const propertyExamples = getExampleValuesFromSchemaRecursive(innerSchema, {
|
296
|
+
...config,
|
356
297
|
propertyName
|
357
298
|
});
|
358
299
|
objectExamples = duplicateExampleWithNewPropertyValues(objectExamples, propertyName, propertyExamples);
|
359
|
-
|
360
300
|
if (innerSchema.xml && innerSchema.xml.namespace) {
|
361
301
|
xmlAttributes[innerSchema.xml.prefix ? `xmlns:${innerSchema.xml.prefix}` : 'xmlns'] = namespace;
|
362
302
|
}
|
363
|
-
|
364
303
|
const innerNodeName = innerSchema.xml && innerSchema.xml.name || propertyName || config.propertyName;
|
365
304
|
const innerOverridePropertyName = prefix ? `${prefix}:${innerNodeName}` : innerNodeName;
|
366
|
-
|
367
305
|
if (innerSchema.xml && innerSchema.xml.attribute) {
|
368
306
|
xmlAttributes[innerOverridePropertyName] = propertyExamples[0];
|
369
307
|
} else {
|
@@ -372,20 +310,16 @@ function getSimpleValueResult(schema, config, namespace, prefix, xmlAttributes,
|
|
372
310
|
});
|
373
311
|
}
|
374
312
|
});
|
375
|
-
|
376
313
|
if (Object.keys(xmlAttributes).length) {
|
377
314
|
xmlTagProperties.push({
|
378
315
|
_attr: xmlAttributes
|
379
316
|
});
|
380
317
|
}
|
381
|
-
|
382
318
|
return config.xml ? [xmlTagProperties] : objectExamples;
|
383
319
|
}
|
384
|
-
|
385
320
|
const value = getSampleValueByType(schema, config.propertyName, config.skipExampleIds);
|
386
321
|
return [value];
|
387
322
|
}
|
388
|
-
|
389
323
|
export function isPatternProperty(label) {
|
390
324
|
return label.match(/^<any-key>|<pattern:/);
|
391
325
|
}
|
@@ -397,12 +331,10 @@ export function isPatternProperty(label) {
|
|
397
331
|
* @param {number} level - recursion level
|
398
332
|
* @param {string} suffix - used for suffixing property names to avoid duplicate props during object composition
|
399
333
|
*/
|
400
|
-
|
401
334
|
export function schemaInObjectNotation(rawSchema, options, level = 0, suffix = '') {
|
402
335
|
if (!rawSchema) {
|
403
336
|
return undefined;
|
404
337
|
}
|
405
|
-
|
406
338
|
const {
|
407
339
|
allOf,
|
408
340
|
oneOf,
|
@@ -416,15 +348,12 @@ export function schemaInObjectNotation(rawSchema, options, level = 0, suffix = '
|
|
416
348
|
const metadata = {
|
417
349
|
constraints: []
|
418
350
|
};
|
419
|
-
|
420
351
|
if (schema.uniqueItems) {
|
421
352
|
metadata.constraints.push('Requires unique items');
|
422
353
|
}
|
423
|
-
|
424
354
|
if (typeof schema.minItems === 'number' || typeof schema.maxItems === 'number') {
|
425
355
|
metadata.constraints.push(`Length: [${schema.minItems || 0}${schema.maxItems ? ', ' : '+'}${schema.maxItems || ''}]`);
|
426
356
|
}
|
427
|
-
|
428
357
|
if (allOf) {
|
429
358
|
// If allOf is an array of multiple elements, then all the keys makes a single object
|
430
359
|
const objWithAllProps = {};
|
@@ -451,10 +380,8 @@ export function schemaInObjectNotation(rawSchema, options, level = 0, suffix = '
|
|
451
380
|
(anyOf || oneOf || []).forEach((v, index) => {
|
452
381
|
if (v.type === 'object' || v.properties || v.allOf || v.anyOf || v.oneOf) {
|
453
382
|
const partialObj = schemaInObjectNotation(v, options);
|
454
|
-
|
455
383
|
if (partialObj) {
|
456
384
|
var _partialObj$Flags, _partialObj$Flags2;
|
457
|
-
|
458
385
|
objWithAnyOfProps[`::OPTION~${index + 1}${v.title ? `~${v.title}` : ''}`] = partialObj;
|
459
386
|
objWithAnyOfProps['::type'] = 'xxx-of-option';
|
460
387
|
readOnly = readOnly && ((_partialObj$Flags = partialObj['::flags']) === null || _partialObj$Flags === void 0 ? void 0 : _partialObj$Flags['🆁']);
|
@@ -462,7 +389,6 @@ export function schemaInObjectNotation(rawSchema, options, level = 0, suffix = '
|
|
462
389
|
}
|
463
390
|
} else if (v.type === 'array' || v.items) {
|
464
391
|
var _partialObj$Flags3, _partialObj$Flags4;
|
465
|
-
|
466
392
|
const partialObj = schemaInObjectNotation(v, options);
|
467
393
|
objWithAnyOfProps[`::OPTION~${index + 1}${v.title ? `~${v.title}` : ''}`] = partialObj;
|
468
394
|
objWithAnyOfProps['::type'] = 'xxx-of-array';
|
@@ -470,10 +396,8 @@ export function schemaInObjectNotation(rawSchema, options, level = 0, suffix = '
|
|
470
396
|
writeOnly = writeOnly && ((_partialObj$Flags4 = partialObj['::flags']) === null || _partialObj$Flags4 === void 0 ? void 0 : _partialObj$Flags4['🆆']);
|
471
397
|
} else {
|
472
398
|
const typeInfo = getTypeInfo(v, options);
|
473
|
-
|
474
399
|
if (typeInfo !== null && typeInfo !== void 0 && typeInfo.type) {
|
475
400
|
var _objWithAnyOfProps$, _objWithAnyOfProps$2;
|
476
|
-
|
477
401
|
const prop = `::OPTION~${index + 1}${v.title ? `~${v.title}` : ''}`;
|
478
402
|
objWithAnyOfProps[prop] = `${typeInfo.html}`;
|
479
403
|
objWithAnyOfProps['::type'] = 'xxx-of-option';
|
@@ -484,12 +408,11 @@ export function schemaInObjectNotation(rawSchema, options, level = 0, suffix = '
|
|
484
408
|
});
|
485
409
|
const obj = schemaInObjectNotation(schema, options, 0);
|
486
410
|
const resultObj = typeof obj === 'object' && !Array.isArray(obj) ? obj : {};
|
487
|
-
|
488
411
|
if (Object.keys(objWithAnyOfProps).length) {
|
489
412
|
resultObj[anyOf ? `::ANY~OF ${suffix}` : `::ONE~OF ${suffix}`] = objWithAnyOfProps;
|
490
413
|
}
|
491
|
-
|
492
|
-
resultObj['::type'] = 'object';
|
414
|
+
resultObj['::link'] = schema.title;
|
415
|
+
resultObj['::type'] = schema.title || 'object';
|
493
416
|
resultObj['::flags'] = {
|
494
417
|
'🆁': readOnly && '🆁',
|
495
418
|
'🆆': writeOnly && '🆆'
|
@@ -501,9 +424,9 @@ export function schemaInObjectNotation(rawSchema, options, level = 0, suffix = '
|
|
501
424
|
} else if (Array.isArray(propertyType)) {
|
502
425
|
const obj = {
|
503
426
|
'::type': ''
|
504
|
-
};
|
427
|
+
};
|
428
|
+
// When a property has multiple types, then check further if any of the types are array or object, if yes then modify the schema using one-of
|
505
429
|
// Clone the schema - as it will be modified to replace multi-data-types with one-of;
|
506
|
-
|
507
430
|
const subSchema = JSON.parse(JSON.stringify(schema));
|
508
431
|
const primitiveType = [];
|
509
432
|
const complexTypes = [];
|
@@ -522,24 +445,22 @@ export function schemaInObjectNotation(rawSchema, options, level = 0, suffix = '
|
|
522
445
|
}
|
523
446
|
});
|
524
447
|
let multiPrimitiveTypes;
|
525
|
-
|
526
448
|
if (primitiveType.length > 0) {
|
527
449
|
subSchema.type = primitiveType.join('┃');
|
528
450
|
multiPrimitiveTypes = getTypeInfo(subSchema, options);
|
529
|
-
|
530
451
|
if (complexTypes.length === 0) {
|
531
452
|
var _multiPrimitiveTypes;
|
532
|
-
|
533
453
|
return `${((_multiPrimitiveTypes = multiPrimitiveTypes) === null || _multiPrimitiveTypes === void 0 ? void 0 : _multiPrimitiveTypes.html) || ''}`;
|
534
454
|
}
|
535
455
|
}
|
536
|
-
|
537
456
|
if (complexTypes.length > 0) {
|
457
|
+
obj['::link'] = schema.title;
|
538
458
|
obj['::type'] = 'object';
|
539
459
|
const multiTypeOptions = {
|
540
460
|
'::type': 'xxx-of-option'
|
541
|
-
};
|
461
|
+
};
|
542
462
|
|
463
|
+
// Generate ONE-OF options for complexTypes
|
543
464
|
complexTypes.forEach((v, i) => {
|
544
465
|
if (v === 'null') {
|
545
466
|
multiTypeOptions[`::OPTION~${i + 1}`] = 'NULL~|~~|~~|~~|~~|~~|~~|~~|~';
|
@@ -556,21 +477,19 @@ export function schemaInObjectNotation(rawSchema, options, level = 0, suffix = '
|
|
556
477
|
'🆁': schema.readOnly && '🆁',
|
557
478
|
'🆆': schema.writeOnly && '🆆'
|
558
479
|
},
|
559
|
-
'::
|
480
|
+
'::link': schema.title,
|
481
|
+
'::type': schema.title || 'object',
|
560
482
|
'::deprecated': schema.deprecated || false,
|
561
483
|
'::metadata': metadata
|
562
484
|
};
|
563
|
-
|
564
485
|
for (const key in schemaProperties) {
|
565
486
|
var _schema$required;
|
566
|
-
|
567
487
|
if (!schema.deprecated && !schemaProperties[key].deprecated && (_schema$required = schema.required) !== null && _schema$required !== void 0 && _schema$required.includes(key)) {
|
568
488
|
objTypeOption[`${key}*`] = schemaInObjectNotation(schemaProperties[key], options, level + 1);
|
569
489
|
} else {
|
570
490
|
objTypeOption[key] = schemaInObjectNotation(schemaProperties[key], options, level + 1);
|
571
491
|
}
|
572
492
|
}
|
573
|
-
|
574
493
|
multiTypeOptions[`::OPTION~${i + 1}`] = objTypeOption;
|
575
494
|
} else if (v === 'array') {
|
576
495
|
multiTypeOptions[`::OPTION~${i + 1}`] = {
|
@@ -580,6 +499,7 @@ export function schemaInObjectNotation(rawSchema, options, level = 0, suffix = '
|
|
580
499
|
'🆁': schema.readOnly && '🆁',
|
581
500
|
'🆆': schema.writeOnly && '🆆'
|
582
501
|
},
|
502
|
+
'::link': arrayItemsSchema.title || schema.title,
|
583
503
|
'::type': 'array',
|
584
504
|
// Array properties are read from the ::props object instead of reading from the keys of this object
|
585
505
|
// '::props': schemaInObjectNotation(Object.assign({ deprecated: schema.deprecated, readOnly: schema.readOnly, writeOnly: schema.writeOnly }, arrayItemsSchema), options, (level + 1)),
|
@@ -592,7 +512,6 @@ export function schemaInObjectNotation(rawSchema, options, level = 0, suffix = '
|
|
592
512
|
multiTypeOptions[`::OPTION~${complexTypes.length + 1}`] = multiPrimitiveTypes && multiPrimitiveTypes.html || '';
|
593
513
|
obj['::ONE~OF'] = multiTypeOptions;
|
594
514
|
}
|
595
|
-
|
596
515
|
return obj;
|
597
516
|
} else if (propertyType === 'object' || schemaProperties) {
|
598
517
|
const obj = {
|
@@ -604,28 +523,24 @@ export function schemaInObjectNotation(rawSchema, options, level = 0, suffix = '
|
|
604
523
|
'🆁': schema.readOnly && '🆁',
|
605
524
|
'🆆': schema.writeOnly && '🆆'
|
606
525
|
};
|
607
|
-
obj['::
|
526
|
+
obj['::link'] = schema.title;
|
527
|
+
obj['::type'] = schema.title || 'object';
|
608
528
|
obj['::deprecated'] = schema.deprecated || false;
|
609
529
|
obj['::metadata'] = metadata;
|
610
|
-
|
611
530
|
for (const key in schemaProperties) {
|
612
531
|
var _schema$required2;
|
613
|
-
|
614
532
|
if (!schema.deprecated && !schemaProperties[key].deprecated && (_schema$required2 = schema.required) !== null && _schema$required2 !== void 0 && _schema$required2.includes(key)) {
|
615
533
|
obj[`${key}*`] = schemaInObjectNotation(schemaProperties[key], options, level + 1);
|
616
534
|
} else {
|
617
535
|
obj[key] = schemaInObjectNotation(schemaProperties[key], options, level + 1);
|
618
536
|
}
|
619
537
|
}
|
620
|
-
|
621
538
|
for (const key in schemaPatternProperties) {
|
622
539
|
obj[`<pattern: ${key}>`] = schemaInObjectNotation(schemaPatternProperties[key], options, level + 1);
|
623
540
|
}
|
624
|
-
|
625
541
|
if (schema.additionalProperties) {
|
626
542
|
obj['<any-key>'] = schemaInObjectNotation(schema.additionalProperties, options);
|
627
543
|
}
|
628
|
-
|
629
544
|
return obj;
|
630
545
|
} else if (propertyType === 'array' || arrayItemsSchema) {
|
631
546
|
// If Array
|
@@ -638,43 +553,40 @@ export function schemaInObjectNotation(rawSchema, options, level = 0, suffix = '
|
|
638
553
|
'🆁': schema.readOnly && '🆁',
|
639
554
|
'🆆': schema.writeOnly && '🆆'
|
640
555
|
};
|
556
|
+
obj['::link'] = arrayItemsSchema.title || schema.title;
|
641
557
|
obj['::type'] = 'array';
|
642
558
|
obj['::deprecated'] = schema.deprecated || false;
|
643
|
-
obj['::metadata'] = metadata;
|
559
|
+
obj['::metadata'] = metadata;
|
560
|
+
// Array properties are read from the ::props object instead of reading from the keys of this object
|
644
561
|
// Use type: undefined to prevent schema recursion by passing array from the parent to the next loop. arrayItemsSchema should have had type defined but it doesn't.
|
645
|
-
|
646
562
|
obj['::props'] = schemaInObjectNotation(Object.assign({}, schema, {
|
647
563
|
type: undefined
|
648
564
|
}, arrayItemsSchema), options, level + 1);
|
649
|
-
|
650
565
|
if (arrayItemsSchema !== null && arrayItemsSchema !== void 0 && arrayItemsSchema.items) {
|
651
566
|
obj['::array-type'] = arrayItemsSchema.items.type;
|
652
567
|
}
|
653
|
-
|
654
568
|
return obj;
|
655
569
|
}
|
656
|
-
|
657
570
|
const typeObj = getTypeInfo(schema, options);
|
658
571
|
return `${(typeObj === null || typeObj === void 0 ? void 0 : typeObj.html) || ''}`;
|
659
572
|
}
|
660
|
-
/* Create Example object */
|
661
573
|
|
574
|
+
/* Create Example object */
|
662
575
|
export function generateExample(examples, example, schema, rawMimeType, includeReadOnly = true, includeWriteOnly = true, outputType, skipExampleIds = false) {
|
663
576
|
const mimeType = rawMimeType || 'application/json';
|
664
|
-
const finalExamples = [];
|
577
|
+
const finalExamples = [];
|
665
578
|
|
579
|
+
// First check if examples is provided
|
666
580
|
if (examples) {
|
667
581
|
for (const eg in examples) {
|
668
582
|
let egContent = '';
|
669
583
|
let egFormat = 'json';
|
670
|
-
|
671
584
|
if (mimeType.toLowerCase().includes('json')) {
|
672
585
|
if (outputType === 'text') {
|
673
586
|
egContent = typeof examples[eg].value === 'string' ? examples[eg].value : JSON.stringify(examples[eg].value, undefined, 2);
|
674
587
|
egFormat = 'text';
|
675
588
|
} else {
|
676
589
|
egContent = examples[eg].value;
|
677
|
-
|
678
590
|
if (typeof examples[eg].value === 'string') {
|
679
591
|
try {
|
680
592
|
const fixedJsonString = examples[eg].value.replace(/([\w]+)(:)/g, '"$1"$2').replace(/'/g, '"');
|
@@ -690,7 +602,6 @@ export function generateExample(examples, example, schema, rawMimeType, includeR
|
|
690
602
|
egContent = examples[eg].value;
|
691
603
|
egFormat = 'text';
|
692
604
|
}
|
693
|
-
|
694
605
|
finalExamples.push({
|
695
606
|
exampleId: eg,
|
696
607
|
exampleSummary: examples[eg].summary || '',
|
@@ -703,7 +614,6 @@ export function generateExample(examples, example, schema, rawMimeType, includeR
|
|
703
614
|
} else if (example) {
|
704
615
|
let egContent = '';
|
705
616
|
let egFormat = 'json';
|
706
|
-
|
707
617
|
if (mimeType.toLowerCase().includes('json')) {
|
708
618
|
if (outputType === 'text') {
|
709
619
|
egContent = typeof example === 'string' ? example : JSON.stringify(example, undefined, 2);
|
@@ -724,7 +634,6 @@ export function generateExample(examples, example, schema, rawMimeType, includeR
|
|
724
634
|
egContent = example;
|
725
635
|
egFormat = 'text';
|
726
636
|
}
|
727
|
-
|
728
637
|
finalExamples.push({
|
729
638
|
exampleId: 'Example',
|
730
639
|
exampleSummary: '',
|
@@ -733,13 +642,12 @@ export function generateExample(examples, example, schema, rawMimeType, includeR
|
|
733
642
|
exampleValue: egContent,
|
734
643
|
exampleFormat: egFormat
|
735
644
|
});
|
736
|
-
}
|
737
|
-
|
645
|
+
}
|
738
646
|
|
647
|
+
// If schema-level examples are not provided then generate one based on the schema field types
|
739
648
|
if (finalExamples.length) {
|
740
649
|
return finalExamples;
|
741
650
|
}
|
742
|
-
|
743
651
|
if (schema !== null && schema !== void 0 && schema.example) {
|
744
652
|
// Note: schema.examples (plurals) is not allowed as per spec
|
745
653
|
return [{
|
@@ -751,7 +659,6 @@ export function generateExample(examples, example, schema, rawMimeType, includeR
|
|
751
659
|
exampleFormat: mimeType.toLowerCase().includes('json') && typeof schema.example === 'object' ? 'json' : 'text'
|
752
660
|
}];
|
753
661
|
}
|
754
|
-
|
755
662
|
const config = {
|
756
663
|
includeReadOnly,
|
757
664
|
includeWriteOnly,
|
@@ -759,7 +666,6 @@ export function generateExample(examples, example, schema, rawMimeType, includeR
|
|
759
666
|
xml: mimeType.toLowerCase().includes('xml')
|
760
667
|
};
|
761
668
|
const samples = getExampleValuesFromSchema(schema, config);
|
762
|
-
|
763
669
|
if (!samples || !mimeType.toLowerCase().includes('json') && !mimeType.toLowerCase().includes('text') && !mimeType.toLowerCase().includes('*/*') && !mimeType.toLowerCase().includes('xml')) {
|
764
670
|
return [{
|
765
671
|
exampleId: 'Example',
|
@@ -770,10 +676,8 @@ export function generateExample(examples, example, schema, rawMimeType, includeR
|
|
770
676
|
exampleFormat: 'text'
|
771
677
|
}];
|
772
678
|
}
|
773
|
-
|
774
679
|
return samples.map((sample, sampleCounter) => {
|
775
680
|
let exampleValue = '';
|
776
|
-
|
777
681
|
if (mimeType.toLowerCase().includes('xml')) {
|
778
682
|
exampleValue = xmlFormatter(sample, {
|
779
683
|
declaration: true,
|
@@ -782,7 +686,6 @@ export function generateExample(examples, example, schema, rawMimeType, includeR
|
|
782
686
|
} else {
|
783
687
|
exampleValue = outputType === 'text' ? JSON.stringify(sample, null, 8) : sample;
|
784
688
|
}
|
785
|
-
|
786
689
|
return {
|
787
690
|
exampleId: `Example-${sampleCounter}`,
|
788
691
|
exampleSummary: '',
|