crud
crud copied to clipboard
CRUD service updateOne doesn't handle serialization groups
Hello, 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.
I'm having the same issue also with findOneBase
and findManyBase
.
For me it seems to not work, although in #591 there was a fix for this working twice.
So I'm lost a bit, there's a fix for this working twice, while we don't have it working at all (also i see few other issues there that mention decorators not being applied).
Have you solved this, or @eugenio165 can you confirm it works on your side?
I had to override the updateOne
method in my Service class in order to update the this.repo.save
line:
const updated = await this.repo.save(plainToClass(this.entityType, toSave, {
groups: [ADMIN]
}));
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.
Up.
I have the same problem for getManyBase. Please help with this.
Up.
it's causing because alwaysPaginate, if alwaysPaginate has been commented the serialization will work, any help?