Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Using OmitType on Query Dto which has array parameter with ApiProperty decorator causes error #1034

Closed
krisnadiputra opened this issue Nov 9, 2020 · 1 comment

Comments

@krisnadiputra
Copy link

I'm submitting a...


[ ] Regression 
[ x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Using OmitType on QueryDto which has array parameter with ApiProperty decorator in the parent class causes error on Swagger UI. The error is below:
Uncaught (in promise) TypeError: Cannot read property 'anyOf' of undefined
Screen Shot 2020-11-09 at 18 35 12

However the error disappears when I remove ApiProperty decorator on characteristics property. Also it works fine if I change CreateCatDto to @Body Dto in the controller.

create-cat.dto.ts

import { IsInt, IsString, IsOptional, ArrayUnique } from 'class-validator';
import { Transform } from 'class-transformer';
import { OmitType, ApiProperty } from '@nestjs/swagger';

class CreateAnimalDto {
  @ApiProperty({ description: 'comma-separated values' })
  @IsOptional()
  @ArrayUnique()
  @Transform((v: any) => v?.split(',').map((v: string) => v.trim()))
  characteristics?: string[];

  @IsOptional()
  @IsString()
  propertyA?: string;
}

export class CreateCatDto extends OmitType(CreateAnimalDto, ['propertyA'] as const) {
  @IsString()
  readonly name: string;

  @IsInt()
  readonly age: number;

  @IsString()
  readonly breed: string;
}

cats.controller.ts

...
@Post()
  @ApiOperation({ summary: 'Create cat' })
  @ApiResponse({ status: 403, description: 'Forbidden.' })
  async create(@Query() createCatDto: CreateCatDto): Promise<Cat> {
    return this.catsService.create(createCatDto);
  }
...

Expected behavior

Swagger UI should work just fine regardless the existence of ApiProperty decorator.

Minimal reproduction of the problem with instructions

Minimal reproduction repo

What is the motivation / use case for changing the behavior?

My motivation to add ApiProperty decorator there is to show the collectionFormat of csv. Is there any better way to do that regardless this bug?

Environment


"@nestjs/common": "7.4.4",
"@nestjs/core": "7.4.4",
"@nestjs/platform-express": "7.4.4",
"@nestjs/swagger": "4.6.1",
"class-transformer": "0.3.1",
"class-validator": "0.12.2",
"swagger-ui-express": "4.1.4"

 
For Tooling issues:
- Node version: v12.18.3
- Platform:  Mac

Others:

@kamilmysliwiec
Copy link
Member

Fixed in 4.7.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants