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 Settable: allow setting a nested value to a non-string (for 6.4 stable) #4510
Fix Settable: allow setting a nested value to a non-string (for 6.4 stable) #4510
Conversation
|
||
if fields[field] && fields[field].type == Hash && attributes.key?(field) && !value.empty? | ||
if fields[field] && fields[field].type == Hash && attributes.key?(field) && Hash === value && !value.empty? |
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.
Do you think respond_to?(:empty?)
would make more sense here?
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.
No, I think we specifically want to check for a Hash
... for example we don't want to enter this block for a String
, even though String
implements empty?
.
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.
OK that makes sense. Is this behavior documented somewhere?
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 sure. The behavior in this PR seems to make sense: only merge an existing Hash with a new value if that value is a non-empty Hash. The PR that introduced the regression intended to support setting field values to an empty Hash, which remains supported.
Hi may I know when is this PR going to be merged? This issue is quite serious though. Many thanks! |
@gracezhangyaofei I need to dig into the code that this PR changes as the change itself is non-obvious. Additionally neither https://docs.mongodb.com/mongoid/master/tutorials/mongoid-persistence/ nor https://docs.mongodb.com/mongoid/master/tutorials/mongoid-nested-attributes/ seem to mention anything that looks like |
Hi @p-mongo thanks for your fast reply. Didn't dig into these tutorials. But it is a real problem for me right now. And it was caused by |
Documentation about how The new spec should indicate what this PR is attempting to fix. AFAIK there's no existing behavior change here. |
end | ||
|
||
before do | ||
church.set('location.address.city' => 12345) |
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.
If I'm understanding correctly, this is where the exception would have occurred before the change? If so, let's put this inside a let
block instead and invoke inside the it
block below so that any future regression occurs in a place that's more easily identified.
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.
Can do, but this way it's more consistent with all the examples around it. I think an exception raised anywhere in the spec is fine for failing a test; it doesn't necessarily need to be raised in the it
block. However, happy to change it if this blocks merging.
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.
Fair enough; I don't feel strongly enough to block merging on this, so it's fine to leave as-is.
@p-mongo From what I can tell, this change seems to be correct, and I think we should update our documentation to describe this behavior. That being said, I think the documentation you linked to describes a separate feature from this, namely updating both sides of a relation at the same time (which unfortunately is not at all what the name seems to imply). I'm thinking that the best place for the new documentation would be in the "Fields" section. Thoughts? |
I think the docs should be under persistence/#set, I'm going through the relevant bits in the tutorial to expand them. |
https://jira.mongodb.org/browse/MONGOID-4579 for that, if you want to merge this PR as it is that's ok with me. |
@mdehoog Can you provide a reduced failing example that this PR is fixing? |
Or @gracezhangyaofei if you have an example because so far my examples are working. |
Docs are in #4514. |
@p-mongo The specs I added fail without the change:
|
I see, the bug affects 6.x only. This problem does not exist on master. Have you investigated what makes master work in this case? |
#4487 wasn't merged into master, so master might actually suffer from https://jira.mongodb.org/browse/MONGOID-4525. I haven't validated this. |
OK, then we need to figure out if 4525 affects master, ensure master has an equivalent test to the one added in this PR, then see if the fix can be backported from master or potentially master needs some version of #4487 and/or this PR. |
After #4515 lands it needs to be cherry picked back into this PR. |
Closed in favor of #4518 |
We don't currently have any bugfix release scheduled for the 6.4.x branch, although we don't have any opposition to doing one. Is this something you need, or would using the 7.0.x branch work for you? |
Sounds good 👍. We're not in a position to move to 7.0.x yet, but we'll put it on the roadmap. |
After some discussion, we've decided we do in fact want to do a patch release for 6.4, and this seems like a good candidate to be part of that. Unfortunately, it looks like I can't reopen this since the branch is closed. Any chance it's possible to get it pushed back up? (If not, that's totally fine! I can just manually apply the patch, which Github does seem to have cached). |
@saghm I restored the branch, but can't reopen the PR; do you have access to do this? |
Let's cherry-pick this diff on 7.0.0-stable as well. |
Hi @p-mongo , may I know when this fix can be merged? I saw your last msg was two weeks ago. Keep me posted, many thanks! |
@gracezhangyaofei We are configuring CI internally to get this PR appropriately tested before merging it. |
Hi @mdehoog! Sorry for the delay in merging this; we had to get our CI set up for the 6.4.0-stable branch, but it's all ready go to now! Is it all right with you if I push a rebase up to your branch with a few backports we made to the 6.4.0 branch to get the CI passing? (Github suggests that I can "add more commits by pushing" to your branch, but I'm not sure about a force push after a rebase, and I don't want to force push to your repo without getting permission first anyhow) |
@saghm completely fine, go for it 👍 |
Awesome, thanks! |
051ff8b
to
78607ba
Compare
Just merged this. Thanks again for the contribution! |
#4487 introduced a regression: one can no longer
$set
a nested hash value to anything that doesn't implementempty?
.This PR fixes a couple of bugs with the previous PR:
value
fromfield_and_value_hash
before checking if it'sempty?
; this ensures we're checking the actual value that is attempting to be setHash
before checking if it'sempty?
The reason previous tests passed is because they only checked that nested values could be set to a string, which implements
empty?
.Relevant stacktrace here: