Repository: BRIKEV/express-jsdoc-swagger
Pull request #108 (2020-11-14 17:32:24)

Example error message
### What kind of change does this PR introduce? (check at least one)
 - [X] Bugfix
 - [X] Feature
 - [X] Test
 - [X] Docs

### Description:

This PR includes:
- Examples format test.
- Fix example file of OpenAPI responses with examples.
- Remove catch linter rule.



Patch:
diff --git a/.eslintrc.json b/.eslintrc.json
index 4234403..fa0c9db 100644
--- a/.eslintrc.json
+++ b/.eslintrc.json
@@ -28,9 +28,6 @@
     "jest/no-focused-tests": "error",
     "jest/no-identical-title": "error",
     "jest/prefer-to-have-length": "warn",
-    "jest/valid-expect": "error",
-    "no-empty": ["error", {
-      "allowEmptyCatch": true
-    }]
+    "jest/valid-expect": "error"
   }
 }
diff --git a/examples/responses/withExamples.js b/examples/responses/withExamples.js
index 1985105..3840a89 100644
--- a/examples/responses/withExamples.js
+++ b/examples/responses/withExamples.js
@@ -18,7 +18,8 @@ const options = {
 const app = express();
 const port = 3000;
 
-expressJSDocSwagger(app)(options);
+const instance = expressJSDocSwagger(app)(options);
+instance.on('finish', data => console.log('%cwithExamples.js line:22 data', 'color: #007acc;', JSON.stringify(data, null, 2)))
 
 /**
  * A pet
@@ -36,7 +37,7 @@ expressJSDocSwagger(app)(options);
  * [
  *   {
  *     "name": "Blaze",
- *     "type": "Dog"
+ *     "type": "Dog",
  *     "breed": "Siberian husky"
  *   },
  *   {
@@ -48,13 +49,13 @@ expressJSDocSwagger(app)(options);
  * [
  *   {
  *     "name": "Hachiko",
- *     "type": "Dog"
+ *     "type": "Dog",
  *     "breed": "Akita Inu"
- *   },
+ *   }
  * ]
  * @example response - 403 - example error response
  * {
- *   "message": "you cannot access pet data",
+ *   "message": "you cannot access pet data"
  * }
  */
 app.get('/api/v1/pet', (req, res) => (
diff --git a/transforms/paths/responses.js b/transforms/paths/responses.js
index 0139783..fcce777 100644
--- a/transforms/paths/responses.js
+++ b/transforms/paths/responses.js
@@ -3,6 +3,8 @@ const STATUS_CODES = require('./validStatusCodes');
 const mapDescription = require('../utils/mapDescription');
 const getContent = require('./content')('@return');
 
+const DEFAULT_CONTENT_TYPE = 'application/json';
+
 const hasOldContent = (value, status) => (value[status] && value[status].content);
 
 const formatResponses = (values, examples) => values.reduce((acc, value) => {
@@ -13,13 +15,15 @@ const formatResponses = (values, examples) => values.reduce((acc, value) => {
   }
 
   const exampleList = examples[status];
-  if (exampleList && contentType === 'application/json') {
+  if (exampleList && (contentType === DEFAULT_CONTENT_TYPE || !contentType)) {
     Object.keys(exampleList)
       .filter(k => typeof exampleList[k].value === 'string')
       .forEach(k => {
         try {
           exampleList[k].value = JSON.parse(exampleList[k].value);
-        } catch (ignored) { }
+        } catch (err) {
+          errorMessage(`response example for status ${status} with content-type ${contentType} malformed`);
+        }
       });
   }
 


Test patch:
diff --git a/test/transforms/paths/responses.test.js b/test/transforms/paths/responses.test.js
index b81137d..dcc4161 100644
--- a/test/transforms/paths/responses.test.js
+++ b/test/transforms/paths/responses.test.js
@@ -1,3 +1,4 @@
+const chalk = require('chalk');
 const jsdocInfo = require('../../../consumers/jsdocInfo');
 const setPaths = require('../../../transforms/paths');
 
@@ -371,6 +372,37 @@ describe('response tests', () => {
     const result = setPaths({}, parsedJSDocs);
     expect(result).toEqual(expected);
   });
+
+  it('should not parse jsdoc path response with examples when an application/json example is malformed', () => {
+    const jsdocInput = [`
+      /**
+       * GET /api/v1
+       * @summary This is the summary or description of the endpoint
+       * @return {Song} 200 - success response - application/json
+       * @return {object} 403 - forbidden response - application/json
+       * @example response - 200 - example success response
+       * {
+       *   "title": "untitled song",
+       *   "artist": "anonymous"
+       * }
+       * @example response - 403 - example error response
+       * {
+       *   "error": "failed to retrieve results",
+       * }
+       */
+    `];
+    global.console = { ...global.console, warn: jest.fn() };
+    const parsedJSDocs = jsdocInfo()(jsdocInput);
+    setPaths({}, parsedJSDocs);
+    // eslint-disable-next-line
+    expect(console.warn).toHaveBeenCalledTimes(1);
+    // eslint-disable-next-line
+    expect(console.warn).toHaveBeenNthCalledWith(
+      1,
+      chalk.yellow('[express-jsdoc-swagger] response example for status 403 with content-type application/json malformed'),
+    );
+  });
+
   it('should parse undefined if example has no valid types (request or response)', () => {
     const jsdocInput = [`
       /**
@@ -438,6 +470,7 @@ describe('response tests', () => {
     const result = setPaths({}, parsedJSDocs);
     expect(result).toEqual(expected);
   });
+
   it('should not parse an example if has no valid status', () => {
     const jsdocInput = [`
       /**