@@ -169,6 +169,48 @@ export function getSampleValueByType(schemaObj) {
169
169
}
170
170
}
171
171
172
+ /*
173
+ {
174
+ 'prop1' : 'one',
175
+ 'prop2' : 'two',
176
+ 'prop3' : [ 'a', 'b', 'c' ],
177
+ 'prop4' : {
178
+ 'ob1' : 'val-1',
179
+ 'ob2' : 'val-2'
180
+ }
181
+ }
182
+ <root>
183
+ <prop1>simple</prop1>
184
+ <prop2>
185
+ <0> a </0>
186
+ <1> b </1>
187
+ <2> c </2>
188
+ </prop2>
189
+ <prop3>
190
+ <ob1>val-1</ob1>
191
+ <ob2>val-2</ob2>
192
+ </prop3>
193
+ </root>
194
+ */
195
+
196
+ export function json2xml ( obj , level = 1 ) {
197
+ const indent = ' ' . repeat ( level ) ;
198
+ let xmlText = '' ;
199
+ if ( level === 1 && typeof obj !== 'object' ) {
200
+ return `\n${ indent } ${ obj . toString ( ) } ` ;
201
+ }
202
+ for ( const prop in obj ) {
203
+ if ( Array . isArray ( obj [ prop ] ) ) {
204
+ xmlText = `${ xmlText } \n${ indent } <${ prop } > ${ json2xml ( obj [ prop ] , level + 1 ) } \n${ indent } </${ prop } >` ;
205
+ } else if ( typeof obj [ prop ] === 'object' ) {
206
+ xmlText = `${ xmlText } \n${ indent } <${ prop } > ${ json2xml ( obj [ prop ] , level + 1 ) } \n${ indent } </${ prop } >` ;
207
+ } else {
208
+ xmlText = `${ xmlText } \n${ indent } <${ prop } > ${ obj [ prop ] . toString ( ) } </${ prop } >` ;
209
+ }
210
+ }
211
+ return xmlText ;
212
+ }
213
+
172
214
/* For changing JSON-Schema to a Sample Object, as per the schema (to generate examples based on schema) */
173
215
export function schemaToSampleObj ( schema , config = { } ) {
174
216
let obj = { } ;
@@ -364,6 +406,8 @@ export function schemaInObjectNotation(schema, obj, level = 0, suffix = '') {
364
406
/* Create Example object */
365
407
export function generateExample ( examples , example , schema , mimeType , includeReadOnly = true , outputType ) {
366
408
const finalExamples = [ ] ;
409
+
410
+ // First check if examples is provided
367
411
if ( examples ) {
368
412
for ( const eg in examples ) {
369
413
let egContent = '' ;
@@ -420,7 +464,7 @@ export function generateExample(examples, example, schema, mimeType, includeRead
420
464
egFormat = 'text' ;
421
465
}
422
466
finalExamples . push ( {
423
- exampleId : '_default ' ,
467
+ exampleId : 'Example ' ,
424
468
exampleSummary : '' ,
425
469
exampleDescription : '' ,
426
470
exampleType : mimeType ,
@@ -433,14 +477,14 @@ export function generateExample(examples, example, schema, mimeType, includeRead
433
477
if ( schema ) {
434
478
if ( schema . example ) { // Note: schema.examples (plurals) is not allowed as per spec
435
479
finalExamples . push ( {
436
- exampleId : '_default ' ,
480
+ exampleId : 'Example ' ,
437
481
exampleSummary : '' ,
438
482
exampleDescription : '' ,
439
483
exampleType : mimeType ,
440
484
exampleValue : schema . example ,
441
485
exampleFormat : 'text' ,
442
486
} ) ;
443
- } else if ( mimeType . toLowerCase ( ) . includes ( 'json' ) || mimeType . toLowerCase ( ) . includes ( '*/*' ) ) {
487
+ } else if ( mimeType . toLowerCase ( ) . includes ( 'json' ) || mimeType . toLowerCase ( ) . includes ( 'text' ) || mimeType . toLowerCase ( ) . includes ( ' */*') ) {
444
488
const egJson = schemaToSampleObj (
445
489
schema ,
446
490
{
@@ -451,26 +495,35 @@ export function generateExample(examples, example, schema, mimeType, includeRead
451
495
) ;
452
496
453
497
finalExamples . push ( {
454
- exampleId : '_default ' ,
498
+ exampleId : 'Example ' ,
455
499
exampleSummary : '' ,
456
500
exampleDescription : '' ,
457
501
exampleType : mimeType ,
458
502
exampleFormat : outputType ,
459
503
exampleValue : outputType === 'text' ? JSON . stringify ( egJson , null , 2 ) : egJson ,
460
504
} ) ;
461
505
} else if ( mimeType . toLowerCase ( ) . includes ( 'xml' ) ) {
462
- // TODO: in case the mimeType is XML then generate an XML example
506
+ const xmlRootStart = ( schema . xml && schema . xml . name ) ? `<${ schema . xml . name } >` : '<root>' ;
507
+ const xmlRootEnd = ( schema . xml && schema . xml . name ) ? `</${ schema . xml . name } >` : '</root>' ;
508
+ const egJson = schemaToSampleObj (
509
+ schema ,
510
+ {
511
+ includeReadOnly,
512
+ includeWriteOnly : true ,
513
+ deprecated : true ,
514
+ } ,
515
+ ) ;
463
516
finalExamples . push ( {
464
- exampleId : '_default ' ,
517
+ exampleId : 'Example ' ,
465
518
exampleSummary : '' ,
466
519
exampleDescription : '' ,
467
520
exampleType : mimeType ,
468
- exampleValue : '' ,
521
+ exampleValue : ` ${ xmlRootStart } ${ json2xml ( egJson ) } \n ${ xmlRootEnd } ` ,
469
522
exampleFormat : 'text' ,
470
523
} ) ;
471
524
} else {
472
525
finalExamples . push ( {
473
- exampleId : '_default ' ,
526
+ exampleId : 'Example ' ,
474
527
exampleSummary : '' ,
475
528
exampleDescription : '' ,
476
529
exampleType : mimeType ,
@@ -481,7 +534,7 @@ export function generateExample(examples, example, schema, mimeType, includeRead
481
534
} else {
482
535
// No Example or Schema provided (should never reach here)
483
536
finalExamples . push ( {
484
- exampleId : '_default ' ,
537
+ exampleId : 'Example ' ,
485
538
exampleSummary : '' ,
486
539
exampleDescription : '' ,
487
540
exampleType : mimeType ,
@@ -493,7 +546,6 @@ export function generateExample(examples, example, schema, mimeType, includeRead
493
546
return finalExamples ;
494
547
}
495
548
496
-
497
549
export function getBaseUrlFromUrl ( url ) {
498
550
const pathArray = url . split ( '/' ) ;
499
551
return `${ pathArray [ 0 ] } //${ pathArray [ 2 ] } ` ;
0 commit comments