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

Bugfix: Include related virtualAttributes in toJSON() calls #1258

Merged
merged 2 commits into from
Mar 27, 2019

Conversation

venables
Copy link
Contributor

Bug: When calling toJSON on a model with related objects, the related objects do not include their virtualAttributes.

Cause: Due to the change introduced in 5530377 (to close #1252), calls to toJSON set an array of virtualAttributes for opt.virtuals. This array is then passed to child/related object toJSON calls. This causes the related models to believe they were called with toJSON({ virtuals: [<parent attributes>] }), thus excluding their own virtualAttributes.

For example, if a Post belongs to one User, and the User has a virtual fullName, calling post.toJSON() would not include user.fullName

I can confirm v1.6.3 works as expected, and v1.6.4 introduced the bug.

Solution:
This PR partially reverts 5530377 to allow for virtualAttributes to be calculated on a per-model basis by default.

Another solution would be to entirely revert 5530377

@coveralls
Copy link

Coverage Status

Coverage increased (+0.01%) to 97.082% when pulling 0e1e189 on venables:bug/related-virtual-attributes into 6e1d82e on Vincit:master.

@coveralls
Copy link

coveralls commented Mar 26, 2019

Coverage Status

Coverage increased (+0.02%) to 97.094% when pulling ddaeb41 on venables:bug/related-virtual-attributes into 6e1d82e on Vincit:master.

@venables venables force-pushed the bug/related-virtual-attributes branch from 0e1e189 to ddaeb41 Compare March 26, 2019 13:26
@koskimas
Copy link
Collaborator

Oh, crap! I thought there was a test for this case, but it uses the same model class for the related items. I'll merge this or some other fix as soon as I get to my computer.

@feross
Copy link

feross commented Mar 27, 2019

I believe I also experienced the breakage that this PR addresses.

@venables
Copy link
Contributor Author

@koskimas Considering this bug was introduced in a patch release, most apps will pick up this bug due to npm defaults.

What are your thoughts on reverting the original change to fix the apps in the wild, and re-relasing as a minor version bump?

@koskimas koskimas merged commit 285aaa4 into Vincit:master Mar 27, 2019
@koskimas
Copy link
Collaborator

Yeah, I'll release a new version right now. Why would I revert the original change instead of fixing this bug?

@koskimas
Copy link
Collaborator

By the way, thanks for fixing this @venables 🙇‍♂️

@koskimas
Copy link
Collaborator

objection 1.6.5 is out

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.

Pick return virtual attributes?
4 participants