Repository: BRIKEV/express-jsdoc-swagger
Pull request #104 (2020-11-09 07:04:54)

feat: try to convert json string to object for response example tags
### What kind of change does this PR introduce? (check at least one)
 - [ ] Bugfix
 - [X] Feature
 - [x] Test
 - [ ] Docs
 - [ ] Refactor
 - [ ] Build-related changes
 - [ ] Other, please describe:

### Description:

I try to parse the response example in case it's content type is "application/json". 

kevinccbsg: @all-contributors please add @thuydx55 for code
allcontributors[bot]: @kevinccbsg 

I've put up [a pull request](https://github.com/BRIKEV/express-jsdoc-swagger/pull/107) to add @thuydx55! :tada:

Patch:
diff --git a/.eslintrc.json b/.eslintrc.json
index fa0c9db..4234403 100644
--- a/.eslintrc.json
+++ b/.eslintrc.json
@@ -28,6 +28,9 @@
     "jest/no-focused-tests": "error",
     "jest/no-identical-title": "error",
     "jest/prefer-to-have-length": "warn",
-    "jest/valid-expect": "error"
+    "jest/valid-expect": "error",
+    "no-empty": ["error", {
+      "allowEmptyCatch": true
+    }]
   }
 }
diff --git a/consumers/utils/getComments.js b/consumers/utils/getComments.js
index b525fb4..a2d9668 100644
--- a/consumers/utils/getComments.js
+++ b/consumers/utils/getComments.js
@@ -1,4 +1,3 @@
-
 const COMMENTS_PATTERN = /((\/\*+[\s\S]*?\*\/)|(\/\*+.*\*\/)|^\/\/.*?[\r\n])[\r\n]*/gm;
 const BREAK_LINE = /\n/g;
 
diff --git a/transforms/components/index.js b/transforms/components/index.js
index 96cb244..ac88eab 100644
--- a/transforms/components/index.js
+++ b/transforms/components/index.js
@@ -57,7 +57,6 @@ const getRequiredProperties = properties => (
 
 const formatRequiredProperties = requiredProperties => requiredProperties.map(getPropertyName);
 
-
 const parseSchema = schema => {
   const typedef = getTagInfo(schema.tags, 'typedef');
   const propertyValues = getTagsInfo(schema.tags, 'property');
diff --git a/transforms/paths/responses.js b/transforms/paths/responses.js
index 050b2e0..0139783 100644
--- a/transforms/paths/responses.js
+++ b/transforms/paths/responses.js
@@ -11,13 +11,25 @@ const formatResponses = (values, examples) => values.reduce((acc, value) => {
     errorMessage(`Status ${status} is not valid to create a response`);
     return {};
   }
+
+  const exampleList = examples[status];
+  if (exampleList && contentType === 'application/json') {
+    Object.keys(exampleList)
+      .filter(k => typeof exampleList[k].value === 'string')
+      .forEach(k => {
+        try {
+          exampleList[k].value = JSON.parse(exampleList[k].value);
+        } catch (ignored) { }
+      });
+  }
+
   return {
     ...acc,
     [status]: {
       description,
       content: {
         ...(hasOldContent(acc, status) ? { ...acc[status].content } : {}),
-        ...getContent(value.type, contentType, value.description, examples[status]),
+        ...getContent(value.type, contentType, value.description, exampleList),
       },
     },
   };
diff --git a/transforms/paths/validStatusCodes.js b/transforms/paths/validStatusCodes.js
index 3ad1c12..7bf1fc7 100644
--- a/transforms/paths/validStatusCodes.js
+++ b/transforms/paths/validStatusCodes.js
@@ -1,4 +1,3 @@
-
 const STATUS_CODES = {
   202: 'Accepted',
   502: 'Bad Gateway',
diff --git a/transforms/security/index.js b/transforms/security/index.js
index 3ac90be..032dce5 100644
--- a/transforms/security/index.js
+++ b/transforms/security/index.js
@@ -1,4 +1,3 @@
-
 const formatSecurity = securitySchemes => {
   const securityTypes = Object.keys(securitySchemes);
   return securityTypes.map(type => ({ [type]: [] }));
diff --git a/transforms/utils/arrays.js b/transforms/utils/arrays.js
index cfd352c..32b71e6 100644
--- a/transforms/utils/arrays.js
+++ b/transforms/utils/arrays.js
@@ -1,4 +1,3 @@
-
 const flatArray = elements => (
   elements.reduce((acc, val) => acc.concat(val), [])
 );
diff --git a/transforms/utils/enumValues.js b/transforms/utils/enumValues.js
index b4e7449..f9a5192 100644
--- a/transforms/utils/enumValues.js
+++ b/transforms/utils/enumValues.js
@@ -1,4 +1,3 @@
-
 const addEnumValues = (values = []) => {
   if (values.length === 0) return {};
   return { enum: values };
diff --git a/transforms/utils/httpMethods.js b/transforms/utils/httpMethods.js
index 9a82bb2..b52b41d 100644
--- a/transforms/utils/httpMethods.js
+++ b/transforms/utils/httpMethods.js
@@ -1,4 +1,3 @@
-
 const validRequestBodyMethods = {
   get: false,
   delete: false,
diff --git a/transforms/utils/mapDescription.js b/transforms/utils/mapDescription.js
index 137596f..33d7f97 100644
--- a/transforms/utils/mapDescription.js
+++ b/transforms/utils/mapDescription.js
@@ -1,4 +1,3 @@
-
 const DECRIPTION_DIVIDER = ' - ';
 
 const mapDescription = description => (description || '').split(DECRIPTION_DIVIDER);


Test patch:
diff --git a/test/e2e/components/jsdoc-example.js b/test/e2e/components/jsdoc-example.js
index c129b99..54a6e41 100644
--- a/test/e2e/components/jsdoc-example.js
+++ b/test/e2e/components/jsdoc-example.js
@@ -1,4 +1,3 @@
-
 /**
  * A song
  * @typedef {object} Song
diff --git a/test/e2e/errors/jsdoc-parameter-error.js b/test/e2e/errors/jsdoc-parameter-error.js
index b066483..c55eb10 100644
--- a/test/e2e/errors/jsdoc-parameter-error.js
+++ b/test/e2e/errors/jsdoc-parameter-error.js
@@ -1,4 +1,3 @@
-
 /**
  * GET /api/v1/album
  * @summary This is the summary or description of the endpoint
diff --git a/test/e2e/errors/jsdoc-requestBody-error.js b/test/e2e/errors/jsdoc-requestBody-error.js
index f1e2903..9d1fe14 100644
--- a/test/e2e/errors/jsdoc-requestBody-error.js
+++ b/test/e2e/errors/jsdoc-requestBody-error.js
@@ -1,4 +1,3 @@
-
 /**
  * GET /api/v1/album
  * @summary This is the summary or description of the endpoint
diff --git a/test/e2e/errors/jsdoc-response-error.js b/test/e2e/errors/jsdoc-response-error.js
index 5638698..5ec1289 100644
--- a/test/e2e/errors/jsdoc-response-error.js
+++ b/test/e2e/errors/jsdoc-response-error.js
@@ -1,4 +1,3 @@
-
 /**
  * GET /api/v1/album
  * @summary This is the summary or description of the endpoint
diff --git a/test/transforms/paths/responses.test.js b/test/transforms/paths/responses.test.js
index e66d810..b81137d 100644
--- a/test/transforms/paths/responses.test.js
+++ b/test/transforms/paths/responses.test.js
@@ -335,7 +335,10 @@ describe('response tests', () => {
                     examples: {
                       example1: {
                         summary: 'example success response',
-                        value: '{\n  "title": "untitled song",\n  "artist": "anonymous"\n}',
+                        value: {
+                          title: 'untitled song',
+                          artist: 'anonymous',
+                        },
                       },
                     },
                   },
@@ -351,7 +354,9 @@ describe('response tests', () => {
                     examples: {
                       example2: {
                         summary: 'example error response',
-                        value: '{\n  "error": "failed to retrieve results"\n}',
+                        value: {
+                          error: 'failed to retrieve results',
+                        },
                       },
                     },
                   },
@@ -404,7 +409,10 @@ describe('response tests', () => {
                     examples: {
                       example1: {
                         summary: 'example success response',
-                        value: '{\n  "title": "untitled song",\n  "artist": "anonymous"\n}',
+                        value: {
+                          title: 'untitled song',
+                          artist: 'anonymous',
+                        },
                       },
                     },
                   },
@@ -468,7 +476,10 @@ describe('response tests', () => {
                     examples: {
                       example1: {
                         summary: 'example success response',
-                        value: '{\n  "title": "untitled song",\n  "artist": "anonymous"\n}',
+                        value: {
+                          title: 'untitled song',
+                          artist: 'anonymous',
+                        },
                       },
                     },
                   },