Skip to content

Commit 3a6b1bc

Browse files
feat: Update postProcess config to allow alternate save mechanism (#91)
Update `postProcess` configuration to run after tag clean-up phase to allow an alternate mechanism for saving the generated Swagger to be specified, rather than the standard writing a file to the filesystem. Allow `swaggerJsonPath` to be empty/null/undefined, in which case no file write attempted. This supports deployment to environments that do not allow writing to the filesystem e.g. store in memory or database.
1 parent 3746532 commit 3a6b1bc

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

README.MD

+16-4
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ By default, the Swagger Generator Sails Hook generates:
4242

4343
## Use with [Swagger UI](https://github.com/swagger-api/swagger-ui)
4444

45-
See [#28](https://github.com/theoomoregbee/sails-hook-swagger-generator/issues/28)
45+
See [#28](https://github.com/theoomoregbee/sails-hook-swagger-generator/issues/28)
4646

4747
## Adding/Customising Generated Output
4848

@@ -100,7 +100,8 @@ module.exports['swagger-generator'] = {
100100
Notes on the use of configuration:
101101

102102
* `disabled` attribute is used to disable the module (e.g you may want to disable it on production).
103-
* `swaggerJsonPath` where to generate the `swagger.json` file to; defaults to `sails.config.appPath + '/swagger/swagger.json'`.
103+
* `swaggerJsonPath` where to generate the `swagger.json` file to; defaults to `sails.config.appPath + '/swagger/swagger.json'`
104+
and output file will not be written if empty/null/undefined (see `postProcess` below for alternate save mechanism).
104105
* `swagger` object is template for the Swagger/OpenAPI output. It defaults to the minimal content above.
105106
Check Swagger/OpenAPI specification for more, in case you want to extend it.
106107
Generally, this hook provides sensible defaults for as much as possible but you may
@@ -111,7 +112,8 @@ Notes on the use of configuration:
111112
routes be excluded from generated Swagger output; defaults to `true`.
112113
* `includeRoute` function used to filter routes to be included in generated Swagger output; see advanced section below.
113114
* `updateBlueprintActionTemplates` allows customisation of the templates used to generate Swagger for blueprints; see advanced section below.
114-
* `postProcess` allows modification of the generated Swagger output before it is written to the output file; see advanced section below.
115+
* `postProcess` allows an alternate mechanism for saving and/or modification of the generated Swagger output before it is written to
116+
the output file; see advanced section below.
115117

116118

117119
## Custom Route Configuration
@@ -454,6 +456,10 @@ Tags are added to the top-level Swagger/OpenAPI definitions as follows:
454456
1. Where a tag with the specified name **does** exist, elements _of that tag_ that do not exist are added
455457
e.g. `description` and `externalDocs` elements.
456458

459+
Note that a final *clean-up* phase is run after processing, which performs the following:
460+
1. Removal of unreferenced tags; and
461+
2. Creation of tags referenced but are not defined.
462+
457463
### Component Element Handling
458464

459465
Elements of components are added to the top-level Swagger/OpenAPI definitions as follows:
@@ -494,7 +500,7 @@ let componentDefinitionReference = {
494500
Three mechanisms are provided to enable advancing filtering of the Swagger generation process:
495501
1. An `includeRoute()` function used to filter routes to be included in generated Swagger output.
496502
1. An `updateBlueprintActionTemplates()` function allows customisation of the templates used to generate Swagger for blueprints.
497-
1. A `postProcess()` function allows modification of the generated Swagger output before it is written to the output file.
503+
1. A `postProcess()` function allows an alternate mechanism for saving and/or modification of the generated Swagger output before it is written to the output file.
498504

499505
Each is configured in `config/swaggergenerator.js`.
500506

@@ -621,6 +627,12 @@ Note that:
621627
The final generated Swagger output may be post-processed before it is written to
622628
the output file using a post-processing function specified as the `postProcess` config option.
623629

630+
For situations where saving the generated swagger documentation JSON to a file is
631+
not desired/appropriate, the `postProcess` config option may be used to specify
632+
an alternate save mechanism.
633+
634+
Note that if `swaggerJsonPath` config option is empty/null/undefined the output file will not be written.
635+
624636
For example:
625637
```javascript
626638
module.exports['swagger-generator'] = {

lib/swagger-doc.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,6 @@ export default async (sails: Sails.Sails, sailsRoutes: Array<Sails.Route>, conte
9696

9797
defaults(specifications.components!.parameters, blueprintParameterTemplates);
9898

99-
if (hookConfig.postProcess) hookConfig.postProcess(specifications);
100-
10199
// clean up of specification, removing unreferenced tags
102100
const referencedTags = getUniqueTagsFromPath(specifications.paths);
103101

@@ -118,11 +116,17 @@ export default async (sails: Sails.Sails, sailsRoutes: Array<Sails.Route>, conte
118116
}
119117
});
120118

119+
if (hookConfig.postProcess) {
120+
hookConfig.postProcess(specifications);
121+
}
122+
121123
const destPath = hookConfig.swaggerJsonPath;
122-
try {
123-
fs.writeFileSync(destPath, JSON.stringify(specifications, null, 2));
124-
} catch (e) {
125-
sails.log.error(`ERROR: sails-hook-swagger-generator: Error writing ${destPath}: ${e.message}`, e);
124+
if (destPath) {
125+
try {
126+
fs.writeFileSync(destPath, JSON.stringify(specifications, null, 2));
127+
} catch (e) {
128+
sails.log.error(`ERROR: sails-hook-swagger-generator: Error writing ${destPath}: ${e.message}`, e);
129+
}
126130
}
127131

128132
sails.log.info('Swagger generated successfully');

0 commit comments

Comments
 (0)