From 70cffb9b0e799bfe6d63d2dfa9dc3697928c4258 Mon Sep 17 00:00:00 2001 From: Kalin Chernev Date: Fri, 12 Mar 2021 09:25:19 +0100 Subject: [PATCH] Migrate failOnErrors --- CHANGELOG.md | 24 ++++++++++++++++++++++- docs/FIRST-STEPS.md | 3 ++- lerna.json | 2 +- packages/lib/index.js | 1 + packages/lib/package.json | 2 +- packages/lib/src/specification.js | 6 +++++- packages/lib/test/specification.spec.js | 26 +++++++++++++++++++++++++ 7 files changed, 59 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e40c9cc9..9481ad2a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,28 @@ ## Change Log -### v6.0.7 (2021/02/26 14:33 +00:00) +### upcoming (2021/03/12 08:24 +00:00) + +- [#258](https://github.com/Surnet/swagger-jsdoc/pull/258) doc: Add article to materials to inspire you (#258) (@goldsziggy) + +### v6.1.0 (2021/03/11 07:53 +00:00) + +- [e85a78d](https://github.com/Surnet/swagger-jsdoc/commit/e85a78d836105ffc24a9db773a44639f8a544186) Provide an option to allow throwing on parsing errors (@kalinchernev) + +### v6.0.9 (2021/03/07 10:34 +00:00) + +- [ff80f64](https://github.com/Surnet/swagger-jsdoc/commit/ff80f642bd7f30ce861049d2a0b8701b1ea98ba6) Release 6.0.9 (@kalinchernev) +- [#256](https://github.com/Surnet/swagger-jsdoc/pull/256) fix(anchors): applied a fix for anchors living in seperate files with… (#256) (@goldsziggy) + +### v7.0.0-rc.4 (2021/03/01 16:00 +00:00) + +- [15bce04](https://github.com/Surnet/swagger-jsdoc/commit/15bce0415b4dfff7ca21d8931b5498e71a9013ff) correction: v6.0.8 (@kalinchernev) + +### v6.0.8 (2021/03/01 15:20 +00:00) + +- [fc3d62c](https://github.com/Surnet/swagger-jsdoc/commit/fc3d62c61a266b879fd8ab1531f77634645f0247) v6.0.7 (@kalinchernev) +- [#253](https://github.com/Surnet/swagger-jsdoc/pull/253) fix(specification): apply fix for multiple anchors (#253) (@goldsziggy) + +### v7.0.0-rc.3 (2021/02/26 14:33 +00:00) - [8873370](https://github.com/Surnet/swagger-jsdoc/commit/887337050ec3b2d246d2fa9a9bd3570b3746d379) Update docs (@kalinchernev) - [#250](https://github.com/Surnet/swagger-jsdoc/pull/250) Update FIRST-STEPS.md (#250) (@azizkale) diff --git a/docs/FIRST-STEPS.md b/docs/FIRST-STEPS.md index 6521d214..48c926d9 100644 --- a/docs/FIRST-STEPS.md +++ b/docs/FIRST-STEPS.md @@ -87,10 +87,11 @@ The following is an acceptable reference to information from `x-amazon-apigatewa Additional materials to inspire you: +- [De-duping the Duplication in Services Featuring: Swagger/OpenAPI and AJV](https://medium.com/geekculture/de-duping-the-duplication-in-services-featuring-swagger-openapi-and-ajv-abd22c8c764e) - 09/03/2021 - [How to implement and use Swagger in Node.js](https://js.plainenglish.io/how-to-implement-and-use-swagger-in-nodejs-d0b95e765245) - 24/02/2021 - [Document your Javascript code with JSDoc](https://dev.to/paulasantamaria/document-your-javascript-code-with-jsdoc-2fbf) - 20/08/2019 - [Swagger: Time to document that Express API you built!](https://levelup.gitconnected.com/swagger-time-to-document-that-express-api-you-built-9b8faaeae563) - 25/05/2019 - [Express API with autogenerated OpenAPI doc through Swagger](https://www.acuriousanimal.com/blog/2018/10/20/express-swagger-doc) - 20/10/2018 +- [Express API with autogenerated OpenAPI doc through Swagger](https://www.acuriousanimal.com/blog/2018/10/20/express-swagger-doc) - 20/10/2018 - [Swaggerize your API Documentation](http://imaginativethinking.ca/swaggerize-your-api-documentation/) - 01/06/2018 - [Swagger and NodeJS](https://mherman.org/blog/swagger-and-nodejs/) 20/11/2017 - [Agile documentation for your API-driven project](https://kalinchernev.github.io/agile-documentation-api-driven-project) - 21/01/2017 diff --git a/lerna.json b/lerna.json index 0e3ff8ba..ea3baa3f 100644 --- a/lerna.json +++ b/lerna.json @@ -1,5 +1,5 @@ { - "version": "7.0.0-rc.5", + "version": "7.0.0-rc.6", "npmClient": "yarn", "useWorkspaces": true } diff --git a/packages/lib/index.js b/packages/lib/index.js index a1a2536a..20c94bd3 100644 --- a/packages/lib/index.js +++ b/packages/lib/index.js @@ -5,6 +5,7 @@ import { validateOptions } from './src/utils.js'; * Main function * @param {object} options - Configuration options * @param {string} options.encoding Optional, passed to read file function options. Defaults to 'utf8'. + * @param {boolean} options.failOnErrors Whether or not to throw when parsing errors. Defaults to false. * @param {string} options.format Optional, defaults to '.json' - target file format '.yml' or '.yaml'. * @param {object} options.swaggerDefinition * @param {object} options.definition diff --git a/packages/lib/package.json b/packages/lib/package.json index 37723a42..5cadfac9 100644 --- a/packages/lib/package.json +++ b/packages/lib/package.json @@ -1,7 +1,7 @@ { "name": "swagger-jsdoc", "description": "Generates swagger doc based on JSDoc", - "version": "7.0.0-rc.5", + "version": "7.0.0-rc.6", "engines": { "node": ">=12.0.0" }, diff --git a/packages/lib/src/specification.js b/packages/lib/src/specification.js index 5da4f6d9..b419ab0b 100644 --- a/packages/lib/src/specification.js +++ b/packages/lib/src/specification.js @@ -1,6 +1,7 @@ import doctrine from 'doctrine'; import parser from 'swagger-parser'; import YAML from 'yaml'; +import fs from 'fs'; import { convertGlobPaths, @@ -285,7 +286,10 @@ export async function extract(options) { .filter((error) => !!error); if (errReport.length) { - // Place to provide feedback for errors. Previously throwing, now reporting only. + if (options.failOnErrors) { + fs.writeFileSync('bump.txt', errReport); + throw new Error(errReport); + } console.info( 'Not all input has been taken into account at your final specification.' ); diff --git a/packages/lib/test/specification.spec.js b/packages/lib/test/specification.spec.js index 05c421dc..f03538a2 100644 --- a/packages/lib/test/specification.spec.js +++ b/packages/lib/test/specification.spec.js @@ -431,5 +431,31 @@ describe('Specification module', () => { consoleInfo.mockClear(); consoleErr.mockClear(); }); + + it('should throw when failOnErrors is truthy', async () => { + const consoleInfo = jest.spyOn(console, 'info'); + const consoleErr = jest.spyOn(console, 'error'); + + await expect( + extract({ + apis: [`${__dirname}/fixtures/wrong/example.yaml`], + failOnErrors: true, + }) + ).rejects + .toThrow(`YAMLSemanticError: The !!! tag handle is non-default and was not declared. at line 2, column 3: + + !!!title: Hello World + ^^^^^^^^^^^^^^^^^^^^^… + +YAMLSemanticError: Implicit map keys need to be on a single line at line 2, column 3: + + !!!title: Hello World + ^^^^^^^^^^^^^^^^^^^^^… +`); + expect(consoleInfo).not.toHaveBeenCalled(); + expect(consoleInfo).not.toHaveBeenCalled(); + consoleInfo.mockClear(); + consoleErr.mockClear(); + }); }); });