1
+ import { html } from 'lit-element' ;
2
+ import { unsafeHTML } from 'lit-html/directives/unsafe-html.js' ;
3
+
1
4
/* For Delayed Event Handler Execution */
2
5
export function debounce ( fn , delay ) {
3
6
var timeoutID = null ;
@@ -28,6 +31,9 @@ export function getTypeInfo(schema, overrideAttributes=null){
28
31
if ( ! schema ) {
29
32
return ;
30
33
}
34
+ if ( schema . type === "circular" ) {
35
+ debugger ;
36
+ }
31
37
let returnObj = {
32
38
hasCircularRefs :schema . type === "circular" ,
33
39
format : schema . format ?schema . format :'' ,
@@ -149,7 +155,7 @@ export function schemaToModel (schema, obj) {
149
155
if ( schema . required && schema . required . includes ( key ) ) {
150
156
obj [ key + '*' ] = schemaToModel ( schema . properties [ key ] , { } ) ;
151
157
}
152
- else {
158
+ else {
153
159
obj [ key ] = schemaToModel ( schema . properties [ key ] , { } ) ;
154
160
}
155
161
}
@@ -158,24 +164,38 @@ export function schemaToModel (schema, obj) {
158
164
obj = [ schemaToModel ( schema . items , { } ) ]
159
165
}
160
166
else if ( schema . allOf ) {
161
- // If allOf is an array of multiple elements, then all the keys makes a single object
162
167
let objWithAllProps = { } ;
163
- schema . allOf . map ( function ( v ) {
164
- if ( v . type === 'object' || v . properties ) {
165
- let partialObj = schemaToModel ( v , { } ) ;
166
- Object . assign ( objWithAllProps , partialObj ) ;
167
- }
168
- else if ( v . type === "array" || v . items ) {
169
- let partialObj = [ schemaToModel ( v , { } ) ] ;
170
- Object . assign ( objWithAllProps , partialObj ) ;
168
+ if ( schema . allOf . length === 1 && ! schema . allOf [ 0 ] . properties && ! schema . allOf [ 0 ] . items ) {
169
+ // If allOf has single item and the type is not an object or array, then its a primitive
170
+ if ( schema . allOf [ 0 ] . $ref ) {
171
+ return `{ ${ schema . allOf [ 0 ] . $ref } } ~|~ Recursive Object` ;
171
172
}
172
173
else {
173
- let prop = 'prop' + Object . keys ( objWithAllProps ) . length ;
174
- objWithAllProps [ prop ] = `${ getTypeInfo ( v ) . html } ~|~${ v . description ? v . description : '' } `
174
+ let tempSchema = schema . allOf [ 0 ] ;
175
+ return `${ getTypeInfo ( tempSchema ) . html } ~|~${ tempSchema . description ? tempSchema . description : '' } ` ;
175
176
}
176
-
177
- } ) ;
178
- debugger ;
177
+ }
178
+ else {
179
+ // If allOf is an array of multiple elements, then all the keys makes a single object
180
+ schema . allOf . map ( function ( v ) {
181
+ if ( v . type === 'object' || v . properties ) {
182
+ let partialObj = schemaToModel ( v , { } ) ;
183
+ Object . assign ( objWithAllProps , partialObj ) ;
184
+ }
185
+ else if ( v . type === "array" || v . items ) {
186
+ let partialObj = [ schemaToModel ( v , { } ) ] ;
187
+ Object . assign ( objWithAllProps , partialObj ) ;
188
+ }
189
+ else if ( v . type ) {
190
+ let prop = 'prop' + Object . keys ( objWithAllProps ) . length ;
191
+ let typeObj = getTypeInfo ( v ) ;
192
+ objWithAllProps [ prop ] = `${ typeObj . html } ~|~${ v . description ? v . description : '' } `
193
+ }
194
+ else {
195
+ return '' ;
196
+ }
197
+ } ) ;
198
+ }
179
199
obj = objWithAllProps ;
180
200
}
181
201
else if ( schema . anyOf || schema . oneOf ) {
@@ -200,9 +220,9 @@ export function schemaToModel (schema, obj) {
200
220
obj [ ( schema . anyOf ? "ANY_OF" : "ONE_OF" ) ] = objWithAnyOfProps ;
201
221
}
202
222
else {
203
- let typeHtml = getTypeInfo ( schema ) . html ;
204
- if ( typeHtml ) {
205
- return `${ getTypeInfo ( schema ) . html } ~|~${ schema . description ?schema . description :'' } ` ;
223
+ let typeObj = getTypeInfo ( schema ) ;
224
+ if ( typeObj . html ) {
225
+ return `${ typeObj . html } ~|~${ schema . description ?schema . description :'' } ` ;
206
226
}
207
227
else {
208
228
return '' ;
@@ -213,10 +233,8 @@ export function schemaToModel (schema, obj) {
213
233
}
214
234
215
235
216
-
217
-
218
236
/* Create Example object */
219
- export function generateExample ( examples , example , schema , mimeType , includeReadOnly = true , outputType ) {
237
+ export function generateExample ( examples , example , schema , mimeType , includeReadOnly = false , outputType ) {
220
238
let finalExamples = [ ] ;
221
239
if ( examples ) {
222
240
for ( let eg in examples ) {
@@ -288,10 +306,10 @@ export function generateExample(examples, example, schema, mimeType, includeRead
288
306
289
307
/* For changing JSON-Schema to a Sample Object, as per the schema */
290
308
export function schemaToObj ( schema , obj , config = { } ) {
291
- if ( schema == null ) {
309
+ if ( schema === null ) {
292
310
return ;
293
311
}
294
- if ( schema . type === "object" || schema . properties ) {
312
+ if ( schema . type === "object" || schema . properties ) {
295
313
for ( let key in schema . properties ) {
296
314
if ( schema . properties [ key ] . deprecated ) {
297
315
continue ;
@@ -312,20 +330,44 @@ export function schemaToObj (schema, obj, config={}) {
312
330
}
313
331
else if ( schema . allOf ) {
314
332
let objWithAllProps = { } ;
315
- schema . allOf . map ( function ( v ) {
316
- if ( v . type === 'object' || v . properties ) {
317
- let partialObj = schemaToObj ( v , { } , config ) ;
318
- Object . assign ( objWithAllProps , partialObj ) ;
319
- }
320
- else if ( v . type === "array" || v . items ) {
321
- let partialObj = [ schemaToObj ( v , { } , config ) ] ;
322
- Object . assign ( objWithAllProps , partialObj ) ;
333
+
334
+ if ( schema . allOf . length === 1 && ! schema . allOf [ 0 ] . properties && ! schema . allOf [ 0 ] . items ) {
335
+ // If allOf has single item and the type is not an object or array, then its a primitive
336
+ if ( schema . allOf [ 0 ] . $ref ) {
337
+ return '{ }' ;
323
338
}
324
- else {
325
- let prop = 'prop' + Object . keys ( objWithAllProps ) . length ;
326
- objWithAllProps [ prop ] = getSampleValueByType ( v ) ;
339
+ else {
340
+ if ( schema . allOf [ 0 ] . readOnly && config . includeReadOnly ) {
341
+ let tempSchema = schema . allOf [ 0 ] ;
342
+ return getSampleValueByType ( tempSchema ) ;
343
+ }
344
+ else {
345
+ return ;
346
+ }
327
347
}
328
- } ) ;
348
+ }
349
+ else {
350
+ schema . allOf . map ( function ( v ) {
351
+ if ( v . readOnly ) {
352
+ return 'abcd' ;
353
+ }
354
+ else if ( v . type === 'object' || v . properties ) {
355
+ let partialObj = schemaToObj ( v , { } , config ) ;
356
+ Object . assign ( objWithAllProps , partialObj ) ;
357
+ }
358
+ else if ( v . type === "array" || v . items ) {
359
+ let partialObj = [ schemaToObj ( v , { } , config ) ] ;
360
+ Object . assign ( objWithAllProps , partialObj ) ;
361
+ }
362
+ else if ( v . type ) {
363
+ let prop = 'prop' + Object . keys ( objWithAllProps ) . length ;
364
+ objWithAllProps [ prop ] = getSampleValueByType ( v ) ;
365
+ }
366
+ else {
367
+ return '' ;
368
+ }
369
+ } ) ;
370
+ }
329
371
obj = objWithAllProps ;
330
372
}
331
373
else if ( schema . oneOf ) {
0 commit comments