diff --git a/lib/fast_jsonapi/serialization_core.rb b/lib/fast_jsonapi/serialization_core.rb index 43ad4e3..8598fe0 100644 --- a/lib/fast_jsonapi/serialization_core.rb +++ b/lib/fast_jsonapi/serialization_core.rb @@ -72,7 +72,7 @@ module FastJsonapi temp_hash[:links] = links_hash(record, params) if data_links.present? temp_hash end - record_hash[:relationships] = record_hash[:relationships].merge(relationships_hash(record, uncachable_relationships_to_serialize, params)) if uncachable_relationships_to_serialize.present? + record_hash[:relationships] = record_hash[:relationships].merge(relationships_hash(record, uncachable_relationships_to_serialize, fieldset, params)) if uncachable_relationships_to_serialize.present? record_hash[:meta] = meta_hash(record, params) if meta_to_serialize.present? record_hash else @@ -130,7 +130,7 @@ module FastJsonapi included_objects.each do |inc_obj| if remaining_items(items) - serializer_records = serializer.get_included_records(inc_obj, remaining_items(items), known_included_objects, fieldsets) + serializer_records = serializer.get_included_records(inc_obj, remaining_items(items), known_included_objects, fieldsets, params) included_records.concat(serializer_records) unless serializer_records.empty? end diff --git a/spec/lib/object_serializer_spec.rb b/spec/lib/object_serializer_spec.rb index 07cbbef..06aac37 100644 --- a/spec/lib/object_serializer_spec.rb +++ b/spec/lib/object_serializer_spec.rb @@ -310,6 +310,23 @@ describe FastJsonapi::ObjectSerializer do end end + context 'when serializing included, params should be available in any serializer' do + subject(:serializable_hash) do + options = {} + options[:include] = [:"actors.awards"] + options[:params] = { include_award_year: true } + MovieSerializer.new(movie, options).serializable_hash + end + let(:actor) { movie.actors.first } + let(:award) { actor.awards.first } + let(:year) { award.year } + + it 'passes params to deeply nested includes' do + expect(year).to_not be_blank + expect(serializable_hash[:included][0][:attributes][:year]).to eq year + end + end + context 'when is_collection option present' do subject { MovieSerializer.new(resource, is_collection_options).serializable_hash } diff --git a/spec/shared/contexts/movie_context.rb b/spec/shared/contexts/movie_context.rb index 9061226..b92d3d5 100644 --- a/spec/shared/contexts/movie_context.rb +++ b/spec/shared/contexts/movie_context.rb @@ -80,6 +80,7 @@ RSpec.shared_context 'movie class' do a.id = i a.title = "Test Award #{i}" a.actor_id = id + a.year = 1990 + i end end end @@ -110,7 +111,7 @@ RSpec.shared_context 'movie class' do end class Award - attr_accessor :id, :title, :actor_id + attr_accessor :id, :title, :actor_id, :year end class State @@ -225,6 +226,11 @@ RSpec.shared_context 'movie class' do class AwardSerializer include FastJsonapi::ObjectSerializer attributes :id, :title + attribute :year, if: Proc.new { |record, params| + params[:include_award_year].present? ? + params[:include_award_year] : + false + } belongs_to :actor end