-
Notifications
You must be signed in to change notification settings - Fork 402
feat: add a useResponseClassTransformer global option #329
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Conversation
src/driver/BaseDriver.ts
Outdated
@@ -129,14 +135,14 @@ export abstract class BaseDriver { | |||
|
|||
protected transformResult(result: any, action: ActionMetadata, options: Action): any { | |||
// check if we need to transform result | |||
const shouldTransform = (this.useClassTransformer && result != null) // transform only if enabled and value exist | |||
const shouldTransform = (this.useClassTransformer && this.useResponseClassTransformer && result != null) // transform only if enabled and value exist |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not updated comment
Isn't that the feature that @NoNameProvided promised to implement a few months ago? 😉 |
Yes, it was. Shame I haven't had the time. I haven't reviewed this either because I think we had agreed on a slightly different approach, but I am not sure, and I don't have time to read it back now. I have a lot of thing on my table now, so I struggle to find any time to spend on my hobbies (including this lib) right now. |
Hi, I updated the PR to better match with @NoNameProvided's solution outlined here, i.e. added What do you think? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please refactor your test entities to make sure that class-transformer option works - use @Exclude
and @Expose
decorator on class properties instead of toJSON
.
/** | ||
* Extra options that apply to each controller action. | ||
*/ | ||
export interface ControllerOptions extends TransformationOptions { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that you should implement TransformationOptions
, not extending it - it would be ready for future extension/changes because you can't extend multiple interfaces.
assertRequest([3001, 3002], "post", "default", { firstName: "Umed", lastName: "Khudoiberdiev" }, response => { | ||
expect(initializedUser).to.be.instanceOf(User); | ||
expect(response).to.have.status(200); | ||
expect(response.body.lastName).to.be.defined; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You've set transformResponse: false
but you expect lastName
to be defined even if lastName is excluded when class-transformer is disabled
... I don't get it 😕
} | ||
|
||
@JsonController("/transform", {transformRequest: true, transformResponse: true}) | ||
class NoResponseTransformController { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NoResponseTransformController
? No? 😆
assertRequest([3001, 3002], "post", "default", { firstName: "Umed", lastName: "Khudoiberdiev" }, response => { | ||
expect(initializedUser).to.be.instanceOf(User); | ||
expect(response).to.have.status(200); | ||
expect(response.body.lastName).to.be.defined; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- default:
...lastName).to.be.defined;
- enable:
...lastName).not.to.be.undefined;
Please be consistent with assertion, don't negate the negative expectation 😉
assertRequest([3001, 3002], "post", "override", { firstName: "Umed", lastName: "Khudoiberdiev" }, response => { | ||
expect(initializedUser).not.to.be.instanceOf(User); | ||
expect(response).to.have.status(200); | ||
expect(response.body.lastName).to.be.defined; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't you use .exist
here? I can't see defined
in chai documentation 😕
Thanks for the feedback @19majkel94, lots of good points in there 👍 . I refactored the tests, let me know if there's anything else you'd like to see changed. I did have some trouble with class-transformer's global metadata storage: calling |
Yeah, it clears because you call |
Because the way it was set up, after a test module finished it would not only clear the metadata of its own test classes, but also of all the classes declared in the following tests. For example after |
So the problem was that pleerock uses the class-transformer only in one test so clearing metadata wasn't affect other tests but now you have tests also in other files so it clears the metadata of all tests, right? 😄 |
Exactly 👍 . I took a peek at class-transformer tests, seems like they're handling things in a similar way, declaring classes in the upper |
Ok, you have |
Hey, sorry for the delay, got kinda busy over the holidays! I added the test case and removed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
I'm not sure if @pleerock will like the options object inside decorator so I will wait for his review before merging this PR 😉 |
Hello, i'm jumping here to understand if this was holded back due to an alternative way to use both JSON controllers and "Raw" controllers that returns buffers |
I'm also curious about what's holding this back. Care to comment @NoNameProvided ? |
@pleerock, @NoNameProvided could you please have a look at this? This is still an existing issue :( |
Is there anything happening regarding this? |
Is there still something holding this back @NoNameProvided @pleerock ? |
Stale pull request message |
What's the update on this ? 😅 |
Also curious about the status of this? |
Still waiting :) |
Hi, first of all thanks for a great library!
By default response values are coerced to plain objects with class-transformer. When returning large objects (#226) or values with complex serialization logic (e.g. Mongoose documents #149) you might opt for the default
toJSON
handler instead.This PR adds a global
useResponseClassTransformer
option parameter for toggling response transformation. It defaults totrue
, and is overridden byclassTransformer: false
.Also related to #179.