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

ClassKit::Helper#from_hash can cause side effects as it modifies the hash parameter #11

Open
ianoxley opened this issue Nov 1, 2017 · 0 comments

Comments

@ianoxley
Copy link

ianoxley commented Nov 1, 2017

We came across an issue where we created a ClassKit instance from a hash, then used that same hash elsewhere to write data to a Redis cache. This was in turn calling Marshal.dump(hash) but that failed with a TypeError: can't dump hash with default proc.

Here's a test case to illustrate this:

class Foo
  extend ClassKit

  attr_accessor_type :bar
end

data = { bar: 'bar' }
data.default_proc
# => nil
Marshal.dump(data)
# => "\x04\b{\x06:\bbarI\"\bbar\x06:\x06ET"

helper = ClassKit::Helper.new
foo = helper.from_hash(hash: data, klass: Foo)
foo.bar
# => 'bar'
data.default_proc
# => #<Proc:0x007fb811104368@/path/to/gems/hash_kit-0.5.4/lib/hash_kit/helper.rb:11>
Marshal.dump(data)
# => TypeError: can't dump hash with default proc

This seems to be caused by the from_hash method calling HashKit::indifferent! on the hash parameter: https://github.com/Sage/class_kit/blob/master/lib/class_kit/helper.rb#L54

Would a possible fix be to create a copy of the hash before calling Hashkit::indifferent!?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant