Repository: nestjs/swagger
Pull request #3153 (2024-11-07 07:35:39)

fix: omit schema field in response object 
## 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: #3152


## What is the new behavior?


## 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/response-object-mapper.ts b/lib/services/response-object-mapper.ts
index aec7da13..0bfa10bb 100644
--- a/lib/services/response-object-mapper.ts
+++ b/lib/services/response-object-mapper.ts
@@ -55,8 +55,10 @@ export class ResponseObjectMapper {
       schema: response.schema,
       ...pick(response, exampleKeys)
     });
+
+    const keysToOmit = [...exampleKeys, 'schema'];
     return {
-      ...omit(response, exampleKeys),
+      ...omit(response, keysToOmit),
       ...content
     };
   }
diff --git a/package-lock.json b/package-lock.json
index 9be7c0eb..7d895a18 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -14,7 +14,7 @@
         "js-yaml": "4.1.0",
         "lodash": "4.17.21",
         "path-to-regexp": "3.3.0",
-        "swagger-ui-dist": "5.18.1"
+        "swagger-ui-dist": "5.17.14"
       },
       "devDependencies": {
         "@commitlint/cli": "19.5.0",
@@ -2590,13 +2590,6 @@
       "dev": true,
       "license": "MIT"
     },
-    "node_modules/@scarf/scarf": {
-      "version": "1.3.0",
-      "resolved": "https://registry.npmjs.org/@scarf/scarf/-/scarf-1.3.0.tgz",
-      "integrity": "sha512-lHKK8M5CTcpFj2hZDB3wIjb0KAbEOgDmiJGDv1WBRfQgRm/a8/XMEkG/N1iM01xgbUDsPQwi42D+dFo1XPAKew==",
-      "hasInstallScript": true,
-      "license": "Apache-2.0"
-    },
     "node_modules/@sinclair/typebox": {
       "version": "0.27.8",
       "resolved": "https://registry.npmjs.org/@sinclair/typebox/-/typebox-0.27.8.tgz",
@@ -12732,13 +12725,10 @@
       }
     },
     "node_modules/swagger-ui-dist": {
-      "version": "5.18.1",
-      "resolved": "https://registry.npmjs.org/swagger-ui-dist/-/swagger-ui-dist-5.18.1.tgz",
-      "integrity": "sha512-nOUpYnU6HZ5Eq2+xmptFti4RX/Vcbe+6PDtQGnSiGT2j9ImgcrJsjsX3OEl8TZV+48y8Wfn0gcjxyoXsMFHr/w==",
-      "license": "Apache-2.0",
-      "dependencies": {
-        "@scarf/scarf": "=1.3.0"
-      }
+      "version": "5.17.14",
+      "resolved": "https://registry.npmjs.org/swagger-ui-dist/-/swagger-ui-dist-5.17.14.tgz",
+      "integrity": "sha512-CVbSfaLpstV65OnSjbXfVd6Sta3q3F7Cj/yYuvHMp1P90LztOLs6PfUnKEVAeiIVQt9u2SaPwv0LiH/OyMjHRw==",
+      "license": "Apache-2.0"
     },
     "node_modules/tar": {
       "version": "6.2.1",
diff --git a/package.json b/package.json
index 8bb1eca4..0fadce31 100644
--- a/package.json
+++ b/package.json
@@ -29,7 +29,7 @@
     "js-yaml": "4.1.0",
     "lodash": "4.17.21",
     "path-to-regexp": "3.3.0",
-    "swagger-ui-dist": "5.18.1"
+    "swagger-ui-dist": "5.17.14"
   },
   "devDependencies": {
     "@commitlint/cli": "19.5.0",


Test patch:
diff --git a/e2e/api-spec.json b/e2e/api-spec.json
index f1822ab0..6ac9b373 100644
--- a/e2e/api-spec.json
+++ b/e2e/api-spec.json
@@ -1271,6 +1271,82 @@
           "cats"
         ]
       }
+    },
+    "/api/cats/raw-schema-response": {
+      "get": {
+        "operationId": "CatsController_rawSchemaResponse",
+        "parameters": [
+          {
+            "name": "header",
+            "in": "header",
+            "description": "Test",
+            "required": false,
+            "schema": {
+              "type": "string",
+              "default": "test"
+            }
+          },
+          {
+            "name": "x-tenant-id",
+            "in": "header",
+            "schema": {
+              "type": "string"
+            }
+          }
+        ],
+        "responses": {
+          "200": {
+            "description": "The paginated response",
+            "content": {
+              "application/json": {
+                "schema": {
+                  "type": "object",
+                  "properties": {
+                    "data": {
+                      "type": "array",
+                      "items": {
+                        "$ref": "#/components/schemas/Cat"
+                      }
+                    },
+                    "pageInfo": {
+                      "type": "object",
+                      "properties": {
+                        "hasPreviousPage": {
+                          "type": "boolean"
+                        },
+                        "hasNextPage": {
+                          "type": "boolean"
+                        },
+                        "startCursor": {
+                          "type": "string"
+                        },
+                        "endCursor": {
+                          "type": "string"
+                        }
+                      }
+                    }
+                  }
+                }
+              }
+            }
+          }
+        },
+        "security": [
+          {
+            "key2": [],
+            "key1": []
+          },
+          {
+            "bearer": []
+          },
+          {
+            "basic": []
+          }
+        ],
+        "tags": [
+          "cats"
+        ]
+      }
     }
   },
   "info": {
diff --git a/e2e/src/cats/cats.controller.ts b/e2e/src/cats/cats.controller.ts
index 67397cc8..b52b8754 100644
--- a/e2e/src/cats/cats.controller.ts
+++ b/e2e/src/cats/cats.controller.ts
@@ -1,4 +1,12 @@
-import { Body, Controller, Get, Param, Post, Query } from '@nestjs/common';
+import {
+  Body,
+  Controller,
+  Get,
+  HttpStatus,
+  Param,
+  Post,
+  Query
+} from '@nestjs/common';
 import {
   ApiBearerAuth,
   ApiBody,
@@ -12,7 +20,8 @@ import {
   ApiQuery,
   ApiResponse,
   ApiSecurity,
-  ApiTags
+  ApiTags,
+  getSchemaPath
 } from '../../../lib';
 import { CatsService } from './cats.service';
 import { Cat } from './classes/cat.class';
@@ -196,4 +205,31 @@ export class CatsController {
     }
   })
   download() {}
+
+  @Get('raw-schema-response')
+  @ApiResponse({
+    status: HttpStatus.OK,
+    description: 'The paginated response',
+
+    schema: {
+      type: 'object',
+
+      properties: {
+        data: {
+          type: 'array',
+          items: { $ref: getSchemaPath(Cat) }
+        },
+        pageInfo: {
+          type: 'object',
+          properties: {
+            hasPreviousPage: { type: 'boolean' },
+            hasNextPage: { type: 'boolean' },
+            startCursor: { type: 'string' },
+            endCursor: { type: 'string' }
+          }
+        }
+      }
+    }
+  })
+  rawSchemaResponse() {}
 }