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

Problem flattening multiple nested properties via Trash #514

Open
dub-squared opened this issue Jan 24, 2020 · 3 comments
Open

Problem flattening multiple nested properties via Trash #514

dub-squared opened this issue Jan 24, 2020 · 3 comments
Labels
bug? hacktoberfest Issues that would be suitable for Hacktoberfest

Comments

@dub-squared
Copy link

dub-squared commented Jan 24, 2020

I've come across a possible issue when trying to flatten multiple properties from a nested hash. It works file when I'm only flattening one property, but when a second one is added both no longer work. I've also been able to narrow it down to string keys. If I symbolize the keys, the flattening works as expected. (See last example).

I can work around it by just calling deep_symbolize_keys prior to passing it to the Trash, but that leaves a bit of a code smell. Any input is appreciated.

Sample Class:

class Person < Hashie::Trash
  include Hashie::Extensions::IndifferentAccess
  include Hashie::Extensions::IgnoreUndeclared

  property :first_name, from: :name, transform_with: ->(name) { name['first'] }
  property :last_name, from: :name, transform_with: ->(name) { name['last'] }
  property :primary_phone, from: :phoneNumbers, transform_with: ->(numbers) { numbers['primary'] }
end

Sample Hash:

{"name"=>{"first"=>"First", "last"=>"Last"}, "phoneNumbers"=>{"primary"=>"9999999999"}}

Output:

irb(main):001:0> hash = {"name"=>{"first"=>"First", "last"=>"Last"}, "phoneNumbers"=>{"primary"=>"9999999999"}}
=> {"name"=>{"first"=>"First", "last"=>"Last"}, "phoneNumbers"=>{"primary"=>"9999999999"}}
irb(main):002:0> person = Person.new(hash)
=> #<Person primary_phone="9999999999">

If I symbolize the the top level keys everything works as expected

Output with symbolized keys:

irb(main):003:0> hash = {"name"=>{"first"=>"First", "last"=>"Last"}, "phoneNumbers"=>{"primary"=>"9999999999"}}
=> {"name"=>{"first"=>"First", "last"=>"Last"}, "phoneNumbers"=>{"primary"=>"9999999999"}}
irb(main):004:0> person = Person.new(hash.symbolize_keys)
=> #<Person first_name="First" last_name="Last" primary_phone="9999999999">
@dblock dblock added the bug? label Jan 25, 2020
@dblock
Copy link
Member

dblock commented Jan 25, 2020

Looks like a bug to me. Turn it into a spec, try to fix it, we can help.

@jitendra1998
Copy link
Contributor

Hi @dblock and @dub-squared, interested in working on it.

@jitendra1998
Copy link
Contributor

jitendra1998 commented Apr 5, 2020

After some debugging, found that Hashie::Extensions::IgnoreUndeclared is causing issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug? hacktoberfest Issues that would be suitable for Hacktoberfest
Projects
None yet
Development

No branches or pull requests

4 participants