crud icon indicating copy to clipboard operation
crud copied to clipboard

CRUD service updateOne doesn't handle serialization groups

Open joelclouddistrict opened this issue 4 years ago • 7 comments

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.

joelclouddistrict avatar Jul 01 '20 07:07 joelclouddistrict

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?

atomicus avatar Nov 13 '20 19:11 atomicus

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]
    }));

joelclouddistrict avatar Nov 16 '20 07:11 joelclouddistrict

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.

malcheg avatar Nov 28 '20 13:11 malcheg

Up.

herenickname avatar Dec 09 '20 23:12 herenickname

I have the same problem for getManyBase. Please help with this.

bilalceyylan avatar Mar 23 '21 08:03 bilalceyylan

Up.

nevedimko avatar May 24 '22 11:05 nevedimko

it's causing because alwaysPaginate, if alwaysPaginate has been commented the serialization will work, any help?

abdokouta avatar Sep 23 '23 00:09 abdokouta