1- const generateRequestBodyExample = ( schema ) => {
1+ const generateRequestBodyExample = ( schema , level = 0 , context = { processedSchemas : new Set ( ) } ) => {
22 if ( ! schema ) return { } ;
33
4+ if ( schema . example !== undefined ) {
5+ return schema . example ;
6+ }
7+
48 if ( schema . enum ) {
59 return schema . example || schema . enum [ 0 ] ;
610 }
711
812 if ( schema . oneOf ) {
9- return generateRequestBodyExample ( schema . oneOf [ 0 ] ) ;
13+ return generateRequestBodyExample ( schema . oneOf [ 0 ] , level + 1 , context ) ;
1014 }
1115
1216 if ( schema . anyOf ) {
13- return generateRequestBodyExample ( schema . anyOf [ 0 ] ) ;
17+ return generateRequestBodyExample ( schema . anyOf [ 0 ] , level + 1 , context ) ;
1418 }
1519
1620 if ( schema . allOf ) {
17- return generateAllOfExample ( schema . allOf ) ;
21+ return generateAllOfExample ( schema . allOf , level + 1 , context ) ;
1822 }
1923
2024 switch ( schema . type ) {
2125 case 'object' :
22- return generateObjectExample ( schema ) ;
26+ return generateObjectExample ( schema , level + 1 , context ) ;
2327 case 'array' :
24- return generateArrayExample ( schema ) ;
28+ return generateArrayExample ( schema , level + 1 , context ) ;
2529 case 'string' :
2630 return generateStringExample ( schema ) ;
2731 case 'integer' :
@@ -35,33 +39,46 @@ const generateRequestBodyExample = (schema) => {
3539 }
3640} ;
3741
38- const generateAllOfExample = ( schemas ) => {
42+ const generateAllOfExample = ( schemas , level , context ) => {
3943 const example = { } ;
4044 schemas . forEach ( ( subSchema ) => {
41- const subExample = generateRequestBodyExample ( subSchema ) ;
45+ const subExample = generateRequestBodyExample ( subSchema , level , context ) ;
4246 Object . assign ( example , subExample ) ;
4347 } ) ;
4448 return example ;
4549} ;
4650
47- const generateObjectExample = ( schema ) => {
51+ const generateObjectExample = ( schema , level , context ) => {
52+ if ( schema . example !== undefined ) {
53+ return schema . example ;
54+ }
55+
56+ if ( context . processedSchemas . has ( schema ) && level > 1 ) {
57+ return { } ;
58+ }
59+ context . processedSchemas . add ( schema ) ;
60+
4861 const example = { } ;
4962 const properties = schema . properties || { } ;
5063
5164 for ( const [ key , propertySchema ] of Object . entries ( properties ) ) {
52- example [ key ] = generateRequestBodyExample ( propertySchema ) ;
65+ example [ key ] = generateRequestBodyExample ( propertySchema , level , context ) ;
5366 }
5467
68+ context . processedSchemas . delete ( schema ) ;
5569 return example ;
5670} ;
5771
58- const generateArrayExample = ( schema ) => {
72+ const generateArrayExample = ( schema , level , context ) => {
73+ if ( schema . example !== undefined ) {
74+ return schema . example ;
75+ }
5976 const itemsSchema = schema . items || { } ;
60- return [ generateRequestBodyExample ( itemsSchema ) ] ;
77+ return [ generateRequestBodyExample ( itemsSchema , level , context ) ] ;
6178} ;
6279
6380const generateStringExample = ( schema ) => {
64- let example = schema . example || 'string' ;
81+ let example = String ( schema . example || 'string' ) ;
6582
6683 if ( schema . minLength || schema . maxLength ) {
6784 example = generateStringWithLengthConstraints ( example , schema . minLength , schema . maxLength ) ;
0 commit comments