-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathgenerateSchemaExample.ts
522 lines (464 loc) · 17.1 KB
/
generateSchemaExample.ts
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
import type { OpenAPIV3 } from '@gitbook/openapi-parser';
import { checkIsReference } from './utils';
type JSONValue = string | number | boolean | null | JSONValue[] | { [key: string]: JSONValue };
type ScalarGetExampleFromSchemaOptions = NonNullable<Parameters<typeof getExampleFromSchema>[1]>;
type GenerateSchemaExampleOptions = Pick<
ScalarGetExampleFromSchemaOptions,
'xml' | 'omitEmptyAndOptionalProperties' | 'mode'
>;
/**
* Generate a JSON example from a schema
*/
export function generateSchemaExample(
schema: OpenAPIV3.SchemaObject,
options?: GenerateSchemaExampleOptions
): JSONValue | undefined {
return getExampleFromSchema(schema, {
emptyString: 'text',
...options,
});
}
/**
* Generate an example for a media type.
*/
export function generateMediaTypeExamples(
mediaType: OpenAPIV3.MediaTypeObject,
options?: GenerateSchemaExampleOptions
): OpenAPIV3.ExampleObject[] {
if (mediaType.example) {
return [{ summary: 'default', value: mediaType.example }];
}
if (mediaType.examples) {
const { examples } = mediaType;
const keys = Object.keys(examples);
if (keys.length > 0) {
return keys.reduce<OpenAPIV3.ExampleObject[]>((result, key) => {
const example = examples[key];
if (!example || checkIsReference(example)) {
return result;
}
result.push({
summary: example.summary || key,
value: example.value,
description: example.description,
externalValue: example.externalValue,
});
return result;
}, []);
}
}
if (mediaType.schema) {
return [{ summary: 'default', value: generateSchemaExample(mediaType.schema, options) }];
}
return [];
}
/** Hard limit for rendering circular references */
const MAX_LEVELS_DEEP = 5;
const genericExampleValues: Record<string, string> = {
'date-time': new Date().toISOString(),
date: new Date().toISOString().split('T')[0] ?? '1970-01-01',
email: 'name@gmail.com',
hostname: 'example.com',
ipv4: '0.0.0.0',
ipv6: '2001:0db8:85a3:0000:0000:8a2e:0370:7334',
uri: 'https://example.com',
uuid: '123e4567-e89b-12d3-a456-426614174000',
binary: 'binary',
byte: 'Ynl0ZXM=',
password: 'password',
'idn-email': 'jane.doe@example.com',
'idn-hostname': 'example.com',
'iri-reference': '/entitiy/1',
// https://tools.ietf.org/html/rfc3987
iri: 'https://example.com/entity/123',
'json-pointer': '/nested/objects',
regex: '/[a-z]/',
// https://tools.ietf.org/html/draft-handrews-relative-json-pointer-01
'relative-json-pointer': '1/nested/objects',
// full-time in https://tools.ietf.org/html/rfc3339#section-5.6
time: new Date().toISOString().split('T')[1]?.split('.')[0] ?? '00:00:00Z',
// either a URI or relative-reference https://tools.ietf.org/html/rfc3986#section-4.1
'uri-reference': '../folder',
'uri-template': 'https://example.com/{id}',
'object-id': '6592008029c8c3e4dc76256c',
};
/**
* We can use the `format` to generate some random values.
*/
function guessFromFormat(schema: Record<string, any>, fallback = '') {
return genericExampleValues[schema.format] ?? fallback;
}
/**
* This function takes an OpenAPI schema and generates an example from it
* Forked from : https://github.com/scalar/scalar/blob/main/packages/oas-utils/src/spec-getters/getExampleFromSchema.ts
*/
const getExampleFromSchema = (
schema: Record<string, any>,
options?: {
/**
* The fallback string for empty string values.
* @default ''
*/
emptyString?: string;
/**
* Whether to use the XML tag names as keys
* @default false
*/
xml?: boolean;
/**
* Whether to show read-only/write-only properties. Otherwise all properties are shown.
* @default undefined
*/
mode?: 'read' | 'write';
/**
* Dynamic values to add to the example.
*/
variables?: Record<string, any>;
/**
* Whether to omit empty and optional properties.
* @default false
*/
omitEmptyAndOptionalProperties?: boolean;
},
level = 0,
parentSchema?: Record<string, any>,
name?: string,
resultCache = new WeakMap<Record<string, any>, any>()
): any => {
// Store result in the cache, and return the result
function cache(schema: Record<string, any>, result: unknown) {
// Avoid unnecessary WeakMap operations for primitive values
if (typeof result !== 'object' || result === null) {
return result;
}
resultCache.set(schema, result);
return result;
}
// Check if the result is already cached
if (resultCache.has(schema)) {
return resultCache.get(schema);
}
// Check whether it’s a circular reference
if (level === MAX_LEVELS_DEEP + 1) {
try {
// Fails if it contains a circular reference
JSON.stringify(schema);
} catch {
return '[Circular Reference]';
}
}
// Sometimes, we just want the structure and no values.
// But if `emptyString` is set, we do want to see some values.
const makeUpRandomData = !!options?.emptyString;
// If the property is deprecated we don't show it in examples.
if (schema.deprecated) {
return undefined;
}
// Check if the property is read-only/write-only
if (
(options?.mode === 'write' && schema.readOnly) ||
(options?.mode === 'read' && schema.writeOnly)
) {
return undefined;
}
// Use given variables as values
if (schema['x-variable']) {
const value = options?.variables?.[schema['x-variable']];
// Return the value if it’s defined
if (value !== undefined) {
// Type-casting
if (schema.type === 'number' || schema.type === 'integer') {
return Number.parseInt(value, 10);
}
return cache(schema, value);
}
}
// Use the first example, if there’s an array
if (Array.isArray(schema.examples) && schema.examples.length > 0) {
return cache(schema, schema.examples[0]);
}
// Use an example, if there’s one
if (schema.example !== undefined) {
return cache(schema, schema.example);
}
// enum: [ 'available', 'pending', 'sold' ]
if (Array.isArray(schema.enum) && schema.enum.length > 0) {
return cache(schema, schema.enum[0]);
}
// Check if the property is required
const isObjectOrArray =
schema.type === 'object' ||
schema.type === 'array' ||
!!schema.allOf?.at?.(0) ||
!!schema.anyOf?.at?.(0) ||
!!schema.oneOf?.at?.(0);
if (!isObjectOrArray && options?.omitEmptyAndOptionalProperties === true) {
const isRequired =
schema.required === true ||
parentSchema?.required === true ||
parentSchema?.required?.includes(name ?? schema.name);
if (!isRequired) {
return undefined;
}
}
// Object
if (schema.type === 'object' || schema.properties !== undefined) {
const response: Record<string, any> = {};
// Regular properties
if (schema.properties !== undefined) {
for (const propertyName in schema.properties) {
if (Object.prototype.hasOwnProperty.call(schema.properties, propertyName)) {
const property = schema.properties[propertyName];
const propertyXmlTagName = options?.xml ? property.xml?.name : undefined;
response[propertyXmlTagName ?? propertyName] = getExampleFromSchema(
property,
options,
level + 1,
schema,
propertyName,
resultCache
);
if (typeof response[propertyXmlTagName ?? propertyName] === 'undefined') {
delete response[propertyXmlTagName ?? propertyName];
}
}
}
}
// Pattern properties (regex)
if (schema.patternProperties !== undefined) {
for (const pattern in schema.patternProperties) {
if (Object.prototype.hasOwnProperty.call(schema.patternProperties, pattern)) {
const property = schema.patternProperties[pattern];
// Use the regex pattern as an example key
const exampleKey = pattern;
response[exampleKey] = getExampleFromSchema(
property,
options,
level + 1,
schema,
exampleKey,
resultCache
);
}
}
}
// Additional properties
if (schema.additionalProperties !== undefined) {
const anyTypeIsValid =
// true
schema.additionalProperties === true ||
// or an empty object {}
(typeof schema.additionalProperties === 'object' &&
!Object.keys(schema.additionalProperties).length);
if (anyTypeIsValid) {
response.ANY_ADDITIONAL_PROPERTY = 'anything';
} else if (schema.additionalProperties !== false) {
response.ANY_ADDITIONAL_PROPERTY = getExampleFromSchema(
schema.additionalProperties,
options,
level + 1,
undefined,
undefined,
resultCache
);
}
}
if (schema.anyOf !== undefined) {
Object.assign(
response,
getExampleFromSchema(
schema.anyOf[0],
options,
level + 1,
undefined,
undefined,
resultCache
)
);
} else if (schema.oneOf !== undefined) {
Object.assign(
response,
getExampleFromSchema(
schema.oneOf[0],
options,
level + 1,
undefined,
undefined,
resultCache
)
);
} else if (schema.allOf !== undefined) {
Object.assign(
response,
...schema.allOf
.map((item: Record<string, any>) =>
getExampleFromSchema(
item,
options,
level + 1,
schema,
undefined,
resultCache
)
)
.filter((item: any) => item !== undefined)
);
}
return cache(schema, response);
}
// Array
if (schema.type === 'array' || schema.items !== undefined) {
const itemsXmlTagName = schema?.items?.xml?.name;
const wrapItems = !!(options?.xml && schema.xml?.wrapped && itemsXmlTagName);
if (schema.example !== undefined) {
return cache(
schema,
wrapItems ? { [itemsXmlTagName]: schema.example } : schema.example
);
}
// Check whether the array has a anyOf, oneOf, or allOf rule
if (schema.items) {
// First handle allOf separately since it needs special handling
if (schema.items.allOf) {
// If the first item is an object type, merge all schemas
if (schema.items.allOf[0].type === 'object') {
const mergedExample = getExampleFromSchema(
{ type: 'object', allOf: schema.items.allOf },
options,
level + 1,
schema,
undefined,
resultCache
);
return cache(
schema,
wrapItems ? [{ [itemsXmlTagName]: mergedExample }] : [mergedExample]
);
}
// For non-objects (like strings), collect all examples
const examples = schema.items.allOf
.map((item: Record<string, any>) =>
getExampleFromSchema(
item,
options,
level + 1,
schema,
undefined,
resultCache
)
)
.filter((item: any) => item !== undefined);
return cache(
schema,
wrapItems
? examples.map((example: any) => ({ [itemsXmlTagName]: example }))
: examples
);
}
// Handle other rules (anyOf, oneOf)
const rules = ['anyOf', 'oneOf'];
for (const rule of rules) {
if (!schema.items[rule]) {
continue;
}
const schemas = schema.items[rule].slice(0, 1);
const exampleFromRule = schemas
.map((item: Record<string, any>) =>
getExampleFromSchema(
item,
options,
level + 1,
schema,
undefined,
resultCache
)
)
.filter((item: any) => item !== undefined);
return cache(
schema,
wrapItems ? [{ [itemsXmlTagName]: exampleFromRule }] : exampleFromRule
);
}
}
if (schema.items?.type) {
const exampleFromSchema = getExampleFromSchema(
schema.items,
options,
level + 1,
undefined,
undefined,
resultCache
);
return wrapItems ? [{ [itemsXmlTagName]: exampleFromSchema }] : [exampleFromSchema];
}
return [];
}
const exampleValues: Record<any, any> = {
string: makeUpRandomData ? guessFromFormat(schema, options?.emptyString) : '',
boolean: true,
integer: schema.min ?? 1,
number: schema.min ?? 1,
array: [],
};
if (schema.type !== undefined && exampleValues[schema.type] !== undefined) {
return cache(schema, exampleValues[schema.type]);
}
const discriminateSchema = schema.oneOf || schema.anyOf;
// Check if property has the `oneOf` | `anyOf` key
if (Array.isArray(discriminateSchema) && discriminateSchema.length > 0) {
// Get the first item from the `oneOf` | `anyOf` array
const firstOneOfItem = discriminateSchema[0];
// Return an example for the first item
return getExampleFromSchema(
firstOneOfItem,
options,
level + 1,
undefined,
undefined,
resultCache
);
}
// Check if schema has the `allOf` key
if (Array.isArray(schema.allOf)) {
let example: any = null;
// Loop through all `allOf` schemas
schema.allOf.forEach((allOfItem: Record<string, any>) => {
// Return an example from the schema
const newExample = getExampleFromSchema(
allOfItem,
options,
level + 1,
undefined,
undefined,
resultCache
);
// Merge or overwrite the example
example =
typeof newExample === 'object' && typeof example === 'object'
? {
...(example ?? {}),
...newExample,
}
: Array.isArray(newExample) && Array.isArray(example)
? [...(example ?? {}), ...newExample]
: newExample;
});
return cache(schema, example);
}
// Check if schema is a union type
if (Array.isArray(schema.type)) {
// Return null if the type is nullable
if (schema.type.includes('null')) {
return null;
}
// Return an example for the first type in the union
const exampleValue = exampleValues[schema.type[0]];
if (exampleValue !== undefined) {
return cache(schema, exampleValue);
}
}
// Warn if the type is unknown …
// console.warn(`[getExampleFromSchema] Unknown property type "${schema.type}".`)
// … and just return null for now.
return null;
};