jsonapi-rb / jsonapi-renderer

Efficiently render JSON API documents.
http://jsonapi-rb.org
MIT License
27 stars 11 forks source link

if fields is nil, there is an error. #20

Closed NullVoxPopuli closed 7 years ago

NullVoxPopuli commented 7 years ago

is this intentional? 2017-06-23-082158_458x110_scrot above is the workaround.

without I get the error:

       undefined method `each_with_object' for nil:NilClass
beauby commented 7 years ago

Not sure I'm understanding the situation. The fields option can be either not supplied, or a hash of arrays of symbols. Could you provide a full stack trace along with the faulty code?

NullVoxPopuli commented 7 years ago

yup, here is a trace:

  Failure/Error:
           # the json assignment is line 12
           json = JSONAPI::Serializable::Renderer.render(
             data,
             include: params[:include],
             fields: params[:fields],
             class: SerializableUser

           )

     NoMethodError:
       undefined method `each_with_object' for nil:NilClass
     # /home/lprestonsegoiii/.gem/ruby/2.4.0/gems/jsonapi-renderer-0.1.2/lib/jsonapi/renderer/document.rb:53:in `_symbolize_fields'
     # /home/lprestonsegoiii/.gem/ruby/2.4.0/gems/jsonapi-renderer-0.1.2/lib/jsonapi/renderer/document.rb:12:in `initialize'
     # /home/lprestonsegoiii/.gem/ruby/2.4.0/gems/jsonapi-renderer-0.1.2/lib/jsonapi/renderer.rb:23:in `new'
     # /home/lprestonsegoiii/.gem/ruby/2.4.0/gems/jsonapi-renderer-0.1.2/lib/jsonapi/renderer.rb:23:in `render'
     # /home/lprestonsegoiii/.gem/ruby/2.4.0/gems/jsonapi-serializable-0.1.3/lib/jsonapi/serializable/renderer.rb:24:in `render'
     # /home/lprestonsegoiii/.gem/ruby/2.4.0/gems/jsonapi-serializable-0.1.3/lib/jsonapi/serializable/renderer.rb:8:in `render'
     # ./app/controllers/concerns/jsonapi_endpoints.rb:12:in `render_data'

I'm manually invoking jsonapi-rb, just for fun for some benchmarks of various scenarios -- so it's totally possible I messed something up.

Here is the calling code's context if you're curious: https://github.com/NullVoxPopuli/rails-NPlusOneTests/blob/master/app/controllers/concerns/jsonapi_endpoints.rb#L12

beauby commented 7 years ago

Right, so what's happening is that params.fetch(:fields, {}) will result in nil if params[:fields] is explicitly set to nil. I'd accept a PR to fix that (basically replacing the params.fetch(:fields, {}) in this line with params[:fields] || {}.

NullVoxPopuli commented 7 years ago

NOICE, @beauby