Repository: BRIKEV/express-jsdoc-swagger
Pull request #59 (2020-06-13 23:54:54)

Components array
### What kind of change does this PR introduce? (check at least one)
 - [x] Bugfix
 - [ ] Feature
 - [x] Test
 - [x] Docs
 - [ ] Refactor
 - [ ] Build-related changes
 - [ ] Other, please describe:

### Description:
This PR includes: 
- Fixed components schema when it has a property with an array of string/number
![image](https://user-images.githubusercontent.com/24435223/84581460-73301800-ade1-11ea-8db9-816ed77f3bab.png)




Patch:
diff --git a/examples/components/simple.js b/examples/components/simple.js
index 5f9d4fe..7f13549 100644
--- a/examples/components/simple.js
+++ b/examples/components/simple.js
@@ -42,4 +42,11 @@ expressJSDocSwagger(app)(options);
  * @property {Author} author
  */
 
+/**
+ * Album
+ * @typedef {object} Album
+ * @property {string} title - The title
+ * @property {array<number>} years
+ */
+
 app.listen(port, () => logger.info(`Example app listening at http://localhost:${port}`));
diff --git a/transforms/components/index.js b/transforms/components/index.js
index d2a2138..65cdca2 100644
--- a/transforms/components/index.js
+++ b/transforms/components/index.js
@@ -12,11 +12,19 @@ const getPropertyName = ({ name: propertyName }) => {
   return name;
 };
 
+const formatTypeApplication = (applications, expression) => ({
+  type: expression.name,
+  items: {
+    type: applications[0].name,
+  },
+});
+
 const formatProperties = properties => {
   if (!properties || !Array.isArray(properties)) return {};
   return properties.reduce((acum, property) => {
     const name = getPropertyName(property);
-    const type = property.type.name;
+    const { name: typeName, applications, expression } = property.type;
+    const type = typeName;
     const [descriptionValue, enumValues] = formatDescription(property.description);
     const [description, format] = mapDescription(descriptionValue);
     return {
@@ -25,6 +33,7 @@ const formatProperties = properties => {
         description,
         ...refSchema(type),
         ...combineSchema(property.type.elements),
+        ...(applications && expression ? formatTypeApplication(applications, expression) : {}),
         ...(format ? { format } : {}),
         ...addEnumValues(enumValues),
       },


Test patch:
diff --git a/test/transforms/components/index.test.js b/test/transforms/components/index.test.js
index 72e659c..b95e71f 100644
--- a/test/transforms/components/index.test.js
+++ b/test/transforms/components/index.test.js
@@ -498,4 +498,118 @@ describe('parseComponents method', () => {
     const result = parseComponents({}, parsedJSDocs);
     expect(result).toEqual(expected);
   });
+
+  it('Should parse jsdoc component spec when songs property is an array of strings', () => {
+    const jsodInput = [`
+      /**
+       * Album
+       * @typedef {object} Album
+       * @property {string} title - The title
+       * @property {array<string>} songs - songs array
+       */
+    `];
+    const expected = {
+      components: {
+        schemas: {
+          Album: {
+            type: 'object',
+            required: [],
+            description: 'Album',
+            properties: {
+              title: {
+                type: 'string',
+                description: 'The title',
+              },
+              songs: {
+                type: 'array',
+                description: 'songs array',
+                items: {
+                  type: 'string',
+                },
+              },
+            },
+          },
+        },
+      },
+    };
+    const parsedJSDocs = jsdocInfo()(jsodInput);
+    const result = parseComponents({}, parsedJSDocs);
+    expect(result).toEqual(expected);
+  });
+
+  it('Should parse jsdoc component spec when songs property is an array of numbers', () => {
+    const jsodInput = [`
+      /**
+       * Album
+       * @typedef {object} Album
+       * @property {string} title - The title
+       * @property {array<number>} years - years description
+       */
+    `];
+    const expected = {
+      components: {
+        schemas: {
+          Album: {
+            type: 'object',
+            required: [],
+            description: 'Album',
+            properties: {
+              title: {
+                type: 'string',
+                description: 'The title',
+              },
+              years: {
+                type: 'array',
+                description: 'years description',
+                items: {
+                  type: 'number',
+                },
+              },
+            },
+          },
+        },
+      },
+    };
+    const parsedJSDocs = jsdocInfo()(jsodInput);
+    const result = parseComponents({}, parsedJSDocs);
+    expect(result).toEqual(expected);
+  });
+
+  it('Should parse jsdoc component spec when songs property is an array of numbers with empty description', () => {
+    const jsodInput = [`
+      /**
+       * Album
+       * @typedef {object} Album
+       * @property {string} title - The title
+       * @property {array<number>} years
+       */
+    `];
+    const expected = {
+      components: {
+        schemas: {
+          Album: {
+            type: 'object',
+            required: [],
+            description: 'Album',
+            properties: {
+              title: {
+                type: 'string',
+                description: 'The title',
+              },
+              years: {
+                type: 'array',
+                description: '',
+                items: {
+                  type: 'number',
+                },
+              },
+            },
+          },
+        },
+      },
+    };
+    const parsedJSDocs = jsdocInfo()(jsodInput);
+    const result = parseComponents({}, parsedJSDocs);
+    expect(result).toEqual(expected);
+  });
 });