Skip to content

Collection parsing raises an error if the value is nil #232

@fabioperrella

Description

@fabioperrella

Hi! I'm not sure if this is a bug or if there is some way to deal with it.

Given a representable with a collection inside:

class SongRepresenter < Representable::Decorator
  include Representable::JSON  
  
  property :title  
  property :track  
  collection :composers  
end 

When trying to parse a Json with collection: [], it works!

pry(main)> hash = {title: "aa", track: "bb", composers: []}
pry(main)> SongRepresenter.new(Song.new).from_json(hash.to_json)                                                                                                                                                
=> #<Song title="aa", track="bb", composers=[]>

But when setting composer: nil, it raises an error (very difficult to understand):

pry(main)> hash = {title: "aa", track: "bb", composers: nil}
pry(main)> SongRepresenter.new(Song.new).from_json(hash.to_json)                                                                                                                                                
NoMethodError: undefined method `each_with_index' for nil:NilClass
from /home/fabioperrella/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/representable-2.3.0/lib/representable/deserializer.rb:86:in `call'

I tried to use render_nil: true, but the same problem occurred.

I got this problem because Rails 4.x is replacing the [] value by nil -> rails/rails#12251 (comment)

Is there any option to a collection accept a nil value?

Is this really a bug?

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions