Skip to content

Commit

Permalink
Merge pull request #3153 from nestjs/fix/omit-schema-for-response
Browse files Browse the repository at this point in the history
fix: omit schema field in response object
  • Loading branch information
kamilmysliwiec authored Nov 7, 2024
2 parents a550529 + d35c1c5 commit 647c0a6
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 3 deletions.
76 changes: 76 additions & 0 deletions e2e/api-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
40 changes: 38 additions & 2 deletions e2e/src/cats/cats.controller.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -12,7 +20,8 @@ import {
ApiQuery,
ApiResponse,
ApiSecurity,
ApiTags
ApiTags,
getSchemaPath
} from '../../../lib';
import { CatsService } from './cats.service';
import { Cat } from './classes/cat.class';
Expand Down Expand Up @@ -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() {}
}
4 changes: 3 additions & 1 deletion lib/services/response-object-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
}
Expand Down

0 comments on commit 647c0a6

Please sign in to comment.