{"action":"created","issue":{"url":"https://api.github.com/repos/apotonick/representable/issues/115","labels_url":"https://api.github.com/repos/apotonick/representable/issues/115/labels{/name}","comments_url":"https://api.github.com/repos/apotonick/representable/issues/115/comments","events_url":"https://api.github.com/repos/apotonick/representable/issues/115/events","html_url":"https://github.com/apotonick/representable/issues/115","id":46050470,"number":115,"title":"Calling `#from_hash`: NoMethodError: undefined method `has_key?' for nil:NilClass","user":{"login":"moxley","id":86996,"avatar_url":"https://avatars.githubusercontent.com/u/86996?v=3","gravatar_id":"","url":"https://api.github.com/users/moxley","html_url":"https://github.com/moxley","followers_url":"https://api.github.com/users/moxley/followers","following_url":"https://api.github.com/users/moxley/following{/other_user}","gists_url":"https://api.github.com/users/moxley/gists{/gist_id}","starred_url":"https://api.github.com/users/moxley/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/moxley/subscriptions","organizations_url":"https://api.github.com/users/moxley/orgs","repos_url":"https://api.github.com/users/moxley/repos","events_url":"https://api.github.com/users/moxley/events{/privacy}","received_events_url":"https://api.github.com/users/moxley/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":9,"created_at":"2014-10-17T00:15:51Z","updated_at":"2015-07-29T19:21:44Z","closed_at":null,"body":"When calling `#from_hash` or `#from_json` with parameters that contain `nil` for a nested property, it raises the following error:\r\n\r\n```\r\nNoMethodError: undefined method `has_key?' for nil:NilClass\r\n    ruby-2.1.2/gems/representable-2.1.0/lib/representable/hash/binding.rb:13:in `read'\r\n    ruby-2.1.2/gems/representable-2.1.0/lib/representable/binding.rb:64:in `read_fragment'\r\n    ruby-2.1.2/gems/representable-2.1.0/lib/representable/binding.rb:45:in `block in uncompile_fragment'\r\n    ruby-2.1.2/gems/representable-2.1.0/lib/representable/binding.rb:99:in `evaluate_option'\r\n    ruby-2.1.2/gems/representable-2.1.0/lib/representable/binding.rb:44:in `uncompile_fragment'\r\n    ruby-2.1.2/gems/representable-2.1.0/lib/representable/mapper.rb:68:in `uncompile_fragment'\r\n    ruby-2.1.2/gems/representable-2.1.0/lib/representable/mapper.rb:35:in `deserialize_property'\r\n    ruby-2.1.2/gems/representable-2.1.0/lib/representable/mapper.rb:15:in `block in deserialize'\r\n    ruby-2.1.2/gems/representable-2.1.0/lib/representable/mapper.rb:14:in `each'\r\n    ruby-2.1.2/gems/representable-2.1.0/lib/representable/mapper.rb:14:in `deserialize'\r\n    ruby-2.1.2/gems/representable-2.1.0/lib/representable.rb:34:in `update_properties_from'\r\n    ruby-2.1.2/gems/representable-2.1.0/lib/representable/hash.rb:31:in `from_hash'\r\n    ruby-2.1.2/gems/representable-2.1.0/lib/representable/deserializer.rb:31:in `block in deserialize'\r\n    ruby-2.1.2/gems/representable-2.1.0/lib/representable/binding.rb:99:in `evaluate_option'\r\n    ruby-2.1.2/gems/representable-2.1.0/lib/representable/deserializer.rb:30:in `deserialize'\r\n    ruby-2.1.2/gems/representable-2.1.0/lib/representable/deserializer.rb:23:in `call'\r\n    ruby-2.1.2/gems/representable-2.1.0/lib/representable/populator.rb:34:in `deserialize'\r\n    ruby-2.1.2/gems/representable-2.1.0/lib/representable/populator.rb:20:in `call'\r\n    ruby-2.1.2/gems/representable-2.1.0/lib/representable/binding.rb:66:in `read_fragment'\r\n    ruby-2.1.2/gems/representable-2.1.0/lib/representable/binding.rb:45:in `block in uncompile_fragment'\r\n    ruby-2.1.2/gems/representable-2.1.0/lib/representable/binding.rb:99:in `evaluate_option'\r\n    ruby-2.1.2/gems/representable-2.1.0/lib/representable/binding.rb:44:in `uncompile_fragment'\r\n    ruby-2.1.2/gems/representable-2.1.0/lib/representable/mapper.rb:68:in `uncompile_fragment'\r\n    ruby-2.1.2/gems/representable-2.1.0/lib/representable/mapper.rb:35:in `deserialize_property'\r\n    ruby-2.1.2/gems/representable-2.1.0/lib/representable/mapper.rb:15:in `block in deserialize'\r\n    ruby-2.1.2/gems/representable-2.1.0/lib/representable/mapper.rb:14:in `each'\r\n    ruby-2.1.2/gems/representable-2.1.0/lib/representable/mapper.rb:14:in `deserialize'\r\n    ruby-2.1.2/gems/representable-2.1.0/lib/representable.rb:34:in `update_properties_from'\r\n    ruby-2.1.2/gems/representable-2.1.0/lib/representable/hash.rb:31:in `from_hash'\r\n    null_child_test.rb:27:in `block in <class:NullChildTest>'\r\n```\r\n\r\nHere's a test that reproduces it:\r\n\r\n```ruby\r\nrequire 'minitest/autorun'\r\nrequire 'representable/json'\r\n\r\nclass NullChildTest < MiniTest::Spec\r\n  class ParentClass\r\n    attr_accessor :child\r\n  end\r\n\r\n  class ChildClass\r\n    attr_accessor :id\r\n  end\r\n\r\n  module ChildClassRepresenter\r\n    include Representable::JSON\r\n    property :id\r\n  end\r\n\r\n  module ParentClassRepresenter\r\n    include Representable::JSON\r\n    property :child, class: ChildClass, extend: ChildClassRepresenter\r\n  end\r\n\r\n  it \"allows child to be nil\" do\r\n    parent = ParentClass.new.extend(ParentClassRepresenter)\r\n\r\n    # Raises error: undefined method `has_key?` for nil:NilClass\r\n    parent.from_hash 'child' => nil\r\n  end\r\nend\r\n```\r\n\r\nGemfile:\r\n\r\n```ruby\r\ngem \"representable\", git: 'git@github.com:apotonick/representable.git', ref: 'master'\r\ngem \"minitest\"\r\n```\r\n\r\nIs there any way to allow a `nil` value for the child? I don't have a workaround other than manually looking for `nil` in the input hash for the child property and deleting it from the Hash."},"comment":{"url":"https://api.github.com/repos/apotonick/representable/issues/comments/126066162","html_url":"https://github.com/apotonick/representable/issues/115#issuecomment-126066162","issue_url":"https://api.github.com/repos/apotonick/representable/issues/115","id":126066162,"user":{"login":"apotonick","id":10406,"avatar_url":"https://avatars.githubusercontent.com/u/10406?v=3","gravatar_id":"","url":"https://api.github.com/users/apotonick","html_url":"https://github.com/apotonick","followers_url":"https://api.github.com/users/apotonick/followers","following_url":"https://api.github.com/users/apotonick/following{/other_user}","gists_url":"https://api.github.com/users/apotonick/gists{/gist_id}","starred_url":"https://api.github.com/users/apotonick/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apotonick/subscriptions","organizations_url":"https://api.github.com/users/apotonick/orgs","repos_url":"https://api.github.com/users/apotonick/repos","events_url":"https://api.github.com/users/apotonick/events{/privacy}","received_events_url":"https://api.github.com/users/apotonick/received_events","type":"User","site_admin":false},"created_at":"2015-07-29T19:21:44Z","updated_at":"2015-07-29T19:21:44Z","body":"@moxley I merged your test and found out a weird inconsistency. You do\r\n\r\n```ruby\r\n it \"sets represented property to nil when incoming value is nil\" do\r\n      portland = Hometown.new\r\n      portland.name = \"Portland\"\r\n      @band.hometown = portland\r\n      @band.from_hash 'hometown' => nil\r\n      assert_equal nil, @band.hometown.name\r\n```\r\n\r\nSo, you say `from_hash {hometown: nil}` but then you test `band.hometown.name`, which works with your fix. However, `hometown` should be `nil` - at least, that's how I would interpret `{hometown: nil}`. Any ideas?"}}