Skip to content

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

@ianoxley

Description

@ianoxley

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!?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions