-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Bug: loadClass and virtual getters or setters (no args possible, getters and setters can not be overwritten at inheritance) #9975
Comments
GIT Patch:
|
Ok, I know the intention behind multiple getters and setters now to have inheritance on value level too. This means something like this does not work in typescript because parameters are not allowed in getters:
|
There are two additional ways to cover the getters problem in typescript: Option one: add a new clearGetters / clearSetters function virtualtype.js
schema.js
Pro: Keeps the logic as it is, daisy chaining is possible for short and clear logic. Option two: add a parameter to .get / .set to define overwriting
schema.js
Pro: Number of functions are the same, behavior is the same as before only with additional switch param Summary
Example for parent access:
|
We'll be applying virtual getters in forward order rather than reverse order in 6.0, see #8897. The issue here is that |
Do you want to request a feature or report a bug?
BUG
What is the current behavior?
loadClass will add virtuals in following order:
applyGetters and applySetters will execute the functions in reverse order.
Because of the order mismatch the wrong value will be used.
I am not sure which order is the intended. I have also not testet what happens if a virtual with the same name is registered twice without loadClass (but if I have a look at the code line, the same problem should be reproducible).
If the current behavior is a bug, please provide the steps to reproduce.
Example Schemas (written in NestJS):
Base User Model:
Extending TechnicalUser (shortened)
The loadClass method will detect the displayAs virtual getters in both models.
But if I use extends in TechnicalUser, the execution order is the following:
Codelines:
The getters in VirtualType has these order:
After fetching the user records by find without any condition, the getters will be applied in reversed order:
all functions will be executed and overwrites the previous return value.
https://github.com/Automattic/mongoose/blob/master/lib/virtualtype.js#L141-L153
What is the expected behavior?
In my opinion, there are two options:
For early returns:
Replace https://github.com/Automattic/mongoose/blob/master/lib/virtualtype.js#L148-L152
with
Or reverse the order (causes unnecessary calls):
For setters the reversed order does also makes no sense because the more specific return will also be overwritten.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
From package-lock.json:
The text was updated successfully, but these errors were encountered: