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

ClassSerializerInterceptor not triggered on paginated data #259

Closed
wafs opened this issue Sep 13, 2019 · 2 comments
Closed

ClassSerializerInterceptor not triggered on paginated data #259

wafs opened this issue Sep 13, 2019 · 2 comments

Comments

@wafs
Copy link

wafs commented Sep 13, 2019

Hello, there is a bug involved when using the class-transformer package which is mentioned in the documentation, the official nestJS ClassSerializerInterceptor and this library.

The interceptor works on a single entity, on an array of entities, but not from a default paginated object - which leads me to believe this is not intended behaviour.

In the source code for the interceptor, you'll see it does a check to see if the constructor of the item being passed in is an Object and if not, it will serialize : https://github.com/nestjs/nest/blob/b4baa995e43c77ef169585943bab0c21f31af418/packages/common/serializer/class-serializer.interceptor.ts#L66

In the source code for this codebase to create a page, a plain object is sent back, and since the entities are nested inside the data param, they are never transformed because their parent object does not pass the classToPlain check:

Changing the page method to return some class instance which implements the GetManyDefaultResponse interface should be enough to fix it without breaking changes, otherwise a nest interceptor that uses a typescript runtime guard to check if the response object follows the interface could be used but it would require users to wire it up.

@agarbund
Copy link

agarbund commented Nov 7, 2019

Same issue exists for updateOne method. I was able to bypass it by using query.allow to include only needed fields, but it's indeed a weird behavior that ClassSerializerInterceptor works for getOne or getMany, and doesn't work for updateOne and other methods.

Would be extremely useful to have a list of entity public fields in one place, not two.

PS. This package is great anyway, thanks for creating it :)

@michaelyali
Copy link
Member

michaelyali commented Dec 18, 2019

I've added CrudSerializeInterceptor (applies automatically) along with the new CrudOption serialize where you can provide separate response dtos for each route (action), and if there is no dto provided for a particular action the model type will be used.
Also, all action methods in TypeormCrudService return an entity instance, no plain objects. So, serialization now works. Please try 4.3.0-beta versions. The documentation will be added soon

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

3 participants