Repository: nestjs/swagger (ts)

Issue:
Array of enums not working as expected
<!--
PLEASE HELP US PROCESS GITHUB ISSUES FASTER BY PROVIDING THE FOLLOWING INFORMATION.

ISSUES MISSING IMPORTANT INFORMATION MAY BE CLOSED WITHOUT INVESTIGATION.
-->

## I'm submitting a...
<!-- 
Please search GitHub for a similar issue or PR before submitting.
Check one of the following options with "x" -->
<pre><code>
[ ] Regression <!--(a behavior that used to work and stopped working in a new release)-->
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
</code></pre>

## Current behavior
Adding an array of enums with the ApiProperty does not seem to work correctly.  For example, given the following enum:
```
enum MyEnum {
  One = "one",
  Two = "two"
}
```
I want to use this with a model property as follows:
`@ApiProperty({ enum: MyEnum, enumName: 'MyEnum',  isArray: true })`

This does not render correctly in the UI, and shows a type of `{}`

I've also tried both of these:
`@ApiProperty({ enum: MyEnum, enumName: 'MyEnum',  type: 'array' })`
and 
`@ApiProperty({ type: "array", enum: MyEnum, enumName: 'MyEnum', items: { $ref: getSchemaPath('MyEnum')} })` 

## Expected behavior
I'd expect the property to show up as an array with a reference to MyEnum.  

I'd expect the result of `@ApiProperty({ enum: MyEnum, enumName: 'MyEnum',  isArray: true })` to add `"type": "array"`.


## Minimal reproduction of the problem with instructions
Let me know if this is needed, I'd be happy to provide.

## What is the motivation / use case for changing the behavior?
N/A

## Environment

<pre><code>
Nest version: 7.1.3
Nest Swagger 4.5.10
<!-- Check whether this is still an issue in the most recent Nest version -->
 
For Tooling issues:
- Node version: 12.18
- Platform:  Mac
- NestJS: 4.5.10

Others:
N/A


Pull request:
fix(lib): fix enum array on model properties
fix #808

## PR Checklist
Please check if your PR fulfills the following requirements:

- [x] The commit message follows our guidelines: https://github.com/nestjs/nest/blob/master/CONTRIBUTING.md
- [x] Tests for the changes have been added (for bug fixes / features)
- [ ] Docs have been added / updated (for bug fixes / features)


## PR Type
What kind of change does this PR introduce?

<!-- Please check the one that applies to this PR using "x". -->
```
[x] Bugfix
[ ] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Other... Please describe:
```

## What is the current behavior?
<!-- Please describe the current behavior that you are modifying, or link to a relevant issue. -->

Issue Number: #808 


## What is the new behavior?
`@ApiProperty({ enum: EnumType, enumName: 'EnumType', isArray: true })` should produce the correct schema 
```
{
   "type": "array",
   "items": {
       "$ref$": "#/components/schemas/EnumType"
    }
}
```

## Does this PR introduce a breaking change?
```
[ ] Yes
[x] No
```

<!-- If this PR contains a breaking change, please describe the impact and migration path for existing applications below. -->


## Other information

Patch:
diff --git a/lib/services/schema-object-factory.ts b/lib/services/schema-object-factory.ts
index 6ea78e59..48e7c11d 100644
--- a/lib/services/schema-object-factory.ts
+++ b/lib/services/schema-object-factory.ts
@@ -317,8 +317,13 @@ export class SchemaObjectFactory {
 
     const refHost = metadata.isArray ? { items: { $ref } } : { $ref };
     const paramObject = { ..._schemaObject, ...refHost };
+    const pathsToOmit = ['enum', 'enumName'];
 
-    return omit(paramObject, ['enum', 'type']);
+    if (!metadata.isArray) {
+      pathsToOmit.push('type');
+    }
+
+    return omit(paramObject, pathsToOmit);
   }
 
   createNotBuiltInTypeReference(


Test patch:
diff --git a/test/services/schema-object-factory.spec.ts b/test/services/schema-object-factory.spec.ts
index 7b1d6551..e813b967 100644
--- a/test/services/schema-object-factory.spec.ts
+++ b/test/services/schema-object-factory.spec.ts
@@ -34,6 +34,8 @@ describe('SchemaObjectFactory', () => {
     class Person {
       @ApiProperty({ enum: Role, enumName: 'Role' })
       role: Role;
+      @ApiProperty({ enum: Role, enumName: 'Role', isArray: true })
+      roles: Role[];
     }
 
     it('should explore enum', () => {
@@ -53,9 +55,15 @@ describe('SchemaObjectFactory', () => {
         properties: {
           role: {
             $ref: '#/components/schemas/Role'
+          },
+          roles: {
+            type: 'array',
+            items: {
+              $ref: '#/components/schemas/Role'
+            }
           }
         },
-        required: ['role']
+        required: ['role', 'roles']
       });
 
       schemaObjectFactory.exploreModelSchema(CreatePersonDto, schemas, [