serverless-openapi-documenter 0.0.97 → 0.0.101
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +25 -8
- package/package.json +1 -1
- package/src/definitionGenerator.js +15 -17
- package/src/openAPIGenerator.js +33 -21
- package/test/helpers/redocly.json +4 -0
- package/test/unit/definitionGenerator.spec.js +749 -587
- package/test/unit/openAPIGenerator.spec.js +10 -0
package/README.md
CHANGED
|
@@ -48,7 +48,7 @@ Options:
|
|
|
48
48
|
--format -f Whether to output the OpenAPI Description as json or yaml. Default: json
|
|
49
49
|
--indent -i File indentation in spaces. Default: 2
|
|
50
50
|
--openApiVersion -a OpenAPI version to generate for. Default: 3.0.0
|
|
51
|
-
--postmanCollection -p Will generate a postman collection (from the generated OpenAPI Description), in json only, if passed in. Default postman.json
|
|
51
|
+
--postmanCollection -p Will generate a postman collection (from the generated OpenAPI Description), in json only, if passed in. Default: postman.json
|
|
52
52
|
--validationWarn -w Warn about validation errors only. Will write the OpenAPI file if generation is successful. Default: false
|
|
53
53
|
```
|
|
54
54
|
|
|
@@ -942,16 +942,33 @@ This will set the `Cache-Control` Response Header to have a value of "no-store"
|
|
|
942
942
|
|
|
943
943
|
Validation for the OpenAPI Description is now (as of 0.0.90) done by [Redocly](https://redocly.com/). This is a slightly less opinionated validator for an OpenAPI Description, it should result in less errors around "YAML Anchors". It's also a maintained library, and has support for OpenAPI 3.1.0 which I hope to be able to support very soon.
|
|
944
944
|
|
|
945
|
+
I am making use of https://www.npmjs.com/package/@redocly/openapi-core, which I have been warned is likely to change. If you notice anything going wrong with validation of your OpenAPI Description, feel free to open an issue here. I make active use of this library, so will hopefully come across those issues too.
|
|
946
|
+
|
|
947
|
+
### Rules
|
|
948
|
+
|
|
945
949
|
I have configured the validator to use these Rules:
|
|
946
950
|
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
951
|
+
- [spec](https://redocly.com/docs/cli/rules/spec/)
|
|
952
|
+
- [path-parameters-defined](https://redocly.com/docs/cli/rules/path-parameters-defined/)
|
|
953
|
+
- [operation-2xx-response](https://redocly.com/docs/cli/rules/operation-2xx-response/)
|
|
954
|
+
- [operation-4xx-response](https://redocly.com/docs/cli/rules/operation-4xx-response/)
|
|
955
|
+
- [operation-operationId-unique](https://redocly.com/docs/cli/rules/operation-operationid-unique/)
|
|
956
|
+
- [path-declaration-must-exist](https://redocly.com/docs/cli/rules/path-declaration-must-exist/)
|
|
953
957
|
|
|
954
|
-
|
|
958
|
+
However, you can configure your own rules from the [ruleset available on the Redocly site](https://redocly.com/docs/cli/rules/built-in-rules/). To do this, you will need to create a `redocly.json` file within an `options` folder. The file should look like:
|
|
959
|
+
|
|
960
|
+
```json
|
|
961
|
+
{
|
|
962
|
+
"spec": "error",
|
|
963
|
+
"path-parameters-defined": "error",
|
|
964
|
+
"operation-2xx-response": "error",
|
|
965
|
+
"operation-4xx-response": "error",
|
|
966
|
+
"operation-operationId-unique": "error",
|
|
967
|
+
"path-declaration-must-exist": "error"
|
|
968
|
+
}
|
|
969
|
+
```
|
|
970
|
+
|
|
971
|
+
Since rules can be set to "warn", you no longer are required to tell the plugin to ignore errors with the `--validationWarn` flag.
|
|
955
972
|
|
|
956
973
|
## Example configuration
|
|
957
974
|
|
package/package.json
CHANGED
|
@@ -69,6 +69,19 @@ class DefinitionGenerator {
|
|
|
69
69
|
},
|
|
70
70
|
};
|
|
71
71
|
|
|
72
|
+
try {
|
|
73
|
+
this.REDOCLY_RULES = require(path.resolve("options", "redocly.json"));
|
|
74
|
+
} catch (err) {
|
|
75
|
+
this.REDOCLY_RULES = {
|
|
76
|
+
spec: "error",
|
|
77
|
+
"path-parameters-defined": "error",
|
|
78
|
+
"operation-2xx-response": "error",
|
|
79
|
+
"operation-4xx-response": "error",
|
|
80
|
+
"operation-operationId-unique": "error",
|
|
81
|
+
"path-declaration-must-exist": "error",
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
72
85
|
try {
|
|
73
86
|
this.refParserOptions = require(path.resolve("options", "ref-parser.js"));
|
|
74
87
|
} catch (err) {
|
|
@@ -1003,33 +1016,18 @@ class DefinitionGenerator {
|
|
|
1003
1016
|
async validate() {
|
|
1004
1017
|
const config = await createConfig({
|
|
1005
1018
|
apis: {},
|
|
1006
|
-
|
|
1007
|
-
rules: {
|
|
1008
|
-
spec: "error",
|
|
1009
|
-
"path-parameters-defined": "error",
|
|
1010
|
-
"operation-2xx-response": "error",
|
|
1011
|
-
"operation-4xx-response": "error",
|
|
1012
|
-
"operation-operationId-unique": "error",
|
|
1013
|
-
"path-declaration-must-exist": "error",
|
|
1014
|
-
},
|
|
1015
|
-
// },
|
|
1019
|
+
rules: this.REDOCLY_RULES,
|
|
1016
1020
|
});
|
|
1017
1021
|
|
|
1018
1022
|
const apiDesc = stringifyYaml(this.openAPI);
|
|
1019
1023
|
|
|
1020
|
-
|
|
1024
|
+
return await lintFromString({
|
|
1021
1025
|
source: apiDesc,
|
|
1022
1026
|
config: config,
|
|
1023
1027
|
}).catch((err) => {
|
|
1024
1028
|
console.error(err);
|
|
1025
1029
|
throw err;
|
|
1026
1030
|
});
|
|
1027
|
-
|
|
1028
|
-
if (results.length) {
|
|
1029
|
-
throw results;
|
|
1030
|
-
}
|
|
1031
|
-
|
|
1032
|
-
return true;
|
|
1033
1031
|
}
|
|
1034
1032
|
}
|
|
1035
1033
|
|
package/src/openAPIGenerator.js
CHANGED
|
@@ -195,23 +195,29 @@ class OpenAPIGenerator {
|
|
|
195
195
|
|
|
196
196
|
this.log(`Validating generated OpenAPI Description`, this.logTypes.NOTICE);
|
|
197
197
|
|
|
198
|
-
await generator.validate().catch((err) => {
|
|
198
|
+
const validationResults = await generator.validate().catch((err) => {
|
|
199
199
|
this.log(
|
|
200
200
|
`ERROR: An error was thrown validating the OpenAPI v3 Description`,
|
|
201
201
|
this.logTypes.ERROR
|
|
202
202
|
);
|
|
203
203
|
|
|
204
|
-
this.
|
|
204
|
+
throw new this.serverless.classes.Error(err);
|
|
205
|
+
});
|
|
205
206
|
|
|
206
|
-
|
|
207
|
-
let message = "Error validating OpenAPI Description:\r\n";
|
|
208
|
-
for (const errorMessage of err) {
|
|
209
|
-
message += `${errorMessage.message}\r\n`;
|
|
210
|
-
}
|
|
207
|
+
this.validationErrorDetails(validationResults);
|
|
211
208
|
|
|
212
|
-
|
|
209
|
+
if (validationResults.length && this.config.validationWarn === false) {
|
|
210
|
+
let message = "Error validating OpenAPI Description:\r\n";
|
|
211
|
+
let shouldThrow = false;
|
|
212
|
+
for (const error of validationResults) {
|
|
213
|
+
message += `${error.message}\r\n`;
|
|
214
|
+
if (error.severity === "error") {
|
|
215
|
+
shouldThrow = true;
|
|
216
|
+
}
|
|
213
217
|
}
|
|
214
|
-
|
|
218
|
+
|
|
219
|
+
if (shouldThrow) throw new this.serverless.classes.Error(message);
|
|
220
|
+
}
|
|
215
221
|
|
|
216
222
|
this.log(
|
|
217
223
|
"OpenAPI v3 Description Successfully Generated",
|
|
@@ -306,24 +312,30 @@ class OpenAPIGenerator {
|
|
|
306
312
|
this.config = config;
|
|
307
313
|
}
|
|
308
314
|
|
|
309
|
-
validationErrorDetails(
|
|
310
|
-
|
|
311
|
-
`${chalk.bold.yellow(
|
|
312
|
-
"[VALIDATION]"
|
|
313
|
-
)} Validation errors found in OpenAPI Description: \n`,
|
|
314
|
-
this.logTypes.ERROR
|
|
315
|
-
);
|
|
316
|
-
|
|
317
|
-
for (const error of validationError) {
|
|
315
|
+
validationErrorDetails(validationErrors) {
|
|
316
|
+
if (validationErrors.length) {
|
|
318
317
|
this.log(
|
|
319
|
-
`${chalk.bold.yellow(
|
|
318
|
+
`${chalk.bold.yellow(
|
|
319
|
+
"[VALIDATION]"
|
|
320
|
+
)} Validation errors found in OpenAPI Description: \n`,
|
|
320
321
|
this.logTypes.ERROR
|
|
321
322
|
);
|
|
322
|
-
|
|
323
|
+
|
|
324
|
+
for (const error of validationErrors) {
|
|
323
325
|
this.log(
|
|
324
|
-
`${chalk.bold.
|
|
326
|
+
`${chalk.bold.red("Severity:")} ${error.severity}`,
|
|
325
327
|
this.logTypes.ERROR
|
|
326
328
|
);
|
|
329
|
+
this.log(
|
|
330
|
+
`${chalk.bold.yellow("Message:")} ${error.message}`,
|
|
331
|
+
this.logTypes.ERROR
|
|
332
|
+
);
|
|
333
|
+
for (const location of error.location) {
|
|
334
|
+
this.log(
|
|
335
|
+
`${chalk.bold.yellow("found at location:")} ${location.pointer}`,
|
|
336
|
+
this.logTypes.ERROR
|
|
337
|
+
);
|
|
338
|
+
}
|
|
327
339
|
}
|
|
328
340
|
}
|
|
329
341
|
}
|