graphiti-api / graphiti

Stylish Graph APIs
https://www.graphiti.dev/
MIT License
985 stars 141 forks source link

no implicit conversion of nil into Hash #11

Closed alexphys closed 7 years ago

alexphys commented 7 years ago

I have the following self association:

class OrganizationResource < JsonapiCompliable::Resource
  type :organizations
  ...
  ...
  ...

  belongs_to :parent,
    scope: -> { Organization.all },
    foreign_key: :parent_id,
    resource: OrganizationResource

  has_many :children,
    foreign_key: :parent_id,
    scope: -> { Organization.all },
    resource: OrganizationResource

end

My controller has the following;

class Api::V1::OrganizationsController < ApiController
  jsonapi resource: OrganizationResource
  ...
  ...
  ...
  def index
    organizations = Organization.accessible_by(current_ability)
    render_jsonapi organizations
  end

While this is working for older versions of suite and combliable (0.5.4, 0.5.7), I get the following error in the latest ones (0.6.1, 0.6.2);

 TypeError - no implicit conversion of nil into Hash:
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:107:in `block in nested_sideload_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:105:in `nested_sideload_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:96:in `block (2 levels) in to_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:88:in `block in to_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:87:in `to_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:107:in `block in nested_sideload_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:105:in `nested_sideload_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:96:in `block (2 levels) in to_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:88:in `block in to_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:87:in `to_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:107:in `block in nested_sideload_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:105:in `nested_sideload_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:96:in `block (2 levels) in to_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:88:in `block in to_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:87:in `to_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:107:in `block in nested_sideload_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:105:in `nested_sideload_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:96:in `block (2 levels) in to_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:88:in `block in to_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:87:in `to_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:107:in `block in nested_sideload_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:105:in `nested_sideload_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:96:in `block (2 levels) in to_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:88:in `block in to_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:87:in `to_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:107:in `block in nested_sideload_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:105:in `nested_sideload_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:96:in `block (2 levels) in to_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:88:in `block in to_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:87:in `to_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:107:in `block in nested_sideload_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:105:in `nested_sideload_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:96:in `block (2 levels) in to_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:88:in `block in to_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:87:in `to_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:107:in `block in nested_sideload_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:105:in `nested_sideload_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:96:in `block (2 levels) in to_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:88:in `block in to_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:87:in `to_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:107:in `block in nested_sideload_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:105:in `nested_sideload_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:96:in `block (2 levels) in to_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:88:in `block in to_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/sideload.rb:87:in `to_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/query.rb:31:in `include_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/query.rb:40:in `association_names'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/query.rb:46:in `to_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/scope.rb:32:in `query_hash'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/scope.rb:49:in `apply_scoping'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/scope.rb:14:in `initialize'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/resource.rb:120:in `build_scope'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/base.rb:43:in `jsonapi_scope'
  jsonapi_compliable (0.6.2) lib/jsonapi_compliable/base.rb:75:in `render_jsonapi'
  app/controllers/api/v1/organizations_controller.rb:41:in `index'

Im on ruby v 2.3.0 and rails 4.1.7

Anu suggestions?

richmolj commented 7 years ago

@alexphys just release 0.6.4 which has a fix for this (and tests to make sure it doesn't happen again). Could you confirm that works for you?

alexphys commented 7 years ago

Hi @richmolj, This ideed fixed the issue, thank you.

P.S. A temporary solution I had was to create a ParentOrganizationResource

e.g.

class OrganizationResource < JsonapiCompliable::Resource
  type :organizations
  ...
  ...
  ...
  belongs_to :parent, 
    foreign_key: :parent_id,
    scope: -> { Organization.all },
    resource: ParentOrganizationResource

end

class ParentOrganizationResource < JsonapiCompliable::Resource
  type :organizations
  use_adapter JsonapiCompliable::Adapters::ActiveRecord
end