Skip to content

Commit c4dc723

Browse files
danielsharveytheoomoregbee
authored andcommitted
feat(meta.swagger): Add support for swagger.exclude
Add support for excluding attributes/actions based upon respective `swagger.exclude` attributes.
1 parent 911dbb5 commit c4dc723

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

lib/generators.ts

+29-24
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SwaggerSailsModel, NameKeyMap, SwaggerRouteInfo, BlueprintActionTemplates, Defaults, MiddlewareType, SwaggerSailsController, Action2Response } from './interfaces';
1+
import { SwaggerSailsModel, NameKeyMap, SwaggerRouteInfo, BlueprintActionTemplates, Defaults, MiddlewareType, Action2Response } from './interfaces';
22
import { Reference, Tag } from 'swagger-schema-official';
33
import get from 'lodash/get';
44
import { swaggerTypes, sailsAttributePropertiesMap, validationsMap, actions2Responses } from './type-formatter';
@@ -12,7 +12,8 @@ import isFunction from 'lodash/isFunction';
1212
import forEach from 'lodash/forEach';
1313
import { OpenApi } from '../types/openapi';
1414
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';
1617

1718
/**
1819
* Maps from a Sails route path of the form `/path/:id` to a
@@ -206,14 +207,21 @@ export const generateModelAssociationFKAttributeSchemas = (model: SwaggerSailsMo
206207

207208
/**
208209
* Generate Swagger schema content describing specified Sails models.
210+
*
209211
* @see https://swagger.io/docs/specification/data-models/
212+
*
210213
* @param models parsed Sails models as per `parsers.parseModels()`
211214
* @returns
212215
*/
213216
export const generateSchemas = (models: NameKeyMap<SwaggerSailsModel>): NameKeyMap<OpenApi.UpdatedSchema | Reference> => {
214217
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+
217225
const schema: OpenApi.UpdatedSchema = {
218226
description: get(model, 'swagger.description', `Sails ORM Model **${model.globalId}**`),
219227
required: [],
@@ -222,8 +230,10 @@ export const generateSchemas = (models: NameKeyMap<SwaggerSailsModel>): NameKeyM
222230
const attributes = model.attributes || {}
223231
schema.properties = Object.keys(attributes).reduce((props, attributeName) => {
224232
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+
}
227237
return props
228238
}, {} as NameKeyMap<OpenApi.UpdatedSchema>)
229239

@@ -257,28 +267,25 @@ export const generatePaths = (routes: SwaggerRouteInfo[], templates: BlueprintAc
257267
components.parameters = {}
258268
}
259269

260-
const addTags = (toAdd: Array<Tag>) => {
261-
const newTags = toAdd.filter(newTag => tags.find(tag => newTag.name === tag.name));
262-
tags.push(...newTags)
263-
}
264270

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;
272275
}
273-
}
274276

275-
for (const route of routes) {
276-
const path = route.path;
277277
let pathEntry: OpenApi.Operation;
278278

279279
if (route.middlewareType === MiddlewareType.BLUEPRINT && route.model) {
280+
281+
if(route.model.swagger?.modelSchema?.exclude === true) {
282+
return;
283+
}
284+
280285
const template = templates[route.action as keyof BlueprintActionTemplates] || {};
286+
281287
const subst = (str: string) => str ? str.replace('{globalId}', route.model!.globalId) : '';
288+
282289
// handle special case of PK parameter
283290
const parameters = [...(template.parameters || [])]
284291
.map(parameter => {
@@ -589,11 +596,9 @@ export const generatePaths = (routes: SwaggerRouteInfo[], templates: BlueprintAc
589596
});
590597
}
591598

592-
addTags(get(route.swagger, 'tags', []) as Tag[]);
593-
mergeComponents(get(route.swagger, 'components', {}));
594599

595-
set(paths, [path, route.verb], pathEntry);
596-
}
600+
set(paths, [route.path, route.verb], pathEntry);
601+
});
597602

598603
return paths
599604
}

0 commit comments

Comments
 (0)