Skip to content

records should be included in known_included_objects to avoid duplication #381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/fast_jsonapi/object_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ def hash_for_one_record

return serializable_hash unless @resource

record_type = @resource.class.name.demodulize.underscore
@known_included_objects["#{record_type}_#{self.class.id_from_record(@resource)}"] = @resource
serializable_hash[:data] = self.class.record_hash(@resource, @fieldsets[self.class.record_type.to_sym], @params)
serializable_hash[:included] = self.class.get_included_records(@resource, @includes, @known_included_objects, @fieldsets, @params) if @includes.present?
serializable_hash
Expand All @@ -55,6 +57,8 @@ def hash_for_collection
included = []
fieldset = @fieldsets[self.class.record_type.to_sym]
@resource.each do |record|
record_type = record.class.name.demodulize.underscore
@known_included_objects["#{record_type}_#{self.class.id_from_record(record)}"] = record
data << self.class.record_hash(record, fieldset, @params)
included.concat self.class.get_included_records(record, @includes, @known_included_objects, @fieldsets, @params) if @includes.present?
end
Expand All @@ -75,10 +79,10 @@ def serialized_json
def process_options(options)
@fieldsets = deep_symbolize(options[:fields].presence || {})
@params = {}
@known_included_objects = {}

return if options.blank?

@known_included_objects = {}
@meta = options[:meta]
@links = options[:links]
@is_collection = options[:is_collection]
Expand Down
2 changes: 1 addition & 1 deletion lib/fast_jsonapi/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module FastJsonapi
VERSION = "1.4"
VERSION = "1.5"
end
20 changes: 20 additions & 0 deletions spec/lib/object_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,26 @@ def advertising_campaign
expect(groups_serialized).to include(group.id)
end
end

it 'does not duplicate records in included' do
options = {}
options[:meta] = { total: 1 }
options[:links] = { self: 'self' }
options[:include] = [:actors, :'movie_type.movies']
serializable_hash = MovieSerializer.new([movie, movie], options).serializable_hash

expect(serializable_hash[:data].length).to eq 2
expect(serializable_hash[:data][0][:relationships].length).to eq 4
expect(serializable_hash[:data][0][:attributes].length).to eq 2

expect(serializable_hash[:meta]).to be_instance_of(Hash)
expect(serializable_hash[:links]).to be_instance_of(Hash)

expect(serializable_hash[:included]).to be_instance_of(Array)
expect(serializable_hash[:included][0]).to be_instance_of(Hash)
# 3 actors, one movie_type
expect(serializable_hash[:included].length).to eq 4
end
end

context 'when testing included do block of object serializer' do
Expand Down