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

Coercion scope bug #282

Closed
wants to merge 2 commits into from
Closed

Coercion scope bug #282

wants to merge 2 commits into from

Conversation

maxlinc
Copy link
Contributor

@maxlinc maxlinc commented Feb 25, 2015

Found a nasty bug.

The coercions are being stored on the superclass rather than the current class. This means that two classes that share the same superclass can't have conflicting coercions. This PR has I test, I don't have a fix yet.

I can't even mark the test as pending. Just defining OtherNestedCoercableHash breaks NestedCoercableHash even if it's never used. The problem is that it defines the coercion on RootCoercableHash so the definition from one class leaks into the scope of the other. Since the type was different that breaks other specs.

@maxlinc
Copy link
Contributor Author

maxlinc commented Feb 25, 2015

You can also see this by calling key_coercions:

instance[:nested].class
=> NestedCoercableHash
[5] pry(#<RSpec::ExampleGroups::HashieExtensionsCoercion::CoerceKey::Nesting>)> instance[:nested].class.key_coercions
=> {:foo=>Symbol, :bar=>Integer, :nested=>NestedCoercableHash, :other=>OtherNestedCoercableHash, :nested_list=>[NestedCoercableHash], :nested_hash=>{String=>NestedCoercableHash}}

It would make sense for coercions in BaseCoercableHash to show up in NestedCoercableHash since that's the superclass, but it's actually picking up coercions from NestedCoercableHash.

@dblock
Copy link
Member

dblock commented Feb 26, 2015

Yep :(

@maxlinc
Copy link
Contributor Author

maxlinc commented Mar 8, 2015

I think it's fixed. Double check it for me... I was expecting it to be something trickier.

In fact, I'm not sure this is 100% fixed. It perhaps it still has issues with deeply nested coercable structures. Is there a way to kill off the inherited method completely? It seems like a hack.

@michaelherold
Copy link
Member

There was a bug that was similar to this one, only in reverse, for indifferent_access (see #195). I think the takeaway is that book-keeping for this type of stuff is hard.

In this case, the #dup makes a lot of sense. We want to be able to add to the coercions in the new class, but still maintain the coercions in the parent class. Without the dup, if we add to the list of coercions, we add to both the parent and the child.

What feels like a hack about the inherited hook to you? It makes a lot of domain sense to me. Without changing the way we do book-keeping -- which I'm not sure how we could, given the modular nature -- I can't think of a better way to express this.

@martinstreicher
Copy link
Contributor

I found this bug too. My rspec test to show it is at martinstreicher@b0d2f96.

@martinstreicher
Copy link
Contributor

I submitted a PR for my spec. Perhaps it is another demonstration that the fix works in a form more likely to occur in an app.

@dblock
Copy link
Member

dblock commented Mar 31, 2015

I like #288 as well, I would take both tests, either needs a CHANGELOG, please.

@martinstreicher
Copy link
Contributor

Sure, I will add one.

@martinstreicher
Copy link
Contributor

CHANGELOG updated in my branch.

@dblock
Copy link
Member

dblock commented Mar 31, 2015

Merged spec via fd07b80.

@dblock dblock closed this Mar 31, 2015
@martinstreicher
Copy link
Contributor

It was part of #288 #288

On Mar 31, 2015, at 10:57 AM, Daniel Doubrovkine (dB.) @dblockdotorg notifications@github.com wrote:

Can I have a PR please?


Reply to this email directly or view it on GitHub #282 (comment).

jsonn pushed a commit to jsonn/pkgsrc that referenced this pull request Jun 9, 2015
## 3.4.2 (6/2/2015)

* [#292](hashie/hashie#292): Removed `Mash#id` and `Mash#type` - [@jrochkind](https://github.com/jrochkind).
* [#297](hashie/hashie#297): Extracted `Trash`'s behavior into a new `Dash::PropertyTranslation` extension - [@michaelherold](https://github.com/michaelherold).

## 3.4.1

* [#269](hashie/hashie#272): Added Hashie::Extensions::DeepLocate - [@msievers](https://github.com/msievers).
* [#270](hashie/hashie#277): Fixed ArgumentError raised when using IndifferentAccess and HashWithIndifferentAccess - [@gardenofwine](https://github.com/gardenofwine).
* [#281](hashie/hashie#281): Added #reverse_merge to Mash to override ActiveSupport's version - [@mgold](https://github.com/mgold).
* [#282](hashie/hashie#282): Fixed coercions in a subclass accumulating in the superclass - [@maxlinc](https://github.com/maxlinc), [@martinstreicher](https://github.com/martinstreicher).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants