@@ -181,8 +181,28 @@ function getParameterTableDef(parameters, paramType, localize, includeExample =
181181 ] ;
182182}
183183
184+ function getExamplesDef ( contentTypeObj , localizedExampleLabel ) {
185+ const exampleSectionDef = [ ] ;
186+ if ( contentTypeObj . example ) {
187+ exampleSectionDef . push ( [
188+ { text : `${ localizedExampleLabel } :` , margin : [ 20 , 10 , 0 , 0 ] , style : [ 'small' , 'b' ] } ,
189+ { text : JSON . stringify ( contentTypeObj . example , null , 2 ) , margin : [ 40 , 10 , 0 , 0 ] , style : 'monoSub' } ,
190+ ] ) ;
191+ }
192+ if ( contentTypeObj . examples ) {
193+ let iterCount = 0 ;
194+ for ( const oneExample in contentTypeObj . examples ) {
195+ exampleSectionDef . push ( [
196+ { text : `${ localizedExampleLabel } ${ ++ iterCount } :` , margin : [ 20 , 10 , 0 , 0 ] , style : [ 'small' , 'b' ] } ,
197+ { text : JSON . stringify ( oneExample , null , 2 ) , margin : [ 40 , 10 , 0 , 0 ] , style : 'monoSub' } ,
198+ ] ) ;
199+ }
200+ }
201+ return exampleSectionDef ;
202+ }
203+
184204// Request Body Def
185- function getRequestBodyDef ( requestBody , schemaStyle , localize ) {
205+ function getRequestBodyDef ( requestBody , schemaStyle , localize , includeExample = false ) {
186206 if ( ! requestBody ) {
187207 return ;
188208 }
@@ -212,7 +232,7 @@ function getRequestBodyDef(requestBody, schemaStyle, localize) {
212232 }
213233 requestBodyDef . push ( treeDef ) ;
214234 } else {
215- // if Schema Style is Tree
235+ // Schema style is "tree."
216236 let schemaTableTreeDef ;
217237 if ( schemaInObjectNotaion [ '::type' ] && schemaInObjectNotaion [ '::type' ] === 'array' ) {
218238 schemaTableTreeDef = objectToTableTree ( schemaInObjectNotaion [ '::prop' ] , localize , 'array' ) ;
@@ -239,12 +259,16 @@ function getRequestBodyDef(requestBody, schemaStyle, localize) {
239259 }
240260 content . push ( requestBodyDef ) ;
241261 }
262+
263+ if ( includeExample ) {
264+ content . push ( getExamplesDef ( contentTypeObj , localize . example ) ) ;
265+ }
242266 }
243267 return content ;
244268}
245269
246270// Response Def
247- function getResponseDef ( responses , schemaStyle , localize ) {
271+ function getResponseDef ( responses , schemaStyle , localize , includeExample = false ) {
248272 const respDef = [ ] ;
249273 for ( const statusCode in responses ) {
250274 const allResponseDefs = [ ] ;
@@ -253,7 +277,8 @@ function getResponseDef(responses, schemaStyle, localize) {
253277 { text : `${ localize . responseModel } - ${ contentType } ` , margin : [ 10 , 10 , 0 , 0 ] , style : [ 'small' , 'b' ] } ,
254278 ] ;
255279
256- let origSchema = responses [ statusCode ] . content [ contentType ] . schema ;
280+ const contentTypeObj = responses [ statusCode ] . content [ contentType ] ;
281+ let origSchema = contentTypeObj . schema ;
257282 if ( origSchema ) {
258283 origSchema = JSON . parse ( JSON . stringify ( origSchema ) ) ;
259284 const schemaInObjectNotaion = schemaInObjectNotation ( origSchema ) ;
@@ -299,6 +324,9 @@ function getResponseDef(responses, schemaStyle, localize) {
299324 }
300325 }
301326 }
327+ if ( includeExample ) {
328+ responseDef . push ( getExamplesDef ( contentTypeObj , localize . example ) ) ;
329+ }
302330 allResponseDefs . push ( responseDef ) ;
303331 }
304332 respDef . push ( {
@@ -399,7 +427,7 @@ export function getApiDef(spec, filterPath, schemaStyle, localize, includeExampl
399427
400428 // Generate Response Defs
401429 operationContent . push ( { text : localize . response , style : [ 'p' , 'b' , 'alternate' ] , margin : [ 0 , 10 , 0 , 0 ] } ) ;
402- const respDef = getResponseDef ( path . responses , schemaStyle , localize ) ;
430+ const respDef = getResponseDef ( path . responses , schemaStyle , localize , includeExample ) ;
403431 if ( respDef && respDef . length > 0 ) {
404432 operationContent . push ( {
405433 stack : respDef ,
0 commit comments