typestack / class-transformer

Decorator-based transformation, serialization, and deserialization between objects and classes.
MIT License
6.64k stars 486 forks source link

fix: bug in casting boolean properties from `@Query()` in Nest.js #1696

Closed sebasmrl closed 2 months ago

sebasmrl commented 2 months ago

Description

Exist a bug with boolean transformers, when you try casting boolean property from @Query() in Nest.js @IsBoolean() it does not work correctly, i tried doing with @Transfromer() and its execution callback repeat 2 times if you use others decorators, it's supposed to work using @IsBooolean() and @IsOptional() but is not.I was able to solve it doing a OR condition with @Tranformer()

Minimal code-snippet showcasing the problem

//Controller method
 @Get()
  findAll( @Query() paginationDto: PaginationDto) {
    return this.usersService.findAll(paginationDto);
  }

//Bug example code when you  trying to read 
export class PaginationDto{
    @IsOptional()
    @IsBoolean()
    activeRegisters?:boolean;
}

//Momentary solution
export class PaginationDto{
    @IsOptional()
    @IsBoolean()
    @Transform(({ value} ) => { 
        console.log("TRANSFORM: ",typeof value, value)
        return  (value == 'false' || value==false) ? false: true;
        } )
    activeRegisters?:boolean;
}

Expected behavior

it's supposed to work using @IsBooolean() and @IsOptional() but is not.

Actual behavior

In all other cases it works perfectly, only with @Query() results have problems

diffy0712 commented 2 months ago

Hello,

could you please describe your problem a bit more? I do not fully understand your problem. I do think you are mixing some things up here. The @IsBooolean() and @IsOptional() are decorators for class-validator and not this class-transformer library.

Neither class-transformer nor class-validator has @Query decorator, so I assume by your example that that is some framework's decorator (nest?).

Does the @Query decorator call the plainToInstance or other class-transformer functions?

diffy0712 commented 2 months ago

Hi @sebasmrl,

This may be a duplicate of https://github.com/typestack/class-transformer/issues/626 Could you please check it if you experience the same problem.

I assume when using '@query' the plain contains booleans as strings, is that right for your situation?

NoNameProvided commented 2 months ago

The issue does not contain a simple reproduction case.

Closing as invalid.

github-actions[bot] commented 1 month ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.