Repository: nestjs/swagger
Pull request #1182 (2021-02-06 05:23:12)

Array format placement
## 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)
- [x] Docs have been added / updated (for bug fixes / features)

## PR Type

What kind of change does this PR introduce?

```
[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?

Issue Number: #1178

## What is the new behavior?

Fixes the bug.

## Does this PR introduce a breaking change?
```
[ ] Yes
[x] No
```

## Other information

The set of possible fields that should be moved from the outer type to the inner type in this case could be expanded. I picked a set of fields that match my own use cases, but these might not be exhaustive!


Patch:
diff --git a/lib/services/schema-object-factory.ts b/lib/services/schema-object-factory.ts
index 011c0a69..42ba2cee 100644
--- a/lib/services/schema-object-factory.ts
+++ b/lib/services/schema-object-factory.ts
@@ -8,7 +8,8 @@ import {
   keyBy,
   mapValues,
   omit,
-  omitBy
+  omitBy,
+  pick
 } from 'lodash';
 import { DECORATORS } from '../constants';
 import { getTypeIsArrayTuple } from '../decorators/helpers';
@@ -424,15 +425,20 @@ export class SchemaObjectFactory {
     type: string | Record<string, any>
   ): SchemaObjectMetadata {
     const keysToRemove = ['type', 'enum'];
+    const keysToMove = [
+      'format',
+      'maximum',
+      'maxLength',
+      'minimum',
+      'minLength',
+      'pattern'
+    ];
+    const moved = pick(metadata, keysToMove);
     const schemaHost = {
-      ...omit(metadata, keysToRemove),
+      ...omit(metadata, [...keysToRemove, ...keysToMove]),
       name: metadata.name || key,
       type: 'array',
-      items: isString(type)
-        ? {
-            type
-          }
-        : { ...type }
+      items: isString(type) ? { type, ...moved } : { ...type, ...moved }
     };
     schemaHost.items = omitBy(schemaHost.items, isUndefined);
     return schemaHost as unknown;


Test patch:
diff --git a/test/services/fixtures/create-user.dto.ts b/test/services/fixtures/create-user.dto.ts
index f48366ce..0771e43a 100644
--- a/test/services/fixtures/create-user.dto.ts
+++ b/test/services/fixtures/create-user.dto.ts
@@ -46,7 +46,8 @@ export class CreateUserDto {
 
   @ApiProperty({
     type: String,
-    isArray: true
+    isArray: true,
+    format: 'uri'
   })
   urls: string[];
 
diff --git a/test/services/schema-object-factory.spec.ts b/test/services/schema-object-factory.spec.ts
index d580fce1..d61d7435 100644
--- a/test/services/schema-object-factory.spec.ts
+++ b/test/services/schema-object-factory.spec.ts
@@ -148,6 +148,7 @@ describe('SchemaObjectFactory', () => {
           },
           urls: {
             items: {
+              format: 'uri',
               type: 'string'
             },
             type: 'array'