CRUD service updateOne doesn't handle serialization groups
See original GitHub issueHello, I am having problems trying to PATCH an entity. This entity has fields that are exposed only to some serialization groups.
The problem is that these fields cannot be updated, because this call seems to ignore serialization groups:
const updated = await this.repo.save(plainToClass(this.entityType, toSave));
https://github.com/nestjsx/crud/blob/master/packages/crud-typeorm/src/typeorm-crud.service.ts#L180
The field enabled
that I am trying to update is present in the toSave
var before the plainToClass
call.
This is an excerpt of my entity:
@Entity()
export class User extends BaseEntity {
@PrimaryGeneratedColumn()
id: number
@Column({ unique: true })
username: string
@Column({
default: true,
})
@Expose({ groups: [ADMIN] })
enabled: boolean
}
And the relevant part of my controller:
@Crud({
model: {
type: User,
},
validation: { transform: true },
query: {
alwaysPaginate: true,
limit: 20,
maxLimit: 100,
},
routes: {
updateOneBase: {
decorators: [SerializeOptions({ groups: [ADMIN] })],
},
},
})
@Controller('users')
@UseInterceptors(ClassSerializerInterceptor)
export class UsersController implements CrudController<User> {
What am I doing wrong? I need the enabled
field to be returned only on endpoints that have the ADMIN
serialization group, but the serialization call on updateOne
seems to ignore groups.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:6
Up.
I believe the problem lays in the interceptor implementation: https://github.com/nestjsx/crud/blob/5b02704ac0254fc362bcdff784387959211fc6c2/packages/crud/src/interceptors/crud-response.interceptor.ts#L45
As you may see the interceptor doesn’t apply any options to the
classToPlain
method. Unfortunately, I didn’t find a way to provide a custom global interceptor via DI. Hope it’s going to be fixed soon. The framework looks really good, but the issue is blocking me from introducing the framework to an existing project where serialization groups are crucial.