openapi-explorer 0.9.310 → 0.9.314

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.
Files changed (121) hide show
  1. package/CHANGELOG.md +1 -0
  2. package/dist/{openapi-explorer.min.js → browser/openapi-explorer.min.js} +3 -3
  3. package/dist/{openapi-explorer.min.js.map → browser/openapi-explorer.min.js.map} +0 -0
  4. package/dist/es/components/api-request.js +936 -0
  5. package/dist/es/components/api-response.js +184 -0
  6. package/dist/es/components/json-tree.js +67 -0
  7. package/{src → dist/es}/components/mime-types.js +11 -17
  8. package/dist/es/components/schema-table.js +156 -0
  9. package/dist/es/components/schema-tree.js +191 -0
  10. package/dist/es/components/tag-input.js +67 -0
  11. package/{src → dist/es}/openapi-explorer-oauth-handler.js +2 -2
  12. package/{src → dist/es}/openapi-explorer.js +364 -371
  13. package/dist/es/styles/advanced-search-styles.js +2 -0
  14. package/dist/es/styles/api-request-styles.js +2 -0
  15. package/dist/es/styles/border-styles.js +2 -0
  16. package/dist/es/styles/endpoint-styles.js +2 -0
  17. package/dist/es/styles/flex-styles.js +2 -0
  18. package/dist/es/styles/font-styles.js +2 -0
  19. package/dist/es/styles/info-styles.js +2 -0
  20. package/dist/es/styles/input-styles.js +4 -0
  21. package/dist/es/styles/nav-styles.js +2 -0
  22. package/dist/es/styles/prism-styles.js +2 -0
  23. package/dist/es/styles/schema-styles.js +2 -0
  24. package/dist/es/styles/tab-styles.js +2 -0
  25. package/dist/es/styles/table-styles.js +2 -0
  26. package/dist/es/templates/advance-search-template.js +37 -0
  27. package/dist/es/templates/callback-template.js +7 -0
  28. package/dist/es/templates/code-samples-template.js +26 -0
  29. package/dist/es/templates/components-template.js +17 -0
  30. package/dist/es/templates/endpoint-template.js +94 -0
  31. package/dist/es/templates/expanded-endpoint-template.js +32 -0
  32. package/{src → dist/es}/templates/focused-endpoint-template.js +15 -15
  33. package/dist/es/templates/navbar-template.js +46 -0
  34. package/dist/es/templates/overview-template.js +9 -0
  35. package/dist/es/templates/responsiveViewMainBodyTemplate.js +30 -0
  36. package/dist/es/templates/security-scheme-template.js +330 -0
  37. package/dist/es/templates/server-template.js +42 -0
  38. package/{src → dist/es}/utils/color-utils.js +53 -16
  39. package/{src → dist/es}/utils/common-utils.js +18 -18
  40. package/{src → dist/es}/utils/schema-utils.js +248 -124
  41. package/{src → dist/es}/utils/spec-parser.js +112 -71
  42. package/dist/es/utils/theme.js +75 -0
  43. package/{src → dist/es}/utils/xml/xml.js +41 -38
  44. package/dist/lib/components/api-request.js +957 -0
  45. package/dist/lib/components/api-response.js +206 -0
  46. package/dist/lib/components/json-tree.js +82 -0
  47. package/dist/lib/components/mime-types.js +70 -0
  48. package/dist/lib/components/schema-table.js +170 -0
  49. package/dist/lib/components/schema-tree.js +206 -0
  50. package/dist/lib/components/tag-input.js +76 -0
  51. package/dist/lib/openapi-explorer-oauth-handler.js +19 -0
  52. package/dist/lib/openapi-explorer.js +817 -0
  53. package/dist/lib/styles/advanced-search-styles.js +10 -0
  54. package/dist/lib/styles/api-request-styles.js +10 -0
  55. package/dist/lib/styles/border-styles.js +10 -0
  56. package/dist/lib/styles/endpoint-styles.js +10 -0
  57. package/dist/lib/styles/flex-styles.js +10 -0
  58. package/dist/lib/styles/font-styles.js +10 -0
  59. package/dist/lib/styles/info-styles.js +10 -0
  60. package/dist/lib/styles/input-styles.js +11 -0
  61. package/dist/lib/styles/nav-styles.js +10 -0
  62. package/dist/lib/styles/prism-styles.js +10 -0
  63. package/dist/lib/styles/schema-styles.js +10 -0
  64. package/dist/lib/styles/tab-styles.js +10 -0
  65. package/dist/lib/styles/table-styles.js +10 -0
  66. package/dist/lib/templates/advance-search-template.js +42 -0
  67. package/dist/lib/templates/callback-template.js +12 -0
  68. package/dist/lib/templates/code-samples-template.js +36 -0
  69. package/dist/lib/templates/components-template.js +27 -0
  70. package/dist/lib/templates/endpoint-template.js +111 -0
  71. package/dist/lib/templates/expanded-endpoint-template.js +48 -0
  72. package/dist/lib/templates/focused-endpoint-template.js +95 -0
  73. package/dist/lib/templates/navbar-template.js +54 -0
  74. package/dist/lib/templates/overview-template.js +16 -0
  75. package/dist/lib/templates/responsiveViewMainBodyTemplate.js +47 -0
  76. package/dist/lib/templates/security-scheme-template.js +342 -0
  77. package/dist/lib/templates/server-template.js +49 -0
  78. package/dist/lib/utils/color-utils.js +112 -0
  79. package/dist/lib/utils/common-utils.js +156 -0
  80. package/dist/lib/utils/schema-utils.js +743 -0
  81. package/dist/lib/utils/spec-parser.js +361 -0
  82. package/dist/lib/utils/theme.js +84 -0
  83. package/dist/lib/utils/xml/xml.js +239 -0
  84. package/package.json +19 -6
  85. package/dist/openapi-explorer.min.js.LICENSE.txt +0 -71
  86. package/dist/openapi-explorer.min.js.LICENSE.txt.gz +0 -0
  87. package/dist/openapi-explorer.min.js.gz +0 -0
  88. package/dist/openapi-explorer.min.js.map.gz +0 -0
  89. package/dist/report.html +0 -38
  90. package/src/components/api-request.js +0 -1244
  91. package/src/components/api-response.js +0 -340
  92. package/src/components/json-tree.js +0 -129
  93. package/src/components/schema-table.js +0 -250
  94. package/src/components/schema-tree.js +0 -280
  95. package/src/components/tag-input.js +0 -109
  96. package/src/styles/advanced-search-styles.js +0 -84
  97. package/src/styles/api-request-styles.js +0 -111
  98. package/src/styles/border-styles.js +0 -24
  99. package/src/styles/css/main.css +0 -24
  100. package/src/styles/endpoint-styles.js +0 -222
  101. package/src/styles/flex-styles.js +0 -15
  102. package/src/styles/font-styles.js +0 -266
  103. package/src/styles/info-styles.js +0 -20
  104. package/src/styles/input-styles.js +0 -236
  105. package/src/styles/nav-styles.js +0 -141
  106. package/src/styles/prism-styles.js +0 -107
  107. package/src/styles/schema-styles.js +0 -121
  108. package/src/styles/tab-styles.js +0 -44
  109. package/src/styles/table-styles.js +0 -48
  110. package/src/templates/advance-search-template.js +0 -81
  111. package/src/templates/callback-template.js +0 -63
  112. package/src/templates/code-samples-template.js +0 -35
  113. package/src/templates/components-template.js +0 -43
  114. package/src/templates/endpoint-template.js +0 -175
  115. package/src/templates/expanded-endpoint-template.js +0 -104
  116. package/src/templates/navbar-template.js +0 -175
  117. package/src/templates/overview-template.js +0 -58
  118. package/src/templates/responsiveViewMainBodyTemplate.js +0 -72
  119. package/src/templates/security-scheme-template.js +0 -487
  120. package/src/templates/server-template.js +0 -106
  121. package/src/utils/theme.js +0 -163
@@ -0,0 +1,743 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.generateExample = generateExample;
5
+ exports.getExampleValuesFromSchema = getExampleValuesFromSchema;
6
+ exports.getSampleValueByType = getSampleValueByType;
7
+ exports.getTypeInfo = getTypeInfo;
8
+ exports.schemaInObjectNotation = schemaInObjectNotation;
9
+
10
+ var _lodash = _interopRequireDefault(require("lodash.clonedeep"));
11
+
12
+ var _regexToStrings = require("regex-to-strings");
13
+
14
+ var _xml = _interopRequireDefault(require("./xml/xml"));
15
+
16
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
+
18
+ // When the type is not known for a property set the displayed type to be this:
19
+ const IS_MISSING_TYPE_INFO_TYPE = '';
20
+ /* Generates an schema object containing type and constraint info */
21
+
22
+ function getTypeInfo(schema) {
23
+ if (!schema) {
24
+ return undefined;
25
+ }
26
+
27
+ let dataType = '';
28
+ let constraint = '';
29
+
30
+ if (schema.$ref) {
31
+ const n = schema.$ref.lastIndexOf('/');
32
+ const schemaNode = schema.$ref.substring(n + 1);
33
+ dataType = `{recursive: ${schemaNode}} `;
34
+ } else if (schema.type) {
35
+ const arraySchema = Array.isArray(schema.type) ? schema.type : typeof schema.type === 'string' ? schema.type.split('┃') : schema.type;
36
+ dataType = Array.isArray(arraySchema) ? arraySchema.filter(s => s !== 'null').join('┃') : schema.type;
37
+
38
+ if (schema.format || schema.enum) {
39
+ dataType = dataType.replace('string', schema.enum ? 'enum' : schema.format);
40
+ }
41
+ } else {
42
+ dataType = IS_MISSING_TYPE_INFO_TYPE;
43
+ }
44
+
45
+ const info = {
46
+ type: dataType,
47
+ format: schema.format || '',
48
+ pattern: schema.pattern && !schema.enum ? schema.pattern : '',
49
+ readOrWriteOnly: schema.readOnly && '🆁' || schema.writeOnly && '🆆' || '',
50
+ deprecated: !!schema.deprecated,
51
+ example: Array.isArray(schema.example) ? schema.example : typeof schema.example !== 'undefined' ? `${schema.example}` : '',
52
+ default: schema.default || '',
53
+ title: schema.title || '',
54
+ description: schema.description || '',
55
+ constraint: '',
56
+ allowedValues: '',
57
+ arrayType: '',
58
+ html: ''
59
+ };
60
+
61
+ if (info.type === '{recursive}') {
62
+ info.description = schema.$ref.substring(schema.$ref.lastIndexOf('/') + 1);
63
+ } else if (info.type === IS_MISSING_TYPE_INFO_TYPE) {
64
+ info.description = info.description || '';
65
+ } // Set Allowed Values
66
+
67
+
68
+ info.allowedValues = Array.isArray(schema.enum) ? schema.enum.join('┃') : '';
69
+
70
+ if (dataType === 'array' && schema.items) {
71
+ var _schema$items$default;
72
+
73
+ const arrayItemType = schema.items.type;
74
+ const arrayItemDefault = (_schema$items$default = schema.items.default) !== null && _schema$items$default !== void 0 ? _schema$items$default : '';
75
+ info.arrayType = `${schema.type} of ${Array.isArray(arrayItemType) ? arrayItemType.join('') : arrayItemType}`;
76
+ info.default = arrayItemDefault;
77
+ info.allowedValues = Array.isArray(schema.items.enum) ? schema.items.enum.join('┃') : '';
78
+ }
79
+
80
+ if (dataType.match(/integer|number/g)) {
81
+ var _schema$minimum, _schema$maximum;
82
+
83
+ const minimum = (_schema$minimum = schema.minimum) !== null && _schema$minimum !== void 0 ? _schema$minimum : schema.exclusiveMinimum;
84
+ const maximum = (_schema$maximum = schema.maximum) !== null && _schema$maximum !== void 0 ? _schema$maximum : schema.exclusiveMaximum;
85
+ const leftBound = schema.minimum !== undefined ? '[' : '(';
86
+ const rightBound = schema.maximum !== undefined ? ']' : ')';
87
+
88
+ if (typeof minimum === 'number' || typeof maximum === 'number') {
89
+ constraint = `Range: ${leftBound}${minimum !== null && minimum !== void 0 ? minimum : ''},${maximum !== null && maximum !== void 0 ? maximum : ''}${rightBound}`;
90
+ }
91
+
92
+ if (schema.multipleOf !== undefined) {
93
+ constraint += `${constraint ? ', ' : ''}Multiples: ${schema.multipleOf}`;
94
+ }
95
+ }
96
+
97
+ if (dataType.match(/string/g)) {
98
+ if (schema.minLength !== undefined && schema.maxLength !== undefined) {
99
+ constraint += `Min length: ${schema.minLength}, Max length: ${schema.maxLength}`;
100
+ } else if (schema.minLength !== undefined) {
101
+ constraint += `Min length: ${schema.minLength}`;
102
+ } else if (schema.maxLength !== undefined) {
103
+ constraint += `Max length: ${schema.maxLength}`;
104
+ }
105
+ }
106
+
107
+ info.constraint = constraint;
108
+ info.html = JSON.stringify({
109
+ type: info.type,
110
+ readOrWriteOnly: info.readOrWriteOnly,
111
+ constraint: info.constraint,
112
+ defaultValue: info.default,
113
+ allowedValues: info.allowedValues,
114
+ pattern: info.pattern,
115
+ schemaDescription: info.description,
116
+ schemaTitle: info.title,
117
+ deprecated: info.deprecated
118
+ });
119
+ return info;
120
+ }
121
+
122
+ function getSampleValueByType(schemaObj, fallbackPropertyName, skipExampleStrings) {
123
+ var _schemaObj$type;
124
+
125
+ const example = schemaObj.examples ? schemaObj.examples[0] : schemaObj.example;
126
+
127
+ if (example === '') {
128
+ return '';
129
+ }
130
+
131
+ if (example === null) {
132
+ return null;
133
+ }
134
+
135
+ if (example === 0) {
136
+ return 0;
137
+ }
138
+
139
+ if (skipExampleStrings && typeof example === 'string') {
140
+ return '';
141
+ }
142
+
143
+ if (example) {
144
+ return example;
145
+ }
146
+
147
+ if (schemaObj.default) {
148
+ return schemaObj.default;
149
+ }
150
+
151
+ if (Object.keys(schemaObj).length === 0) {
152
+ return null;
153
+ }
154
+
155
+ if (schemaObj.$ref) {
156
+ // Indicates a Circular ref
157
+ return schemaObj.$ref;
158
+ }
159
+
160
+ const typeValue = Array.isArray(schemaObj.type) ? schemaObj.type.filter(t => t !== 'null')[0] : (_schemaObj$type = schemaObj.type) !== null && _schemaObj$type !== void 0 ? _schemaObj$type : '';
161
+
162
+ if (typeValue.match(/^integer|^number/g)) {
163
+ const multipleOf = Number.isNaN(Number(schemaObj.multipleOf)) ? undefined : Number(schemaObj.multipleOf);
164
+ const maximum = Number.isNaN(Number(schemaObj.maximum)) ? undefined : Number(schemaObj.maximum);
165
+ const minimumPossibleVal = Number.isNaN(Number(schemaObj.minimum)) ? Number.isNaN(Number(schemaObj.exclusiveMinimum)) ? maximum || 0 : Number(schemaObj.exclusiveMinimum) + (typeValue.startsWith('integer') ? 1 : 0.001) : Number(schemaObj.minimum);
166
+ const finalVal = multipleOf ? multipleOf >= minimumPossibleVal ? multipleOf : minimumPossibleVal % multipleOf === 0 ? minimumPossibleVal : Math.ceil(minimumPossibleVal / multipleOf) * multipleOf : minimumPossibleVal;
167
+ return finalVal;
168
+ }
169
+
170
+ if (typeValue.match(/^boolean/g)) {
171
+ return false;
172
+ }
173
+
174
+ if (typeValue.match(/^null/g)) {
175
+ return null;
176
+ }
177
+
178
+ if (skipExampleStrings && typeValue.match(/^string/g)) {
179
+ return '';
180
+ }
181
+
182
+ if (typeValue.match(/^string/g)) {
183
+ if (schemaObj.enum) {
184
+ return schemaObj.enum[0];
185
+ }
186
+
187
+ if (schemaObj.pattern) {
188
+ const examplePattern = schemaObj.pattern.replace(/[+*](?![^\][]*[\]])/g, '{8}').replace(/\{\d*,(\d+)?\}/g, '{8}');
189
+ return (0, _regexToStrings.expandN)(examplePattern, 1)[0] || fallbackPropertyName || 'string';
190
+ }
191
+
192
+ if (schemaObj.format) {
193
+ switch (schemaObj.format.toLowerCase()) {
194
+ case 'url':
195
+ return 'https://example.com';
196
+
197
+ case 'uri':
198
+ return 'urn:namespace:type:example/resource';
199
+
200
+ case 'date':
201
+ return new Date().toISOString().split('T')[0];
202
+
203
+ case 'time':
204
+ return new Date().toISOString().split('T')[1];
205
+
206
+ case 'date-time':
207
+ return new Date().toISOString();
208
+
209
+ case 'duration':
210
+ return 'P3Y6M4DT12H30M5S';
211
+ // P=Period 3-Years 6-Months 4-Days 12-Hours 30-Minutes 5-Seconds
212
+
213
+ case 'email':
214
+ case 'idn-email':
215
+ return 'user@example.com';
216
+
217
+ case 'hostname':
218
+ case 'idn-hostname':
219
+ return 'www.example.com';
220
+
221
+ case 'ipv4':
222
+ return '192.168.0.1';
223
+
224
+ case 'ipv6':
225
+ return '2001:0db8:5b96:0000:0000:426f:8e17:642a';
226
+
227
+ case 'uuid':
228
+ return '4e0ba220-9575-11eb-a8b3-0242ac130003';
229
+
230
+ default:
231
+ return schemaObj.format;
232
+ }
233
+ } else {
234
+ return fallbackPropertyName || 'string';
235
+ }
236
+ } // If type cannot be determined
237
+
238
+
239
+ return '?';
240
+ }
241
+
242
+ function duplicateExampleWithNewPropertyValues(objectExamples, propertyName, propertyValues) {
243
+ // Limit max number of property examples to 2 and limit the max number of examples to 10
244
+ return objectExamples.reduce((exampleList, example) => {
245
+ const examplesFromPropertyValues = propertyValues.slice(0, 2).map(value => ({ ...(0, _lodash.default)(example),
246
+ [propertyName]: value
247
+ }));
248
+ return exampleList.concat(...examplesFromPropertyValues);
249
+ }, []).slice(0, 10);
250
+ }
251
+
252
+ function getExampleValuesFromSchema(schema, config = {}) {
253
+ // 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.
254
+ if (config.xml) {
255
+ const xmlResult = getExampleValuesFromSchemaRecursive(schema.type === 'object' ? {
256
+ properties: {
257
+ _root: schema
258
+ }
259
+ } : schema, config);
260
+ return xmlResult.map(example => example[0]);
261
+ }
262
+
263
+ return getExampleValuesFromSchemaRecursive(schema, config);
264
+ } // TODO: Support getting the `summary` from the examples object or the `title` from the schema object
265
+
266
+
267
+ function getExampleValuesFromSchemaRecursive(schema, config = {}) {
268
+ if (!schema) {
269
+ return [];
270
+ } // XML Support
271
+
272
+
273
+ const xmlAttributes = {};
274
+ const xmlTagProperties = [];
275
+ const {
276
+ prefix,
277
+ namespace
278
+ } = schema.xml || {};
279
+
280
+ if (namespace) {
281
+ xmlAttributes[prefix ? `xmlns:${prefix}` : 'xmlns'] = namespace;
282
+ }
283
+
284
+ const nodeName = schema.items && schema.items.xml && schema.items.xml.name || schema.xml && schema.xml.name || config.propertyName || 'root';
285
+ const overridePropertyName = prefix ? `${prefix}:${nodeName}` : nodeName;
286
+
287
+ if (schema.allOf) {
288
+ // TODO: allOf support should include merging the schemas together
289
+ return getExampleValuesFromSchemaRecursive(schema.allOf[0], config);
290
+ }
291
+
292
+ if (schema.oneOf) {
293
+ return schema.oneOf.map(s => getExampleValuesFromSchemaRecursive(s, config)).flat(1);
294
+ }
295
+
296
+ if (schema.anyOf) {
297
+ return schema.anyOf.map(s => getExampleValuesFromSchemaRecursive(s, config)).flat(1);
298
+ }
299
+
300
+ if (schema.type === 'array' || schema.items) {
301
+ if (!config.xml) {
302
+ return [getExampleValuesFromSchemaRecursive(schema.items || {}, config)];
303
+ }
304
+
305
+ if (!schema.xml || !schema.xml.wrapped) {
306
+ const arrayExamples = getExampleValuesFromSchemaRecursive(schema.items || {}, config);
307
+ xmlTagProperties.push({
308
+ [overridePropertyName]: arrayExamples[0]
309
+ }, {
310
+ _attr: xmlAttributes
311
+ });
312
+ return [xmlTagProperties];
313
+ }
314
+
315
+ const arrayExamples = getExampleValuesFromSchemaRecursive(schema.items || {}, { ...config,
316
+ propertyName: overridePropertyName
317
+ });
318
+ xmlTagProperties.push({
319
+ [overridePropertyName]: arrayExamples[0]
320
+ }, {
321
+ _attr: xmlAttributes
322
+ });
323
+ return [xmlTagProperties];
324
+ }
325
+
326
+ if (schema.type === 'object' || schema.properties) {
327
+ let objectExamples = [{}];
328
+ Object.keys(schema.properties || {}).forEach(propertyName => {
329
+ const innerSchema = schema.properties[propertyName];
330
+
331
+ if (innerSchema.deprecated) {
332
+ return;
333
+ }
334
+
335
+ if (innerSchema.readOnly && !config.includeReadOnly) {
336
+ return;
337
+ }
338
+
339
+ if (innerSchema.writeOnly && !config.includeWriteOnly) {
340
+ return;
341
+ }
342
+
343
+ const propertyExamples = getExampleValuesFromSchemaRecursive(innerSchema, { ...config,
344
+ propertyName
345
+ });
346
+ objectExamples = duplicateExampleWithNewPropertyValues(objectExamples, propertyName, propertyExamples);
347
+
348
+ if (innerSchema.xml && innerSchema.xml.namespace) {
349
+ xmlAttributes[innerSchema.xml.prefix ? `xmlns:${innerSchema.xml.prefix}` : 'xmlns'] = namespace;
350
+ }
351
+
352
+ const innerNodeName = innerSchema.xml && innerSchema.xml.name || propertyName || config.propertyName;
353
+ const innerOverridePropertyName = prefix ? `${prefix}:${innerNodeName}` : innerNodeName;
354
+
355
+ if (innerSchema.xml && innerSchema.xml.attribute) {
356
+ xmlAttributes[innerOverridePropertyName] = propertyExamples[0];
357
+ } else {
358
+ xmlTagProperties.push({
359
+ [innerOverridePropertyName]: propertyExamples[0]
360
+ });
361
+ }
362
+ });
363
+
364
+ if (Object.keys(xmlAttributes).length) {
365
+ xmlTagProperties.push({
366
+ _attr: xmlAttributes
367
+ });
368
+ }
369
+
370
+ return config.xml ? [xmlTagProperties] : objectExamples;
371
+ }
372
+
373
+ const value = getSampleValueByType(schema, config.propertyName, config.skipExampleStrings);
374
+ return [value];
375
+ }
376
+
377
+ function removeTitlesAndDescriptions(obj) {
378
+ if (typeof obj !== 'object' || obj === null) {
379
+ return;
380
+ }
381
+
382
+ delete obj['::TITLE'];
383
+ delete obj['::DESCRIPTION'];
384
+
385
+ for (const k in obj) {
386
+ removeTitlesAndDescriptions(obj[k]);
387
+ }
388
+ }
389
+ /**
390
+ * For changing OpenAPI-Schema to an Object Notation,
391
+ * This Object would further be an input to UI Components to generate an Object-Tree
392
+ * @param {object} schema - Schema object from OpenAPI spec
393
+ * @param {object} obj - recursively pass this object to generate object notation
394
+ * @param {number} level - recursion level
395
+ * @param {string} suffix - used for suffixing property names to avoid duplicate props during object composition
396
+ */
397
+
398
+
399
+ function schemaInObjectNotation(schema, obj, level = 0, suffix = '') {
400
+ if (!schema) {
401
+ return undefined;
402
+ }
403
+
404
+ if (schema.allOf) {
405
+ const objWithAllProps = {};
406
+
407
+ if (schema.allOf.length === 1 && !schema.allOf[0].properties && !schema.allOf[0].items) {
408
+ // If allOf has single item and the type is not an object or array, then its a primitive
409
+ const tempSchema = schema.allOf[0];
410
+ return `${getTypeInfo(tempSchema).html}`;
411
+ } // If allOf is an array of multiple elements, then all the keys makes a single object
412
+
413
+
414
+ schema.allOf.map((v, i) => {
415
+ if (v.type === 'object' || v.properties || v.allOf || v.anyOf || v.oneOf) {
416
+ const propSuffix = (v.anyOf || v.oneOf) && i > 0 ? i : '';
417
+ const partialObj = schemaInObjectNotation(v, {}, level + 1, propSuffix);
418
+ Object.assign(objWithAllProps, partialObj);
419
+ } else if (v.type === 'array' || v.items) {
420
+ const partialObj = schemaInObjectNotation(v, {}, level + 1);
421
+ Object.assign(objWithAllProps, partialObj);
422
+ } else if (v.type) {
423
+ const prop = `prop${Object.keys(objWithAllProps).length}`;
424
+ const typeObj = getTypeInfo(v);
425
+ objWithAllProps[prop] = `${typeObj.html}`;
426
+ }
427
+ }); // eslint-disable-next-line no-param-reassign
428
+
429
+ obj = objWithAllProps;
430
+ } else if (schema.anyOf || schema.oneOf) {
431
+ obj['::description'] = schema.description || ''; // 1. First iterate the regular properties
432
+
433
+ if (schema.type === 'object' || schema.properties) {
434
+ obj['::description'] = schema.description || '';
435
+ obj['::flags'] = {
436
+ '🆁': schema.readOnly && '🆁',
437
+ '🆆': schema.writeOnly && '🆆'
438
+ };
439
+ obj['::type'] = 'object'; // obj['::deprecated'] = schema.deprecated || false;
440
+
441
+ for (const key in schema.properties) {
442
+ if (schema.required && schema.required.includes(key)) {
443
+ obj[`${key}*`] = schemaInObjectNotation(schema.properties[key], {}, level + 1);
444
+ } else {
445
+ obj[key] = schemaInObjectNotation(schema.properties[key], {}, level + 1);
446
+ }
447
+ }
448
+ } // 2. Then show allof/anyof objects
449
+
450
+
451
+ const objWithAnyOfProps = {};
452
+ const xxxOf = schema.anyOf ? 'anyOf' : 'oneOf';
453
+ schema[xxxOf].forEach((v, index) => {
454
+ if (v.type === 'object' || v.properties || v.allOf || v.anyOf || v.oneOf) {
455
+ const partialObj = schemaInObjectNotation(v, {});
456
+ objWithAnyOfProps[`::OPTION~${index + 1}${v.title ? `~${v.title}` : ''}`] = partialObj;
457
+ objWithAnyOfProps['::type'] = 'xxx-of-option';
458
+ } else if (v.type === 'array' || v.items) {
459
+ // This else-if block never seems to get executed
460
+ const partialObj = schemaInObjectNotation(v, {});
461
+ objWithAnyOfProps[`::OPTION~${index + 1}${v.title ? `~${v.title}` : ''}`] = partialObj;
462
+ objWithAnyOfProps['::type'] = 'xxx-of-array';
463
+ } else {
464
+ const prop = `::OPTION~${index + 1}${v.title ? `~${v.title}` : ''}`;
465
+ objWithAnyOfProps[prop] = `${getTypeInfo(v).html}`;
466
+ objWithAnyOfProps['::type'] = 'xxx-of-option';
467
+ }
468
+ });
469
+ obj[schema.anyOf ? `::ANY~OF ${suffix}` : `::ONE~OF ${suffix}`] = objWithAnyOfProps;
470
+ obj['::type'] = 'xxx-of';
471
+ } else if (Array.isArray(schema.type)) {
472
+ // 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
473
+ // Clone the schema - as it will be modified to replace multi-data-types with one-of;
474
+ const subSchema = JSON.parse(JSON.stringify(schema));
475
+ const primitiveType = [];
476
+ const complexTypes = [];
477
+ subSchema.type.forEach(v => {
478
+ if (v.match(/integer|number|string|null|boolean/g)) {
479
+ primitiveType.push(v);
480
+ } else if (v === 'array' && typeof (subSchema.items && subSchema.items.type) === 'string' && schema.items && subSchema.items.type.match(/integer|number|string|null|boolean/g)) {
481
+ // Array with primitive types should also be treated as primitive type
482
+ if (subSchema.items.type === 'string' && subSchema.items.format) {
483
+ primitiveType.push(`${subSchema.items.format}[]`);
484
+ } else {
485
+ primitiveType.push(`${subSchema.items.type}[]`);
486
+ }
487
+ } else {
488
+ complexTypes.push(v);
489
+ }
490
+ });
491
+ let multiPrimitiveTypes;
492
+
493
+ if (primitiveType.length > 0) {
494
+ subSchema.type = primitiveType.join('┃');
495
+ multiPrimitiveTypes = getTypeInfo(subSchema);
496
+
497
+ if (complexTypes.length === 0) {
498
+ return `${multiPrimitiveTypes && multiPrimitiveTypes.html || ''}`;
499
+ }
500
+ }
501
+
502
+ if (complexTypes.length > 0) {
503
+ obj['::type'] = 'xxx-of';
504
+ const multiTypeOptions = {
505
+ '::type': 'xxx-of-option'
506
+ }; // Generate ONE-OF options for complexTypes
507
+
508
+ complexTypes.forEach((v, i) => {
509
+ if (v === 'null') {
510
+ multiTypeOptions[`::OPTION~${i + 1}`] = 'NULL~|~~|~~|~~|~~|~~|~~|~~|~';
511
+ } else if ('integer, number, string, boolean,'.includes(`${v},`)) {
512
+ subSchema.type = Array.isArray(v) ? v.join('┃') : v;
513
+ const primitiveTypeInfo = getTypeInfo(subSchema);
514
+ multiTypeOptions[`::OPTION~${i + 1}`] = primitiveTypeInfo.html;
515
+ } else if (v === 'object') {
516
+ // If object type iterate all the properties and create an object-type-option
517
+ const objTypeOption = {
518
+ '::title': schema.title || '',
519
+ '::description': schema.description || '',
520
+ '::flags': {
521
+ '🆁': schema.readOnly && '🆁',
522
+ '🆆': schema.writeOnly && '🆆'
523
+ },
524
+ '::type': 'object',
525
+ '::deprecated': schema.deprecated || false
526
+ };
527
+
528
+ for (const key in schema.properties) {
529
+ if (schema.required && schema.required.includes(key)) {
530
+ objTypeOption[`${key}*`] = schemaInObjectNotation(schema.properties[key], {}, level + 1);
531
+ } else {
532
+ objTypeOption[key] = schemaInObjectNotation(schema.properties[key], {}, level + 1);
533
+ }
534
+ }
535
+
536
+ multiTypeOptions[`::OPTION~${i + 1}`] = objTypeOption;
537
+ } else if (v === 'array') {
538
+ multiTypeOptions[`::OPTION~${i + 1}`] = {
539
+ '::title': schema.title || '',
540
+ '::description': schema.description || '',
541
+ '::flags': {
542
+ '🆁': schema.readOnly && '🆁',
543
+ '🆆': schema.writeOnly && '🆆'
544
+ },
545
+ '::type': 'array',
546
+ '::props': schemaInObjectNotation(Object.assign({
547
+ readOnly: schema.readOnly,
548
+ writeOnly: schema.writeOnly
549
+ }, schema.items), {}, level + 1)
550
+ };
551
+ }
552
+ });
553
+ multiTypeOptions[`::OPTION~${complexTypes.length + 1}`] = multiPrimitiveTypes && multiPrimitiveTypes.html || '';
554
+ obj['::ONE~OF'] = multiTypeOptions;
555
+ }
556
+ } else if (schema.type === 'object' || schema.properties) {
557
+ obj['::title'] = schema.title || '';
558
+ obj['::description'] = schema.description || '';
559
+ obj['::flags'] = {
560
+ '🆁': schema.readOnly && '🆁',
561
+ '🆆': schema.writeOnly && '🆆'
562
+ };
563
+ obj['::type'] = 'object';
564
+ obj['::deprecated'] = schema.deprecated || false;
565
+
566
+ for (const key in schema.properties) {
567
+ if (schema.required && schema.required.includes(key)) {
568
+ obj[`${key}*`] = schemaInObjectNotation(schema.properties[key], {}, level + 1);
569
+ } else {
570
+ obj[key] = schemaInObjectNotation(schema.properties[key], {}, level + 1);
571
+ }
572
+ }
573
+
574
+ if (schema.additionalProperties) {
575
+ obj['<any-key>'] = schemaInObjectNotation(schema.additionalProperties, {});
576
+ }
577
+ } else if (schema.type === 'array' || schema.items) {
578
+ // If Array
579
+ obj['::title'] = schema.title || '';
580
+ obj['::description'] = schema.description ? schema.description : schema.items && schema.items.description ? `array&lt;${schema.items.description}&gt;` : '';
581
+ obj['::flags'] = {
582
+ '🆁': schema.readOnly && '🆁',
583
+ '🆆': schema.writeOnly && '🆆'
584
+ };
585
+ obj['::type'] = 'array';
586
+ obj['::deprecated'] = schema.deprecated || false;
587
+ obj['::props'] = schemaInObjectNotation(Object.assign({
588
+ deprecated: schema.deprecated,
589
+ readOnly: schema.readOnly,
590
+ writeOnly: schema.writeOnly
591
+ }, schema.items), {}, level + 1);
592
+ } else {
593
+ const typeObj = getTypeInfo(schema);
594
+
595
+ if (typeObj && typeObj.html) {
596
+ return `${typeObj.html}`;
597
+ }
598
+
599
+ return '';
600
+ }
601
+
602
+ return obj;
603
+ }
604
+ /* Create Example object */
605
+
606
+
607
+ function generateExample(examples, example, schema, rawMimeType, includeReadOnly = true, includeWriteOnly = true, outputType, skipExampleStrings = false) {
608
+ const mimeType = rawMimeType || 'application/json';
609
+ const finalExamples = []; // First check if examples is provided
610
+
611
+ if (examples) {
612
+ for (const eg in examples) {
613
+ let egContent = '';
614
+ let egFormat = 'json';
615
+
616
+ if (mimeType.toLowerCase().includes('json')) {
617
+ if (outputType === 'text') {
618
+ egContent = typeof examples[eg].value === 'string' ? examples[eg].value : JSON.stringify(examples[eg].value, undefined, 2);
619
+ egFormat = 'text';
620
+ } else {
621
+ egContent = examples[eg].value;
622
+
623
+ if (typeof examples[eg].value === 'string') {
624
+ try {
625
+ const fixedJsonString = examples[eg].value.replace(/([\w]+)(:)/g, '"$1"$2').replace(/'/g, '"');
626
+ egContent = JSON.parse(fixedJsonString);
627
+ egFormat = 'json';
628
+ } catch (err) {
629
+ egFormat = 'text';
630
+ egContent = examples[eg].value;
631
+ }
632
+ }
633
+ }
634
+ } else {
635
+ egContent = examples[eg].value;
636
+ egFormat = 'text';
637
+ }
638
+
639
+ finalExamples.push({
640
+ exampleId: eg,
641
+ exampleSummary: examples[eg].summary || eg,
642
+ exampleDescription: examples[eg].description || '',
643
+ exampleType: mimeType,
644
+ exampleValue: egContent,
645
+ exampleFormat: egFormat
646
+ });
647
+ }
648
+ } else if (example) {
649
+ let egContent = '';
650
+ let egFormat = 'json';
651
+
652
+ if (mimeType.toLowerCase().includes('json')) {
653
+ if (outputType === 'text') {
654
+ egContent = typeof example === 'string' ? example : JSON.stringify(example, undefined, 2);
655
+ egFormat = 'text';
656
+ } else if (typeof example === 'object') {
657
+ egContent = example;
658
+ egFormat = 'json';
659
+ } else if (typeof example === 'string') {
660
+ try {
661
+ egContent = JSON.parse(example);
662
+ egFormat = 'json';
663
+ } catch (err) {
664
+ egFormat = 'text';
665
+ egContent = example;
666
+ }
667
+ }
668
+ } else {
669
+ egContent = example;
670
+ egFormat = 'text';
671
+ }
672
+
673
+ finalExamples.push({
674
+ exampleId: 'Example',
675
+ exampleSummary: '',
676
+ exampleDescription: '',
677
+ exampleType: mimeType,
678
+ exampleValue: egContent,
679
+ exampleFormat: egFormat
680
+ });
681
+ } // If schema-level examples are not provided then generate one based on the schema field types
682
+
683
+
684
+ if (finalExamples.length) {
685
+ return finalExamples;
686
+ }
687
+
688
+ if (schema && schema.example) {
689
+ // Note: schema.examples (plurals) is not allowed as per spec
690
+ return [{
691
+ exampleId: 'Example',
692
+ exampleSummary: '',
693
+ exampleDescription: '',
694
+ exampleType: mimeType,
695
+ exampleValue: schema.example,
696
+ exampleFormat: mimeType.toLowerCase().includes('json') && typeof schema.example === 'object' ? 'json' : 'text'
697
+ }];
698
+ }
699
+
700
+ const config = {
701
+ includeReadOnly,
702
+ includeWriteOnly,
703
+ skipExampleStrings,
704
+ xml: mimeType.toLowerCase().includes('xml')
705
+ };
706
+ const samples = getExampleValuesFromSchema(schema, config);
707
+
708
+ if (!samples || !mimeType.toLowerCase().includes('json') && !mimeType.toLowerCase().includes('text') && !mimeType.toLowerCase().includes('*/*') && !mimeType.toLowerCase().includes('xml')) {
709
+ return [{
710
+ exampleId: 'Example',
711
+ exampleSummary: '',
712
+ exampleDescription: '',
713
+ exampleType: mimeType,
714
+ exampleValue: '',
715
+ exampleFormat: 'text'
716
+ }];
717
+ }
718
+
719
+ return samples.map((sample, sampleCounter) => {
720
+ const summary = sample['::TITLE'] || `Example ${sampleCounter + 1}`;
721
+ const description = sample['::DESCRIPTION'] || '';
722
+ removeTitlesAndDescriptions(sample);
723
+ let exampleValue = '';
724
+
725
+ if (mimeType.toLowerCase().includes('xml')) {
726
+ exampleValue = (0, _xml.default)(sample, {
727
+ declaration: true,
728
+ indent: ' '
729
+ });
730
+ } else {
731
+ exampleValue = outputType === 'text' ? JSON.stringify(sample, null, 8) : sample;
732
+ }
733
+
734
+ return {
735
+ exampleId: `Example-${sampleCounter}`,
736
+ exampleSummary: summary,
737
+ exampleDescription: description,
738
+ exampleType: mimeType,
739
+ exampleFormat: mimeType.toLowerCase().includes('xml') ? 'text' : outputType,
740
+ exampleValue
741
+ };
742
+ }).filter(s => s);
743
+ }