Skip to content

Commit eafbed4

Browse files
feat: Add option to excluded deprecated PUT update blueprint (default) (#73)
* feat: Add option to excluded deprecated `PUT update` blueprint (default) Sails 1.0 includes `PUT` and `PATCH` routes to the `update` blueprint although `PUT` deprecated; default to excluding the `PUT` route. See - https://sailsjs.com/documentation/reference/blueprint-api/update#?notes - https://github.com/balderdashy/sails/blob/master/lib/hooks/blueprints/index.js#L401 Co-authored-by: Theophilus Omoregbee <[email protected]>
1 parent f60e61a commit eafbed4

File tree

5 files changed

+24
-99
lines changed

5 files changed

+24
-99
lines changed

README.MD

+4
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ module.exports['swagger-generator'] = {
8585
'500': { description: 'Internal server error' }
8686
}
8787
},
88+
excludeDeprecatedPutBlueprintRoutes: true,
8889
includeRoute: function(routeInfo) { return true; },
8990
updateBlueprintActionTemplates: function(blueprintActionTemplates) { ... },
9091
postProcess: function(specifications) { ... }
@@ -100,6 +101,9 @@ Notes on the use of configuration:
100101
Generally, this hook provides sensible defaults for as much as possible but you may
101102
override them in this location or in any of the mechanisms explained below.
102103
* `defaults` object should contain the `responses` element; defaults to the above if not specified.
104+
* `excludeDeprecatedPutBlueprintRoutes` should
105+
[deprecated](https://sailsjs.com/documentation/reference/blueprint-api/update#?notes) `PUT` blueprint
106+
routes be excluded from generated Swagger output; defaults to `true`.
103107
* `includeRoute` function used to filter routes to be included in generated Swagger output; see advanced section below.
104108
* `updateBlueprintActionTemplates` allows customisation of the templates used to generate Swagger for blueprints; see advanced section below.
105109
* `postProcess` allows modification of the generated Swagger output before it is written to the output file; see advanced section below.

index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ module.exports = (sails: Sails.Sails): Sails.Hook<SwaggerGenerator> => {
3030
{ url: 'http://localhost:1337/' }
3131
],
3232
externalDocs: { url: 'https://theoomoregbee.github.io/' }
33-
}
33+
},
34+
excludeDeprecatedPutBlueprintRoutes: true,
3435
}
3536
},
3637
// Run when sails loads-- be sure and call `next()`.

lib/interfaces.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import { OpenApi } from '../types/openapi';
33
import { Reference, Tag, ExternalDocs } from 'swagger-schema-official';
44

55
export interface SwaggerGenerator {
6-
includeRoute?: (route: SwaggerRouteInfo) => boolean;
7-
disabled?: boolean;
8-
swaggerJsonPath: string;
9-
swagger: Omit<OpenApi.OpenApi, 'paths'>;
10-
updateBlueprintActionTemplates?: (template: BlueprintActionTemplates) => BlueprintActionTemplates;
11-
defaults?: Defaults;
12-
postProcess?: (specification: OpenApi.OpenApi) => void;
6+
includeRoute?: (route: SwaggerRouteInfo) => boolean;
7+
disabled?: boolean;
8+
swaggerJsonPath: string;
9+
swagger: Omit<OpenApi.OpenApi, 'paths'>;
10+
updateBlueprintActionTemplates?: (template: BlueprintActionTemplates) => BlueprintActionTemplates;
11+
defaults?: Defaults;
12+
postProcess?: (specification: OpenApi.OpenApi) => void;
13+
excludeDeprecatedPutBlueprintRoutes?: boolean;
1314
}
1415

1516
/**

lib/swagger-doc.ts

+10
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ export default async (sails: Sails.Sails, sailsRoutes: Array<Sails.Route>, conte
5757
routes = routes.filter(route => hookConfig.includeRoute!(route));
5858
}
5959

60+
/*
61+
* Sails 1.0 includes `PUT` and `PATCH` routes to the `update` blueprint although `PUT` deprecated;
62+
* default to excluding the `PUT` route.
63+
* @see https://sailsjs.com/documentation/reference/blueprint-api/update#?notes
64+
* @see https://github.com/balderdashy/sails/blob/master/lib/hooks/blueprints/index.js#L401
65+
*/
66+
if(hookConfig.excludeDeprecatedPutBlueprintRoutes) {
67+
routes = routes.filter(route => !(route.actionType === 'blueprint' && route.action === 'update' && route.verb === 'put'));
68+
}
69+
6070
mergeModelJsDoc(models, modelsJsDoc);
6171
mergeControllerJsDoc(controllers, controllersJsDoc);
6272

test/fixtures/generatedSwagger.json

-91
Original file line numberDiff line numberDiff line change
@@ -2112,51 +2112,6 @@
21122112
"description": "Internal server error"
21132113
}
21142114
}
2115-
},
2116-
"put": {
2117-
"summary": "Update Pet",
2118-
"description": "Update an existing **Pet** record.",
2119-
"externalDocs": {
2120-
"url": "https://sailsjs.com/documentation/reference/blueprint-api/update",
2121-
"description": "See https://sailsjs.com/documentation/reference/blueprint-api/update"
2122-
},
2123-
"tags": [
2124-
"Pet"
2125-
],
2126-
"parameters": [
2127-
{
2128-
"$ref": "#/components/parameters/ModelPKParam-pet"
2129-
}
2130-
],
2131-
"responses": {
2132-
"200": {
2133-
"description": "Responds with the newly updated **Pet** record as a JSON dictionary",
2134-
"content": {
2135-
"application/json": {
2136-
"schema": {
2137-
"$ref": "#/components/schemas/pet"
2138-
}
2139-
}
2140-
}
2141-
},
2142-
"404": {
2143-
"description": "Cannot update, **Pet** record with specified ID **NOT** found"
2144-
},
2145-
"500": {
2146-
"description": "Internal server error"
2147-
}
2148-
},
2149-
"requestBody": {
2150-
"description": "JSON dictionary representing the Pet instance to update.",
2151-
"required": true,
2152-
"content": {
2153-
"application/json": {
2154-
"schema": {
2155-
"$ref": "#/components/schemas/pet-without-required-constraint"
2156-
}
2157-
}
2158-
}
2159-
}
21602115
}
21612116
},
21622117
"/pet/{_petID}/{association}": {
@@ -2396,52 +2351,6 @@
23962351
"description": "Internal server error"
23972352
}
23982353
}
2399-
},
2400-
"put": {
2401-
"summary": "Update User",
2402-
"description": "Update an existing **User** record.",
2403-
"externalDocs": {
2404-
"url": "https://somewhere.com/yep",
2405-
"description": "Refer to these docs for more info"
2406-
},
2407-
"tags": [
2408-
"User (ORM duplicate)",
2409-
"User (ORM)"
2410-
],
2411-
"parameters": [
2412-
{
2413-
"$ref": "#/components/parameters/ModelPKParam-user"
2414-
}
2415-
],
2416-
"responses": {
2417-
"200": {
2418-
"description": "Responds with the newly updated **User** record as a JSON dictionary",
2419-
"content": {
2420-
"application/json": {
2421-
"schema": {
2422-
"$ref": "#/components/schemas/user"
2423-
}
2424-
}
2425-
}
2426-
},
2427-
"404": {
2428-
"description": "Cannot update, **User** record with specified ID **NOT** found"
2429-
},
2430-
"500": {
2431-
"description": "Internal server error"
2432-
}
2433-
},
2434-
"requestBody": {
2435-
"description": "JSON dictionary representing the User instance to update.",
2436-
"required": true,
2437-
"content": {
2438-
"application/json": {
2439-
"schema": {
2440-
"$ref": "#/components/schemas/user-without-required-constraint"
2441-
}
2442-
}
2443-
}
2444-
}
24452354
}
24462355
},
24472356
"/user/{_id}/{association}/{childid}": {

0 commit comments

Comments
 (0)