@@ -293,29 +293,44 @@ export const generateSchemas = (models: NameKeyMap<SwaggerSailsModel>): NameKeyM
293
293
return schemas ;
294
294
}
295
295
296
- const schema : OpenApi . UpdatedSchema = {
296
+ const schemaWithoutRequired : OpenApi . UpdatedSchema = {
297
297
description : model . swagger . modelSchema ?. description || `Sails ORM Model **${ model . globalId } **` ,
298
298
properties : { } ,
299
- required : [ ] ,
300
- ...omit ( model . swagger ?. modelSchema || { } , 'exclude' , 'description' , 'tags' ) ,
299
+ ...omit ( model . swagger ?. modelSchema || { } , 'exclude' , 'description' , 'required' , 'tags' ) ,
301
300
}
302
301
302
+ let required : string [ ] = [ ] ;
303
+
303
304
const attributes = model . attributes || { }
304
305
defaults (
305
- schema . properties ,
306
+ schemaWithoutRequired . properties ,
306
307
Object . keys ( attributes ) . reduce ( ( props , attributeName ) => {
307
308
const attribute = model . attributes [ attributeName ] ;
308
309
if ( attribute . meta ?. swagger ?. exclude !== true ) {
309
310
props [ attributeName ] = generateAttributeSchema ( attribute ) ;
310
- if ( attribute . required ) schema . required ! . push ( attributeName ) ;
311
+ if ( attribute . required ) required ! . push ( attributeName ) ;
311
312
}
312
313
return props
313
314
} , { } as NameKeyMap < OpenApi . UpdatedSchema > )
314
315
) ;
315
316
316
- if ( schema . required ! . length <= 0 ) delete schema . required ;
317
+ const withoutRequiredName = `${ model . identity } -without-required-constraint` ;
318
+ const schema : OpenApi . UpdatedSchema = {
319
+ allOf : [
320
+ { '$ref' : `#/components/schemas/${ withoutRequiredName } ` } ,
321
+ ] ,
322
+ } ;
323
+
324
+ if ( model . swagger ?. modelSchema ?. required ) {
325
+ required = [ ...model . swagger . modelSchema . required ] ;
326
+ }
327
+
328
+ if ( required . length > 0 ) {
329
+ schema . allOf ! . push ( { required : required } ) ;
330
+ }
317
331
318
332
schemas [ model . identity ] = schema ;
333
+ schemas [ withoutRequiredName ] = schemaWithoutRequired ;
319
334
320
335
return schemas
321
336
} , { } as NameKeyMap < OpenApi . UpdatedSchema | Reference > )
@@ -478,13 +493,39 @@ export const generatePaths = (routes: SwaggerRouteInfo[], templates: BlueprintAc
478
493
required : true ,
479
494
content : {
480
495
'application/json' : {
481
- schema : { " $ref" : " #/components/schemas/" + route . model ! . identity }
496
+ schema : { ' $ref' : ` #/components/schemas/${ route . model ! . identity } ` }
482
497
} ,
483
498
} ,
484
499
} ;
485
500
}
486
501
} ,
487
502
503
+ addModelBodyParamUpdate : ( ) => {
504
+ if ( route . actionType === 'shortcutBlueprint' ) {
505
+ const schema = specification ! . components ! . schemas ?. [ route . model ! . identity + '-without-required-constraint' ] ;
506
+ if ( schema ) {
507
+ const resolvedSchema = resolveRef ( specification , schema ) ;
508
+ if ( resolvedSchema ) {
509
+ generateSchemaAsQueryParameters ( resolvedSchema as OpenApi . UpdatedSchema ) . map ( p => {
510
+ if ( isParam ( 'query' , p . name ) ) return ;
511
+ pathEntry . parameters . push ( p ) ;
512
+ } ) ;
513
+ }
514
+ }
515
+ } else {
516
+ if ( pathEntry . requestBody ) return ;
517
+ pathEntry . requestBody = {
518
+ description : subst ( 'JSON dictionary representing the {globalId} instance to update.' ) ,
519
+ required : true ,
520
+ content : {
521
+ 'application/json' : {
522
+ schema : { '$ref' : `#/components/schemas/${ route . model ! . identity } -without-required-constraint` }
523
+ } ,
524
+ } ,
525
+ }
526
+ }
527
+ } ,
528
+
488
529
addResultOfArrayOfModels : ( ) => {
489
530
assign ( pathEntry . responses [ '200' ] , {
490
531
description : subst ( template . resultDescription ) ,
0 commit comments