Skip to content

Commit

Permalink
fix(#1148): prevent decorator properties from leaking into swagger doc
Browse files Browse the repository at this point in the history
  • Loading branch information
tombarton committed Mar 31, 2021
1 parent 3942a15 commit ab3225d
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 16 deletions.
50 changes: 50 additions & 0 deletions e2e/api-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
}
]
}
}
}
}
14 changes: 12 additions & 2 deletions e2e/src/cats/cats.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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) {}
}
30 changes: 16 additions & 14 deletions lib/services/swagger-types-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,22 @@ import {
import { ParamWithTypeMetadata } from './parameter-metadata-accessor';

export class SwaggerTypesMapper {
private readonly keysToRemove: Array<keyof ApiPropertyOptions | '$ref'> = [
'type',
'isArray',
'enum',
'enumName',
'items',
'$ref',
...this.getSchemaOptionsKeys()
]

mapParamTypes(
parameters: Array<ParamWithTypeMetadata | BaseParameterObject>
) {
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 =
Expand All @@ -29,27 +39,19 @@ export class SwaggerTypesMapper {
isUndefined
);

const keysToRemove: Array<keyof ApiPropertyOptions | '$ref'> = [
'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),
Expand Down Expand Up @@ -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<keyof SchemaObject> {
Expand Down

0 comments on commit ab3225d

Please sign in to comment.