@@ -26,7 +26,9 @@ export function getTypeInfo(schema) {
26
26
? 'enum'
27
27
: schema . format
28
28
? schema . format
29
- : schema . type ,
29
+ : schema . type
30
+ ? schema . type
31
+ : '{missing-type-info}' ,
30
32
format : schema . format ? schema . format : '' ,
31
33
pattern : ( schema . pattern && ! schema . enum ) ? schema . pattern : '' ,
32
34
readOrWriteOnly : schema . readOnly
@@ -42,8 +44,11 @@ export function getTypeInfo(schema) {
42
44
arrayType : '' ,
43
45
html : '' ,
44
46
} ;
47
+
45
48
if ( info . type === '{recursive}' ) {
46
49
info . description = schema . $ref . substring ( schema . $ref . lastIndexOf ( '/' ) + 1 ) ;
50
+ } else if ( info . type === '{missing-type-info}' ) {
51
+ info . description = 'No schema details found in the spec' ;
47
52
}
48
53
49
54
// Set Allowed Values
@@ -86,7 +91,6 @@ export function getTypeInfo(schema) {
86
91
info . constrain = `max ${ schema . maxLength } chars` ;
87
92
}
88
93
}
89
-
90
94
info . html = `${ info . type } ~|~${ info . readOrWriteOnly } ${ info . deprecated } ~|~${ info . constrain } ~|~${ info . default } ~|~${ info . allowedValues } ~|~${ info . pattern } ~|~${ info . description } ` ;
91
95
return info ;
92
96
}
@@ -165,7 +169,7 @@ export function getSampleValueByType(schemaObj) {
165
169
}
166
170
}
167
171
168
- /* For changing JSON-Schema to a Sample Object, as per the schema */
172
+ /* For changing JSON-Schema to a Sample Object, as per the schema (to generate examples based on schema) */
169
173
export function schemaToSampleObj ( schema , config = { } ) {
170
174
let obj = { } ;
171
175
if ( ! schema ) {
@@ -208,8 +212,26 @@ export function schemaToSampleObj(schema, config = { }) {
208
212
obj = schemaToSampleObj ( schema . oneOf [ 0 ] , config ) ;
209
213
}
210
214
} else if ( schema . anyOf ) {
215
+ // First generate values for regular properties
216
+ if ( schema . type === 'object' || schema . properties ) {
217
+ for ( const key in schema . properties ) {
218
+ if ( schema . properties [ key ] . deprecated && ! config . includeDeprecated ) { continue ; }
219
+ if ( schema . properties [ key ] . readOnly && ! config . includeReadOnly ) { continue ; }
220
+ if ( schema . properties [ key ] . writeOnly && ! config . includeWriteOnly ) { continue ; }
221
+
222
+ if ( schema . example ) {
223
+ obj [ key ] = schema . example ;
224
+ } else if ( schema . examples && schema . example . length > 0 ) {
225
+ obj [ key ] = schema . examples [ 0 ] ;
226
+ } else {
227
+ obj [ key ] = schemaToSampleObj ( schema . properties [ key ] , config ) ;
228
+ }
229
+ }
230
+ }
231
+ let i = 1 ;
211
232
if ( schema . anyOf . length > 0 ) {
212
- obj = schemaToSampleObj ( schema . anyOf [ 0 ] , config ) ;
233
+ obj [ `prop${ i } ` ] = schemaToSampleObj ( schema . anyOf [ 0 ] , config ) ;
234
+ i ++ ;
213
235
}
214
236
} else if ( schema . type === 'object' || schema . properties ) {
215
237
for ( const key in schema . properties ) {
@@ -277,10 +299,23 @@ export function schemaInObjectNotation(schema, obj, level = 0, suffix = '') {
277
299
} ) ;
278
300
obj = objWithAllProps ;
279
301
} else if ( schema . anyOf || schema . oneOf ) {
302
+ // 1. First iterate the regular properties
303
+ if ( schema . type === 'object' || schema . properties ) {
304
+ obj [ '::description' ] = schema . description ? schema . description : '' ;
305
+ obj [ '::type' ] = 'object' ;
306
+ for ( const key in schema . properties ) {
307
+ if ( schema . required && schema . required . includes ( key ) ) {
308
+ obj [ `${ key } *` ] = schemaInObjectNotation ( schema . properties [ key ] , { } , ( level + 1 ) ) ;
309
+ } else {
310
+ obj [ key ] = schemaInObjectNotation ( schema . properties [ key ] , { } , ( level + 1 ) ) ;
311
+ }
312
+ }
313
+ }
314
+ // 2. Then show allof/anyof objects
280
315
let i = 1 ;
281
316
const objWithAnyOfProps = { } ;
282
317
const xxxOf = schema . anyOf ? 'anyOf' : 'oneOf' ;
283
- schema [ xxxOf ] . map ( ( v ) => {
318
+ schema [ xxxOf ] . forEach ( ( v ) => {
284
319
if ( v . type === 'object' || v . properties || v . allOf || v . anyOf || v . oneOf ) {
285
320
const partialObj = schemaInObjectNotation ( v , { } ) ;
286
321
objWithAnyOfProps [ `::OPTION~${ i } ` ] = partialObj ;
0 commit comments