Skip to content

includes no longer works with sparse fields #122

Closed
@krainboltgreene

Description

@krainboltgreene
    33: def initialize(object, options = {})
    34:   @object = object
    35:   @options = options
    36:   @context = options[:context] || {}
    37:   @base_url = options[:base_url]
 => 38:   binding.pry
    39:   # Internal serializer options, not exposed through attr_accessor. No touchie.
    40:   @_fields = options[:fields] || {}
    41:   @_include_linkages = options[:include_linkages] || []
    42: end
[2] pry(#<V1::AccountSerializer>)> object
=> #<Account id: "d38bd004-e2e0-474e-a58a-129c32608109", name: "Sally Stuthers", email: "sally@example.com", username: "sally", onboarding_state: "completed", role_state: "administrator", authentication_secret: "n5xZVVpmHpy7UnSrpY_L", created_at: "2018-08-26 12:46:59", updated_at: "2018-08-26 12:46:59">
[3] pry(#<V1::AccountSerializer>)> options
=> {:context=>{:policy_finder=>#<Method: main.policy>},
 :serializer=>nil,
 :namespace=>V1,
 :include=>["payments"],
 :fields=>{"accounts"=>[:name]},
 :base_url=>nil,
 :include_linkages=>["payments"]}
    197: def has_many_relationships
    198:   return {} if self.class.to_many_associations.nil?
 => 199:   binding.pry
    200:   data = {}
    201:   self.class.to_many_associations.each do |attribute_name, attr_data|
    202:     next if !should_include_attr?(attribute_name, attr_data)
    203:     data[attribute_name] = attr_data
    204:   end
    205:   data
    206: end

When running 201..205 the value of data is {}. This is because should_include_attr?() checks to see (for some reason) if payments is inside @_fields:

    212: def should_include_attr?(attribute_name, attr_data)
    213:   # Allow "if: :show_title?" and "unless: :hide_title?" attribute options.
    214:   if_method_name = attr_data[:options][:if]
    215:   unless_method_name = attr_data[:options][:unless]
    216:   formatted_attribute_name = format_name(attribute_name).to_sym
    217:   show_attr = true
    218:   show_attr &&= send(if_method_name) if if_method_name
    219:   show_attr &&= !send(unless_method_name) if unless_method_name
 => 220:   binding.pry
    221:   show_attr &&= @_fields[type.to_s].include?(formatted_attribute_name) if @_fields[type.to_s]
    222:   show_attr
    223: end
[5] pry(#<V1::AccountSerializer>)> attribute_name
=> :payments
[6] pry(#<V1::AccountSerializer>)> attr_data
=> {:attr_or_block=>#<Proc:0x00007fde701dee98@/Users/krainboltgreene/Code/krainboltgreene/blank-api-rails/app/serializers/v1/application_serializer.rb:44 (lambda)>,
 :options=>{:if=>:allow_payments?, :include_links=>true, :include_data=>false}}
[7] pry(#<V1::AccountSerializer>)> show_attr
=> true
[8] pry(#<V1::AccountSerializer>)> @_fields[type.to_s].include?(formatted_attribute_name) if @_fields[type.to_s]
=> false
[9] pry(#<V1::AccountSerializer>)> @_fields[type.to_s]
=> [:name]

But...of course it's not, why would it be inside the fields list? There's no requirement for that in the specification: http://jsonapi.org/format/#fetching-sparse-fieldsets

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions