Fix weak entities case (id is not meaningful attribute) with include.

This commit is contained in:
Igor Khodyrev 2018-07-26 16:18:17 +03:00 committed by Shishir Kakaraddi
parent 2eaaa71bd8
commit e3c45d9b1b
2 changed files with 27 additions and 1 deletions

View File

@ -134,7 +134,7 @@ module FastJsonapi
included_records.concat(serializer_records) unless serializer_records.empty?
end
code = "#{record_type}_#{inc_obj.id}"
code = "#{record_type}_#{serializer.id_from_record(inc_obj)}"
next if known_included_objects.key?(code)
known_included_objects[code] = inc_obj

View File

@ -158,6 +158,32 @@ describe FastJsonapi::ObjectSerializer do
end
end
context 'id attribute is the same for actors and not a primary key' do
before do
ActorSerializer.set_id :email
movie.actor_ids = [0, 0, 0]
class << movie
def actors
super.each_with_index { |actor, i| actor.email = "actor#{i}@email.com" }
end
end
end
after { ActorSerializer.set_id nil }
let(:options) { { include: ['actors'] } }
subject { MovieSerializer.new(movie, options).serializable_hash }
it 'returns all actors in includes' do
expect(
subject[:included].select { |i| i[:type] == :actor }.map { |i| i[:id] }
).to eq(
movie.actors.map(&:email)
)
end
end
context 'nested includes' do
it 'has_many to belongs_to: returns correct nested includes when serializable_hash is called' do
# 3 actors, 3 agencies