Repository: BRIKEV/express-jsdoc-swagger
Pull request #6 (2020-05-29 22:49:51)

Response references
This PR includes references to components in the response tags.


Patch:
diff --git a/transforms/components/index.js b/transforms/components/index.js
index 32e5803..48c921b 100644
--- a/transforms/components/index.js
+++ b/transforms/components/index.js
@@ -1,8 +1,7 @@
 const { getTagInfo, getTagsInfo } = require('../utils/tags');
-const validateTypes = require('../utils/validateTypes');
+const refSchema = require('../utils/refSchema');
 
 const REQUIRED = 'required';
-const REF_ROUTE = '#/components/schemas/';
 
 const getPropertyName = ({ name: propertyName }) => {
   const [name] = propertyName.split('.');
@@ -13,14 +12,13 @@ const formatProperties = properties => {
   if (!properties || !Array.isArray(properties)) return {};
   return properties.reduce((acum, property) => {
     const name = getPropertyName(property);
-    const isValidType = validateTypes(property.type.name);
-    const [description, format] = property.description.split(' - ');
     const propertyName = property.type.name;
+    const [description, format] = property.description.split(' - ');
     return {
       ...acum,
       [name]: {
         description,
-        ...(isValidType ? { type: propertyName } : { $ref: `${REF_ROUTE}${propertyName}` }),
+        ...refSchema(propertyName),
         ...(format ? { format } : {}),
       },
     };
diff --git a/transforms/paths/schema.js b/transforms/paths/schema.js
index 5e6a4dc..505edf6 100644
--- a/transforms/paths/schema.js
+++ b/transforms/paths/schema.js
@@ -1,14 +1,16 @@
+const refSchema = require('../utils/refSchema');
 
 const getSchema = type => {
+  const nameType = type.name;
   let schema = {
-    type: type.name,
+    ...refSchema(nameType),
   };
-  const notPrimitiveType = !type.name;
+  const notPrimitiveType = !nameType;
   if (notPrimitiveType) {
     const items = type.applications || {};
     const parseItems = items.reduce((itemAcc, itemTypes) => ({
       ...itemAcc,
-      type: itemTypes.name,
+      ...refSchema(itemTypes.name),
     }), {});
     schema = {
       ...schema,
diff --git a/transforms/utils/refSchema.js b/transforms/utils/refSchema.js
new file mode 100644
index 0000000..904e9c8
--- /dev/null
+++ b/transforms/utils/refSchema.js
@@ -0,0 +1,12 @@
+const validateTypes = require('./validateTypes');
+
+const REF_ROUTE = '#/components/schemas/';
+
+const refSchema = value => {
+  if (!value) return {};
+  const isPrimitive = validateTypes(value);
+  const schema = isPrimitive ? { type: value } : { $ref: `${REF_ROUTE}${value}` };
+  return schema;
+};
+
+module.exports = refSchema;
diff --git a/transforms/utils/validateTypes.js b/transforms/utils/validateTypes.js
index 79d042f..54b8c8e 100644
--- a/transforms/utils/validateTypes.js
+++ b/transforms/utils/validateTypes.js
@@ -3,6 +3,7 @@ const TYPES = [
   'number',
   'string',
   'boolean',
+  'object',
 ];
 
 const validateTypes = type => TYPES.includes(type);


Test patch:
diff --git a/test/transforms/paths/responses.test.js b/test/transforms/paths/responses.test.js
index 0925a13..4f93dcc 100644
--- a/test/transforms/paths/responses.test.js
+++ b/test/transforms/paths/responses.test.js
@@ -82,7 +82,7 @@ describe('response tests', () => {
       /**
        * GET /api/v1
        * @summary This is the summary or description of the endpoint
-       * @return {array<int>} 200 - success response - application/json
+       * @return {array<integer>} 200 - success response - application/json
        */
     `];
     const expected = {
@@ -100,7 +100,82 @@ describe('response tests', () => {
                     schema: {
                       type: 'array',
                       items: {
-                        type: 'int',
+                        type: 'integer',
+                      },
+                    },
+                  },
+                },
+              },
+            },
+          },
+        },
+      },
+    };
+    const parsedJSDocs = jsdocInfo()(jsodInput);
+    const result = setPaths({}, parsedJSDocs);
+    expect(result).toEqual(expected);
+  });
+
+  it('should parse jsdoc path response with components', () => {
+    const jsodInput = [`
+      /**
+       * GET /api/v1
+       * @summary This is the summary or description of the endpoint
+       * @return {Song} 200 - success response - application/json
+       */
+    `];
+    const expected = {
+      paths: {
+        '/api/v1': {
+          get: {
+            summary: 'This is the summary or description of the endpoint',
+            parameters: [],
+            tags: [],
+            responses: {
+              200: {
+                description: 'success response',
+                content: {
+                  'application/json': {
+                    schema: {
+                      $ref: '#/components/schemas/Song',
+                    },
+                  },
+                },
+              },
+            },
+          },
+        },
+      },
+    };
+    const parsedJSDocs = jsdocInfo()(jsodInput);
+    const result = setPaths({}, parsedJSDocs);
+    expect(result).toEqual(expected);
+  });
+
+  it('should parse jsdoc path response with array of components', () => {
+    const jsodInput = [`
+      /**
+       * GET /api/v1
+       * @summary This is the summary or description of the endpoint
+       * @return {array<Song>} 200 - success response - application/json
+       */
+    `];
+    const expected = {
+      paths: {
+        '/api/v1': {
+          get: {
+            summary: 'This is the summary or description of the endpoint',
+            parameters: [],
+            tags: [],
+            responses: {
+              200: {
+                description: 'success response',
+                content: {
+                  'application/json': {
+                    schema: {
+                      type: 'array',
+                      items: {
+                        $ref: '#/components/schemas/Song',
                       },
                     },
                   },