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

fix(serialization): run custom serializer on getters #4860

Merged
merged 5 commits into from
Oct 23, 2023

Conversation

ivanpondal
Copy link
Contributor

Fixes #4859

@codecov
Copy link

codecov bot commented Oct 20, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Files Coverage Δ
...ackages/core/src/serialization/EntitySerializer.ts 100.00% <100.00%> (ø)
...ckages/core/src/serialization/EntityTransformer.ts 100.00% <100.00%> (ø)

... and 6 files with indirect coverage changes

📢 Thoughts on this report? Let us know!.

@@ -97,7 +97,7 @@ export class EntitySerializer {
// decorated getters
meta.props
.filter(prop => prop.getter && typeof entity[prop.name] !== 'undefined' && isVisible(meta, prop.name, options))
.forEach(prop => ret[this.propertyName(meta, prop.name, wrapped.__platform)] = entity[prop.name]);
.forEach(prop => ret[this.propertyName(meta, prop.name, wrapped.__platform)] = this.processProperty(prop.name, entity, options));

// decorated get methods
meta.props
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should do the same here as well, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, had to do some small changes. First off, added a getterName === undefined check to the accessor serializer. This was to avoid it being called twice (for method getters too). Then I modified the processProperty function so that if the property is a function (the method getter case) then it gets the corresponding value by calling the function. Let me know if I'm missing something or if you can think of a more elegant way of doing it 👍

Copy link
Member

@B4nan B4nan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

two more nits

tests/features/serialization/GH4859.test.ts Outdated Show resolved Hide resolved
tests/features/serialization/GH4859.test.ts Outdated Show resolved Hide resolved
@@ -97,7 +97,7 @@ export class EntitySerializer {
// decorated getters
meta.props
.filter(prop => prop.getter && typeof entity[prop.name] !== 'undefined' && isVisible(meta, prop.name, options))
.forEach(prop => ret[this.propertyName(meta, prop.name, wrapped.__platform)] = entity[prop.name]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, this also needs to be addressed in the EntityTransformer class, which is used for implicit serialization. which reminds me i wanted to look into deduplicating this in v6, now the two classes are almost the same

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you point me to some examples where EntityTransformer is being used as to test it accordingly? Thanks!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrap(entity).toObject() is using it, same for JSON.stringify(entity)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added test cases for wrap(entity).toObject() 👍

@@ -129,6 +129,9 @@ export class EntitySerializer {

/* istanbul ignore next */
if (!options.ignoreSerializers && serializer) {
if (entity[prop] as unknown instanceof Function) {
return serializer((entity[prop] as unknown as () => T[keyof T & string])());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same cast that was being used to obtain the getter method value.

@ivanpondal ivanpondal requested a review from B4nan October 22, 2023 22:45
@ivanpondal
Copy link
Contributor Author

Will squash commits once the pr is ready to conform with commit format.

@ivanpondal
Copy link
Contributor Author

I'm looking into the test failure. Things broke with the EntityTransformer changes.

Copy link
Member

@B4nan B4nan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@B4nan B4nan merged commit e76836e into mikro-orm:master Oct 23, 2023
11 of 13 checks passed
@ivanpondal ivanpondal deleted the gh-4859 branch October 23, 2023 19:58
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

Successfully merging this pull request may close these issues.

Custom serializer does not take into account getters
2 participants