Skip to content

Commit e3c45d9

Browse files
hwo411shishirmk
authored andcommitted
Fix weak entities case (id is not meaningful attribute) with include.
1 parent 2eaaa71 commit e3c45d9

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

lib/fast_jsonapi/serialization_core.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def get_included_records(record, includes_list, known_included_objects, fieldset
134134
included_records.concat(serializer_records) unless serializer_records.empty?
135135
end
136136

137-
code = "#{record_type}_#{inc_obj.id}"
137+
code = "#{record_type}_#{serializer.id_from_record(inc_obj)}"
138138
next if known_included_objects.key?(code)
139139

140140
known_included_objects[code] = inc_obj

spec/lib/object_serializer_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,32 @@
158158
end
159159
end
160160

161+
context 'id attribute is the same for actors and not a primary key' do
162+
before do
163+
ActorSerializer.set_id :email
164+
movie.actor_ids = [0, 0, 0]
165+
class << movie
166+
def actors
167+
super.each_with_index { |actor, i| actor.email = "actor#{i}@email.com" }
168+
end
169+
end
170+
end
171+
172+
after { ActorSerializer.set_id nil }
173+
174+
let(:options) { { include: ['actors'] } }
175+
subject { MovieSerializer.new(movie, options).serializable_hash }
176+
177+
it 'returns all actors in includes' do
178+
179+
expect(
180+
subject[:included].select { |i| i[:type] == :actor }.map { |i| i[:id] }
181+
).to eq(
182+
movie.actors.map(&:email)
183+
)
184+
end
185+
end
186+
161187
context 'nested includes' do
162188
it 'has_many to belongs_to: returns correct nested includes when serializable_hash is called' do
163189
# 3 actors, 3 agencies

0 commit comments

Comments
 (0)