Repository: nestjs/swagger
Pull request #3129 (2024-10-28 22:07:45)

feat: Add schema support for more option keys
## 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". -->
- [ ] Bugfix
- [x] 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: https://github.com/nestjs/swagger/issues/3078


## What is the new behavior?
These fields will be supported properly for parameters, referencing the oneOf/anyOf constraints inside the schema instead of directly in the type for parameters.

## 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/swagger-types-mapper.ts b/lib/services/swagger-types-mapper.ts
index e3a4bc06..be2249c6 100644
--- a/lib/services/swagger-types-mapper.ts
+++ b/lib/services/swagger-types-mapper.ts
@@ -174,7 +174,9 @@ export class SwaggerTypesMapper {
       'pattern',
       'nullable',
       'default',
-      'example'
+      'example',
+      'oneOf',
+      'anyOf'
     ];
   }
 }


Test patch:
diff --git a/e2e/api-spec.json b/e2e/api-spec.json
index 90c976f3..9394bc32 100644
--- a/e2e/api-spec.json
+++ b/e2e/api-spec.json
@@ -386,6 +386,26 @@
               "type": "number"
             }
           },
+          {
+            "name": "constrainedLimit",
+            "required": true,
+            "in": "query",
+            "schema": {
+              "oneOf": [
+                {
+                  "minimum": 0,
+                  "maximum": 10,
+                  "format": "int32"
+                },
+                {
+                  "minimum": 100,
+                  "maximum": 100,
+                  "format": "int32"
+                }
+              ],
+              "type": "number"
+            }
+          },
           {
             "name": "enum",
             "required": true,
@@ -1535,6 +1555,20 @@
           "limit": {
             "type": "number"
           },
+          "constrainedLimit": {
+            "oneOf": [
+              {
+                "minimum": 0,
+                "maximum": 10,
+                "format": "int32"
+              },
+              {
+                "minimum": 100,
+                "maximum": 100,
+                "format": "int32"
+              }
+            ]
+          },
           "enum": {
             "allOf": [
               {
@@ -1575,6 +1609,7 @@
           "page",
           "_sortBy",
           "limit",
+          "constrainedLimit",
           "enum",
           "enumArr",
           "letters",
diff --git a/e2e/src/cats/dto/pagination-query.dto.ts b/e2e/src/cats/dto/pagination-query.dto.ts
index 91e3857f..19e9e1c7 100644
--- a/e2e/src/cats/dto/pagination-query.dto.ts
+++ b/e2e/src/cats/dto/pagination-query.dto.ts
@@ -29,6 +29,22 @@ export class PaginationQuery {
   @ApiProperty()
   limit: number;
 
+  @ApiProperty({
+    oneOf: [
+      {
+        minimum: 0,
+        maximum: 10,
+        format: 'int32'
+      },
+      {
+        minimum: 100,
+        maximum: 100,
+        format: 'int32'
+      }
+    ],
+  })
+  constrainedLimit?: number;
+
   @ApiProperty({
     enum: LettersEnum,
     enumName: 'LettersEnum'
@@ -45,7 +61,7 @@ export class PaginationQuery {
   @ApiProperty({
     enum: LettersEnum,
     enumName: 'Letter',
-    isArray: true,
+    isArray: true
   })
   letters: LettersEnum[];
 
@@ -59,7 +75,7 @@ export class PaginationQuery {
         type: 'string'
       },
       age: {
-        type: 'number',
+        type: 'number'
       }
     },
     additionalProperties: true
diff --git a/test/plugin/fixtures/project/cats/dto/pagination-query.dto.ts b/test/plugin/fixtures/project/cats/dto/pagination-query.dto.ts
index a6a179ee..117fd1a1 100644
--- a/test/plugin/fixtures/project/cats/dto/pagination-query.dto.ts
+++ b/test/plugin/fixtures/project/cats/dto/pagination-query.dto.ts
@@ -29,6 +29,22 @@ export class PaginationQuery {
   @ApiProperty()
   limit: number;
 
+  @ApiProperty({
+    oneOf: [
+      {
+        minimum: 0,
+        maximum: 10,
+        format: 'int32'
+      },
+      {
+        minimum: 100,
+        maximum: 100,
+        format: 'int32'
+      }
+    ],
+  })
+  constrainedLimit?: number;
+
   @ApiProperty({
     enum: LettersEnum,
     enumName: 'LettersEnum'
diff --git a/test/plugin/fixtures/serialized-meta.fixture.ts b/test/plugin/fixtures/serialized-meta.fixture.ts
index daa5aa70..d8d9eecf 100644
--- a/test/plugin/fixtures/serialized-meta.fixture.ts
+++ b/test/plugin/fixtures/serialized-meta.fixture.ts
@@ -18,6 +18,7 @@ export default async () => {
               page: { required: true, type: () => Number },
               sortBy: { required: true, type: () => [String] },
               limit: { required: true, type: () => Number },
+              constrainedLimit: { required: false, type: () => Number },
               enum: {
                 required: true,
                 enum: t['./cats/dto/pagination-query.dto'].LettersEnum