-
-
Notifications
You must be signed in to change notification settings - Fork 544
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(core): allow assigning null to embeddable property #1356
Conversation
When you have entity like this: ``` @entity() class Document { ... @Embedded({ entity: () => Summary, nullable: true, }) summary!: Summary; ... } ``` and do this: ``` document.assign({summary: null}, { mergeObjects: true }); ``` it explode with: ``` Cannot convert undefined or null to object at Function.keys (<anonymous>) at Function.assignEmbeddable (***/node_modules/@mikro-orm/core/entity/EntityAssigner.js:108:16) ``` thank you very much for your great work!
Thanks for the fix! Please add a testcase too, otherwise we might fall into same issue during future refactorings easily. |
}); | ||
if (prop.nullable && value === null) { | ||
entity[propName] = null; | ||
} else if (typeof value === 'object') { |
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'd rather keep this as simple else
branch (as noted above, if we return early, there is no need for new branching at all), or add a validation if the value is not object. doing it like this, it would silently ignore badly formed data - we should rather fail in such case.
also this will definitely need some test cases to keep the 100% branch coverage
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.
@B4nan thank you very much for your quick response!
I'll tell you the reasons that made me do it this way:
- In the first if I understand that null allows to delete from the database, so I assign null but only if the property is nullable, to prevent deleting something if it is not configured for it.
- In the second if, the object type check allows to discard two cases, on the one hand null which is not nullable and on the other hand undefined, which is an ignored value and respects the previous value.
These are the reasons, do you think it is preferable to make the changes you are talking about?
Thanks a lot!
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.
As I said, we should not ignore malformed input data, we should throw instead. e.g. we could throw when user tries to set null/undefined and the property is not nullable, but ignoring the request to set a property to null, just because it is not nullable, that sounds wrong to me.
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 are absolutely right! I have updated it
|
||
entity[propName][key] = value[key]; | ||
}); | ||
if (prop.nullable && value === null) { |
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'd rather not check theh prop.nullable
here, also we should think about undefined
too (it is possible to force usage of undefined
instead of null
globally)
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.
done ;)
Some tests are failing
|
When you have entity like this:
and do this:
it explode with:
thank you very much for your great work!