@@ -16,14 +16,10 @@ export function generateSchemaExample(
16
16
schema : OpenAPIV3 . SchemaObject ,
17
17
options ?: GenerateSchemaExampleOptions
18
18
) : JSONValue | undefined {
19
- return getExampleFromSchema (
20
- schema ,
21
- {
22
- emptyString : 'text' ,
23
- ...options ,
24
- } ,
25
- 3 // Max depth for circular references
26
- ) ;
19
+ return getExampleFromSchema ( schema , {
20
+ emptyString : 'text' ,
21
+ ...options ,
22
+ } ) ;
27
23
}
28
24
29
25
/**
@@ -103,21 +99,6 @@ function guessFromFormat(schema: Record<string, any>, fallback = '') {
103
99
return genericExampleValues [ schema . format ] ?? fallback ;
104
100
}
105
101
106
- /** Map of all the results */
107
- const resultCache = new WeakMap < Record < string , any > , any > ( ) ;
108
-
109
- /** Store result in the cache, and return the result */
110
- function cache ( schema : Record < string , any > , result : unknown ) {
111
- // Avoid unnecessary WeakMap operations for primitive values
112
- if ( typeof result !== 'object' || result === null ) {
113
- return result ;
114
- }
115
-
116
- resultCache . set ( schema , result ) ;
117
-
118
- return result ;
119
- }
120
-
121
102
/**
122
103
* This function takes an OpenAPI schema and generates an example from it
123
104
* Forked from : https://github.com/scalar/scalar/blob/main/packages/oas-utils/src/spec-getters/getExampleFromSchema.ts
@@ -152,8 +133,20 @@ const getExampleFromSchema = (
152
133
} ,
153
134
level = 0 ,
154
135
parentSchema ?: Record < string , any > ,
155
- name ?: string
136
+ name ?: string ,
137
+ resultCache = new WeakMap < Record < string , any > , any > ( )
156
138
) : any => {
139
+ // Store result in the cache, and return the result
140
+ function cache ( schema : Record < string , any > , result : unknown ) {
141
+ // Avoid unnecessary WeakMap operations for primitive values
142
+ if ( typeof result !== 'object' || result === null ) {
143
+ return result ;
144
+ }
145
+
146
+ resultCache . set ( schema , result ) ;
147
+ return result ;
148
+ }
149
+
157
150
// Check if the result is already cached
158
151
if ( resultCache . has ( schema ) ) {
159
152
return resultCache . get ( schema ) ;
@@ -245,7 +238,8 @@ const getExampleFromSchema = (
245
238
options ,
246
239
level + 1 ,
247
240
schema ,
248
- propertyName
241
+ propertyName ,
242
+ resultCache
249
243
) ;
250
244
251
245
if ( typeof response [ propertyXmlTagName ?? propertyName ] === 'undefined' ) {
@@ -269,7 +263,8 @@ const getExampleFromSchema = (
269
263
options ,
270
264
level + 1 ,
271
265
schema ,
272
- exampleKey
266
+ exampleKey ,
267
+ resultCache
273
268
) ;
274
269
}
275
270
}
@@ -290,21 +285,51 @@ const getExampleFromSchema = (
290
285
response . ANY_ADDITIONAL_PROPERTY = getExampleFromSchema (
291
286
schema . additionalProperties ,
292
287
options ,
293
- level + 1
288
+ level + 1 ,
289
+ undefined ,
290
+ undefined ,
291
+ resultCache
294
292
) ;
295
293
}
296
294
}
297
295
298
296
if ( schema . anyOf !== undefined ) {
299
- Object . assign ( response , getExampleFromSchema ( schema . anyOf [ 0 ] , options , level + 1 ) ) ;
297
+ Object . assign (
298
+ response ,
299
+ getExampleFromSchema (
300
+ schema . anyOf [ 0 ] ,
301
+ options ,
302
+ level + 1 ,
303
+ undefined ,
304
+ undefined ,
305
+ resultCache
306
+ )
307
+ ) ;
300
308
} else if ( schema . oneOf !== undefined ) {
301
- Object . assign ( response , getExampleFromSchema ( schema . oneOf [ 0 ] , options , level + 1 ) ) ;
309
+ Object . assign (
310
+ response ,
311
+ getExampleFromSchema (
312
+ schema . oneOf [ 0 ] ,
313
+ options ,
314
+ level + 1 ,
315
+ undefined ,
316
+ undefined ,
317
+ resultCache
318
+ )
319
+ ) ;
302
320
} else if ( schema . allOf !== undefined ) {
303
321
Object . assign (
304
322
response ,
305
323
...schema . allOf
306
324
. map ( ( item : Record < string , any > ) =>
307
- getExampleFromSchema ( item , options , level + 1 , schema )
325
+ getExampleFromSchema (
326
+ item ,
327
+ options ,
328
+ level + 1 ,
329
+ schema ,
330
+ undefined ,
331
+ resultCache
332
+ )
308
333
)
309
334
. filter ( ( item : any ) => item !== undefined )
310
335
) ;
@@ -335,7 +360,9 @@ const getExampleFromSchema = (
335
360
{ type : 'object' , allOf : schema . items . allOf } ,
336
361
options ,
337
362
level + 1 ,
338
- schema
363
+ schema ,
364
+ undefined ,
365
+ resultCache
339
366
) ;
340
367
341
368
return cache (
@@ -346,7 +373,14 @@ const getExampleFromSchema = (
346
373
// For non-objects (like strings), collect all examples
347
374
const examples = schema . items . allOf
348
375
. map ( ( item : Record < string , any > ) =>
349
- getExampleFromSchema ( item , options , level + 1 , schema )
376
+ getExampleFromSchema (
377
+ item ,
378
+ options ,
379
+ level + 1 ,
380
+ schema ,
381
+ undefined ,
382
+ resultCache
383
+ )
350
384
)
351
385
. filter ( ( item : any ) => item !== undefined ) ;
352
386
@@ -368,7 +402,14 @@ const getExampleFromSchema = (
368
402
const schemas = schema . items [ rule ] . slice ( 0 , 1 ) ;
369
403
const exampleFromRule = schemas
370
404
. map ( ( item : Record < string , any > ) =>
371
- getExampleFromSchema ( item , options , level + 1 , schema )
405
+ getExampleFromSchema (
406
+ item ,
407
+ options ,
408
+ level + 1 ,
409
+ schema ,
410
+ undefined ,
411
+ resultCache
412
+ )
372
413
)
373
414
. filter ( ( item : any ) => item !== undefined ) ;
374
415
@@ -380,7 +421,14 @@ const getExampleFromSchema = (
380
421
}
381
422
382
423
if ( schema . items ?. type ) {
383
- const exampleFromSchema = getExampleFromSchema ( schema . items , options , level + 1 ) ;
424
+ const exampleFromSchema = getExampleFromSchema (
425
+ schema . items ,
426
+ options ,
427
+ level + 1 ,
428
+ undefined ,
429
+ undefined ,
430
+ resultCache
431
+ ) ;
384
432
385
433
return wrapItems ? [ { [ itemsXmlTagName ] : exampleFromSchema } ] : [ exampleFromSchema ] ;
386
434
}
@@ -407,7 +455,14 @@ const getExampleFromSchema = (
407
455
const firstOneOfItem = discriminateSchema [ 0 ] ;
408
456
409
457
// Return an example for the first item
410
- return getExampleFromSchema ( firstOneOfItem , options , level + 1 ) ;
458
+ return getExampleFromSchema (
459
+ firstOneOfItem ,
460
+ options ,
461
+ level + 1 ,
462
+ undefined ,
463
+ undefined ,
464
+ resultCache
465
+ ) ;
411
466
}
412
467
413
468
// Check if schema has the `allOf` key
@@ -417,7 +472,14 @@ const getExampleFromSchema = (
417
472
// Loop through all `allOf` schemas
418
473
schema . allOf . forEach ( ( allOfItem : Record < string , any > ) => {
419
474
// Return an example from the schema
420
- const newExample = getExampleFromSchema ( allOfItem , options , level + 1 ) ;
475
+ const newExample = getExampleFromSchema (
476
+ allOfItem ,
477
+ options ,
478
+ level + 1 ,
479
+ undefined ,
480
+ undefined ,
481
+ resultCache
482
+ ) ;
421
483
422
484
// Merge or overwrite the example
423
485
example =
0 commit comments