Skip to content

Commit f864099

Browse files
TrevorHinesleyshishirmk
authored andcommitted
Conditional relationships should be removed from included when proc evaluates to false
1 parent 0b70657 commit f864099

2 files changed

Lines changed: 42 additions & 9 deletions

File tree

lib/fast_jsonapi/serialization_core.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ def get_included_records(record, includes_list, known_included_objects, params =
146146
items = parse_include_item(include_item)
147147
items.each do |item|
148148
next unless relationships_to_serialize && relationships_to_serialize[item]
149+
conditional_proc = relationships_to_serialize[item][:conditional_proc]
150+
next if conditional_proc && !conditional_proc.call(record, params)
149151
raise NotImplementedError if @relationships_to_serialize[item][:polymorphic].is_a?(Hash)
150152
record_type = @relationships_to_serialize[item][:record_type]
151153
serializer = @relationships_to_serialize[item][:serializer].to_s.constantize

spec/lib/object_serializer_spec.rb

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -349,11 +349,27 @@ class BlahSerializer
349349
expect(serializable_hash['data']['relationships'].has_key?('actors')).to be_truthy
350350
end
351351

352-
it "doesn't return optional relationship when relationship is not included" do
353-
movie.actor_ids = []
354-
json = MovieOptionalRelationshipSerializer.new(movie).serialized_json
355-
serializable_hash = JSON.parse(json)
356-
expect(serializable_hash['data']['relationships'].has_key?('actors')).to be_falsey
352+
context "when relationship is not included" do
353+
let(:json) {
354+
MovieOptionalRelationshipSerializer.new(movie, options).serialized_json
355+
}
356+
let(:options) {
357+
{}
358+
}
359+
let(:serializable_hash) {
360+
JSON.parse(json)
361+
}
362+
363+
it "doesn't return optional relationship" do
364+
movie.actor_ids = []
365+
expect(serializable_hash['data']['relationships'].has_key?('actors')).to be_falsey
366+
end
367+
368+
it "doesn't include optional relationship" do
369+
movie.actor_ids = []
370+
options[:include] = [:actors]
371+
expect(serializable_hash['included']).to be_blank
372+
end
357373
end
358374
end
359375

@@ -364,10 +380,25 @@ class BlahSerializer
364380
expect(serializable_hash['data']['relationships'].has_key?('owner')).to be_truthy
365381
end
366382

367-
it "doesn't return optional relationship when relationship is not included" do
368-
json = MovieOptionalRelationshipWithParamsSerializer.new(movie, { params: { admin: false }}).serialized_json
369-
serializable_hash = JSON.parse(json)
370-
expect(serializable_hash['data']['relationships'].has_key?('owner')).to be_falsey
383+
context "when relationship is not included" do
384+
let(:json) {
385+
MovieOptionalRelationshipWithParamsSerializer.new(movie, options).serialized_json
386+
}
387+
let(:options) {
388+
{ params: { admin: false }}
389+
}
390+
let(:serializable_hash) {
391+
JSON.parse(json)
392+
}
393+
394+
it "doesn't return optional relationship" do
395+
expect(serializable_hash['data']['relationships'].has_key?('owner')).to be_falsey
396+
end
397+
398+
it "doesn't include optional relationship" do
399+
options[:include] = [:owner]
400+
expect(serializable_hash['included']).to be_blank
401+
end
371402
end
372403
end
373404
end

0 commit comments

Comments
 (0)