1
- import { SwaggerSailsModel , NameKeyMap , SwaggerRouteInfo , BlueprintActionTemplates , Defaults , MiddlewareType , SwaggerSailsController , Action2Response } from './interfaces' ;
1
+ import { SwaggerSailsModel , NameKeyMap , SwaggerRouteInfo , BlueprintActionTemplates , Defaults , MiddlewareType , Action2Response } from './interfaces' ;
2
2
import { Reference , Tag } from 'swagger-schema-official' ;
3
3
import get from 'lodash/get' ;
4
4
import { swaggerTypes , sailsAttributePropertiesMap , validationsMap , actions2Responses } from './type-formatter' ;
@@ -12,7 +12,8 @@ import isFunction from 'lodash/isFunction';
12
12
import forEach from 'lodash/forEach' ;
13
13
import { OpenApi } from '../types/openapi' ;
14
14
import set from 'lodash/set' ;
15
- import { map } from 'lodash' ;
15
+ import { map , omit , find , isEqual , filter , reduce } from 'lodash' ;
16
+ import { attributeValidations } from './utils' ;
16
17
17
18
/**
18
19
* Maps from a Sails route path of the form `/path/:id` to a
@@ -206,14 +207,21 @@ export const generateModelAssociationFKAttributeSchemas = (model: SwaggerSailsMo
206
207
207
208
/**
208
209
* Generate Swagger schema content describing specified Sails models.
210
+ *
209
211
* @see https://swagger.io/docs/specification/data-models/
212
+ *
210
213
* @param models parsed Sails models as per `parsers.parseModels()`
211
214
* @returns
212
215
*/
213
216
export const generateSchemas = ( models : NameKeyMap < SwaggerSailsModel > ) : NameKeyMap < OpenApi . UpdatedSchema | Reference > => {
214
217
return Object . keys ( models )
215
- . reduce ( ( schemas , identiy ) => {
216
- const model = models [ identiy ]
218
+ . reduce ( ( schemas , identity ) => {
219
+ const model = models [ identity ]
220
+
221
+ if ( model . swagger ?. modelSchema ?. exclude === true ) {
222
+ return schemas ;
223
+ }
224
+
217
225
const schema : OpenApi . UpdatedSchema = {
218
226
description : get ( model , 'swagger.description' , `Sails ORM Model **${ model . globalId } **` ) ,
219
227
required : [ ] ,
@@ -222,8 +230,10 @@ export const generateSchemas = (models: NameKeyMap<SwaggerSailsModel>): NameKeyM
222
230
const attributes = model . attributes || { }
223
231
schema . properties = Object . keys ( attributes ) . reduce ( ( props , attributeName ) => {
224
232
const attribute = model . attributes [ attributeName ] ;
225
- props [ attributeName ] = generateAttributeSchema ( attribute ) ;
226
- if ( attribute . required ) schema . required ! . push ( attributeName ) ;
233
+ if ( attribute . meta ?. swagger ?. exclude !== true ) {
234
+ props [ attributeName ] = generateAttributeSchema ( attribute ) ;
235
+ if ( attribute . required ) schema . required ! . push ( attributeName ) ;
236
+ }
227
237
return props
228
238
} , { } as NameKeyMap < OpenApi . UpdatedSchema > )
229
239
@@ -257,28 +267,25 @@ export const generatePaths = (routes: SwaggerRouteInfo[], templates: BlueprintAc
257
267
components . parameters = { }
258
268
}
259
269
260
- const addTags = ( toAdd : Array < Tag > ) => {
261
- const newTags = toAdd . filter ( newTag => tags . find ( tag => newTag . name === tag . name ) ) ;
262
- tags . push ( ...newTags )
263
- }
264
270
265
- const mergeComponents = ( toMerge : OpenApi . Components ) => {
266
- for ( const key in toMerge ) {
267
- const componentName = key as keyof OpenApi . Components
268
- if ( ! components [ componentName ] ) {
269
- components [ componentName ] = { } ;
270
- }
271
- defaults ( components [ componentName ] , toMerge [ componentName ] ) ;
271
+ forEach ( routes , route => {
272
+
273
+ if ( route . swagger ?. exclude === true ) {
274
+ return ;
272
275
}
273
- }
274
276
275
- for ( const route of routes ) {
276
- const path = route . path ;
277
277
let pathEntry : OpenApi . Operation ;
278
278
279
279
if ( route . middlewareType === MiddlewareType . BLUEPRINT && route . model ) {
280
+
281
+ if ( route . model . swagger ?. modelSchema ?. exclude === true ) {
282
+ return ;
283
+ }
284
+
280
285
const template = templates [ route . action as keyof BlueprintActionTemplates ] || { } ;
286
+
281
287
const subst = ( str : string ) => str ? str . replace ( '{globalId}' , route . model ! . globalId ) : '' ;
288
+
282
289
// handle special case of PK parameter
283
290
const parameters = [ ...( template . parameters || [ ] ) ]
284
291
. map ( parameter => {
@@ -589,11 +596,9 @@ export const generatePaths = (routes: SwaggerRouteInfo[], templates: BlueprintAc
589
596
} ) ;
590
597
}
591
598
592
- addTags ( get ( route . swagger , 'tags' , [ ] ) as Tag [ ] ) ;
593
- mergeComponents ( get ( route . swagger , 'components' , { } ) ) ;
594
599
595
- set ( paths , [ path , route . verb ] , pathEntry ) ;
596
- }
600
+ set ( paths , [ route . path , route . verb ] , pathEntry ) ;
601
+ } ) ;
597
602
598
603
return paths
599
604
}
0 commit comments