@@ -87,7 +87,7 @@ function generateDocusaurusMarkdown(spec, groupedEndpoints, prefix) {
8787 if ( operation . responses && operation . responses [ '200' ] . content [ "application/json" ] ) {
8888 const rawSchema = operation . responses [ '200' ] . content [ "application/json" ] . schema
8989 const result = rawSchema . properties . result
90-
90+
9191 if ( result ) {
9292 markdownContent += `\n### Response\n\n` ;
9393
@@ -96,54 +96,77 @@ function generateDocusaurusMarkdown(spec, groupedEndpoints, prefix) {
9696 const schema = rawSchema . properties . result . type === 'array' ?
9797 result . items [ '$ref' ] . split ( '/' ) . pop ( ) : result [ '$ref' ] . split ( '/' ) . pop ( )
9898
99- const bodyParamAttrs = spec . components . schemas [ schema ] . properties
100- const bodyParams = Object . keys ( bodyParamAttrs )
101- const sampleResponseObj = { }
102-
99+ const extractedFields = extractFields ( result , spec . components . schemas , undefined ) ;
103100 markdownContent += `| Name | Type | Description |\n`
104101 markdownContent += `| :--- | :--- | :---------- |\n`
102+ markdownContent += extractedFields . markdown
103+ markdownContent += '\n'
104+ markdownContent += `\n#### Sample response\n\n` ;
105+ markdownContent += '```\n'
106+ markdownContent += `${ JSON . stringify ( extractedFields . json , 0 , 2 ) } `
107+ markdownContent += '\n```\n'
108+ }
109+ }
110+ }
111+ }
112+
113+ return markdownContent ;
114+ }
115+
116+ function extractFields ( result , schemas , fieldPrefix ) {
117+ const schemaRef = result . type === 'array' ? result . items [ '$ref' ] . split ( '/' ) . pop ( ) : result [ '$ref' ] . split ( '/' ) . pop ( ) ;
118+ const bodyParamAttrs = schemas [ schemaRef ] . properties ;
119+ const bodyParams = Object . keys ( bodyParamAttrs ) ;
120+ const resObj = {
121+ markdown : '' ,
122+ json : { }
123+ }
124+ for ( const parameter of bodyParams ) {
125+ const newPrefix = fieldPrefix ? `${ fieldPrefix } .${ parameter } ` : parameter ;
126+ if ( bodyParamAttrs [ parameter ] [ '$ref' ] ) {
127+
128+ const nestedObj = extractFields ( bodyParamAttrs [ parameter ] , schemas , newPrefix )
129+ resObj . markdown += nestedObj . markdown
130+ resObj . json [ parameter ] = nestedObj . json
131+ }
132+ else {
133+ const paramType = bodyParamAttrs [ parameter ] . format || bodyParamAttrs [ parameter ] . type ;
134+ resObj . markdown += `| ${ newPrefix } | ${ paramType || '' } | ${ bodyParamAttrs [ parameter ] . description || '' } | \n` ;
135+ resObj . json [ parameter ] = returnParamTypeSample ( bodyParamAttrs [ parameter ] . format || bodyParamAttrs [ parameter ] . type ) ;
136+ }
137+ }
138+ return resObj ;
139+ }
105140
106- for ( const parameter of bodyParams ) {
107- const paramType = bodyParamAttrs [ parameter ] . format || bodyParamAttrs [ parameter ] . type
108- markdownContent += `| ${ parameter } | ${ paramType || '' } | ${ bodyParamAttrs [ parameter ] . description || '' } | \n`
109-
110- switch ( paramType ) {
111- case 'uuid' :
112- sampleResponseObj [ parameter ] = 'uuid' ;
141+ function returnParamTypeSample ( paramType ) {
142+ let result ;
143+ switch ( paramType ) {
144+ case 'uuid' :
145+ result = 'uuid' ;
113146 break ;
114147 case 'string' :
115- sampleResponseObj [ parameter ] = 'string' ;
148+ result = 'string' ;
116149 break ;
117150 case 'number' :
118- sampleResponseObj [ parameter ] = 0 ;
151+ result = 0 ;
119152 break ;
120153 case 'array' :
121- sampleResponseObj [ parameter ] = 'Array' ;
154+ result = 'Array' ;
122155 break ;
123156 case 'boolean' :
124- sampleResponseObj [ parameter ] = 'boolean' ;
157+ result = 'boolean' ;
125158 break ;
126159 case 'date-time' :
127- sampleResponseObj [ parameter ] = 'date-time' ;
160+ result = 'date-time' ;
161+ break ;
162+ case 'date' :
163+ result = 'date' ;
128164 break ;
129165 case 'email' :
130- sampleResponseObj [ parameter ] = 'email' ;
166+ result = 'email' ;
131167 break ;
132- }
133- }
134-
135- markdownContent += `\n#### Sample response\n\n` ;
136- markdownContent += '```\n'
137- markdownContent += `${ JSON . stringify ( sampleResponseObj , 0 , 2 ) } `
138- markdownContent += '\n```\n'
139- }
140-
141-
142- }
143- }
144168 }
145-
146- return markdownContent ;
169+ return result ;
147170}
148171
149172async function main ( ) {
0 commit comments