From d35c1c54e6c11e2cef70bc604beb67eebe3cc01d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20My=C5=9Bliwiec?= Date: Thu, 7 Nov 2024 08:35:03 +0100 Subject: [PATCH] fix: omit schema field in response object #3152 --- e2e/api-spec.json | 76 ++++++++++++++++++++++++++ e2e/src/cats/cats.controller.ts | 40 +++++++++++++- lib/services/response-object-mapper.ts | 4 +- 3 files changed, 117 insertions(+), 3 deletions(-) diff --git a/e2e/api-spec.json b/e2e/api-spec.json index f1822ab05..6ac9b3738 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 67397cc8b..b52b8754f 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() {} } diff --git a/lib/services/response-object-mapper.ts b/lib/services/response-object-mapper.ts index aec7da138..0bfa10bb3 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 }; }