From ab3225dfb019fcab6a24f6d9a194fe597b5ca8ad Mon Sep 17 00:00:00 2001 From: Tom Barton Date: Wed, 31 Mar 2021 12:18:36 +0100 Subject: [PATCH] fix(#1148): prevent decorator properties from leaking into swagger doc --- e2e/api-spec.json | 50 ++++++++++++++++++++++++++++ e2e/src/cats/cats.controller.ts | 14 ++++++-- lib/services/swagger-types-mapper.ts | 30 +++++++++-------- 3 files changed, 78 insertions(+), 16 deletions(-) diff --git a/e2e/api-spec.json b/e2e/api-spec.json index 476c0dcbf..734639f18 100644 --- a/e2e/api-spec.json +++ b/e2e/api-spec.json @@ -761,6 +761,56 @@ } ] } + }, + "/api/cats/with-enum/{type}": { + "get": { + "operationId": "CatsController_getWithEnumParam", + "parameters": [ + { + "name": "header", + "in": "header", + "description": "Test", + "required": false, + "schema": { + "default": "test", + "type": "string" + } + }, + { + "name": "type", + "required": true, + "in": "path", + "schema": { + "type": "string", + "enum": [ + "A", + "B", + "C" + ] + } + } + ], + "responses": { + "200": { + "description": "" + } + }, + "tags": [ + "cats" + ], + "security": [ + { + "key2": [], + "key1": [] + }, + { + "bearer": [] + }, + { + "basic": [] + } + ] + } } } } \ No newline at end of file diff --git a/e2e/src/cats/cats.controller.ts b/e2e/src/cats/cats.controller.ts index c60ddc601..9f7e8b79e 100644 --- a/e2e/src/cats/cats.controller.ts +++ b/e2e/src/cats/cats.controller.ts @@ -8,12 +8,13 @@ import { ApiQuery, ApiResponse, ApiSecurity, - ApiTags + ApiTags, + ApiParam } from '../../../lib'; import { CatsService } from './cats.service'; import { Cat } from './classes/cat.class'; import { CreateCatDto } from './dto/create-cat.dto'; -import { PaginationQuery } from './dto/pagination-query.dto'; +import { PaginationQuery, LettersEnum } from './dto/pagination-query.dto'; @ApiSecurity('basic') @ApiBearerAuth() @@ -87,4 +88,13 @@ export class CatsController { @Get('site*') getSite() {} + + + @Get("with-enum/:type") + @ApiParam({ + name: "type", + enum: LettersEnum, + enumName: "Letter" + }) + getWithEnumParam(@Param('type') type: LettersEnum) {} } diff --git a/lib/services/swagger-types-mapper.ts b/lib/services/swagger-types-mapper.ts index b8b42b1d3..e49545477 100644 --- a/lib/services/swagger-types-mapper.ts +++ b/lib/services/swagger-types-mapper.ts @@ -8,12 +8,22 @@ import { import { ParamWithTypeMetadata } from './parameter-metadata-accessor'; export class SwaggerTypesMapper { + private readonly keysToRemove: Array = [ + 'type', + 'isArray', + 'enum', + 'enumName', + 'items', + '$ref', + ...this.getSchemaOptionsKeys() + ] + mapParamTypes( parameters: Array ) { return parameters.map((param) => { if (this.hasSchemaDefinition(param as BaseParameterObject)) { - return this.omitParamType(param); + return this.omitParamKeys(param); } const { type } = param as ParamWithTypeMetadata; const typeName = @@ -29,27 +39,19 @@ export class SwaggerTypesMapper { isUndefined ); - const keysToRemove: Array = [ - 'type', - 'isArray', - 'enum', - 'items', - '$ref', - ...this.getSchemaOptionsKeys() - ]; if (this.isEnumArrayType(paramWithTypeMetadata)) { return this.mapEnumArrayType( paramWithTypeMetadata as ParamWithTypeMetadata, - keysToRemove + this.keysToRemove ); } else if (paramWithTypeMetadata.isArray) { return this.mapArrayType( paramWithTypeMetadata as ParamWithTypeMetadata, - keysToRemove + this.keysToRemove ); } return { - ...omit(param, keysToRemove), + ...omit(param, this.keysToRemove), schema: omitBy( { ...this.getSchemaOptions(param), @@ -131,8 +133,8 @@ export class SwaggerTypesMapper { return !!param.schema; } - private omitParamType(param: ParamWithTypeMetadata | BaseParameterObject) { - return omit(param, 'type'); + private omitParamKeys(param: ParamWithTypeMetadata | BaseParameterObject) { + return omit(param, this.keysToRemove); } private getSchemaOptionsKeys(): Array {