@@ -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,76 @@ 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 += `${ JSON . stringify ( extractedFields . json , 0 , 2 ) } `
106+ markdownContent += '```\n\n'
107+ }
108+ }
109+ }
110+ }
111+
112+ return markdownContent ;
113+ }
114+
115+ function extractFields ( result , schemas , fieldPrefix ) {
116+ const schemaRef = result . type === 'array' ? result . items [ '$ref' ] . split ( '/' ) . pop ( ) : result [ '$ref' ] . split ( '/' ) . pop ( ) ;
117+ const bodyParamAttrs = schemas [ schemaRef ] . properties ;
118+ const bodyParams = Object . keys ( bodyParamAttrs ) ;
119+ const resObj = {
120+ markdown : '' ,
121+ json : { }
122+ }
123+ for ( const parameter of bodyParams ) {
124+ const newPrefix = fieldPrefix ? `${ fieldPrefix } .${ parameter } ` : parameter ;
125+ if ( bodyParamAttrs [ parameter ] [ '$ref' ] ) {
126+
127+ const nestedObj = extractFields ( bodyParamAttrs [ parameter ] , schemas , newPrefix )
128+ resObj . markdown += nestedObj . markdown
129+ resObj . json [ parameter ] = nestedObj . json
130+ }
131+ else {
132+ const paramType = bodyParamAttrs [ parameter ] . format || bodyParamAttrs [ parameter ] . type ;
133+ resObj . markdown += `| ${ newPrefix } | ${ paramType || '' } | ${ bodyParamAttrs [ parameter ] . description || '' } | \n` ;
134+ resObj . json [ parameter ] = returnParamTypeSample ( bodyParamAttrs [ parameter ] . format || bodyParamAttrs [ parameter ] . type ) ;
135+ }
136+ }
137+ return resObj ;
138+ }
105139
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' ;
140+ function returnParamTypeSample ( paramType ) {
141+ let result ;
142+ switch ( paramType ) {
143+ case 'uuid' :
144+ result = 'uuid' ;
113145 break ;
114146 case 'string' :
115- sampleResponseObj [ parameter ] = 'string' ;
147+ result = 'string' ;
116148 break ;
117149 case 'number' :
118- sampleResponseObj [ parameter ] = 0 ;
150+ result = 0 ;
119151 break ;
120152 case 'array' :
121- sampleResponseObj [ parameter ] = 'Array' ;
153+ result = 'Array' ;
122154 break ;
123155 case 'boolean' :
124- sampleResponseObj [ parameter ] = 'boolean' ;
156+ result = 'boolean' ;
125157 break ;
126158 case 'date-time' :
127- sampleResponseObj [ parameter ] = 'date-time' ;
159+ result = 'date-time' ;
160+ break ;
161+ case 'date' :
162+ result = 'date' ;
128163 break ;
129164 case 'email' :
130- sampleResponseObj [ parameter ] = 'email' ;
165+ result = 'email' ;
131166 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- }
144167 }
145-
146- return markdownContent ;
168+ return result ;
147169}
148170
149171async function main ( ) {
0 commit comments