Skip to content

Commit 26a1dd8

Browse files
committed
Prevent creation of unnecessary fieldset(mirror and revise changes from rails-api#2370)
Co-authored-by: https://github.com/abcang
1 parent 9fa9373 commit 26a1dd8

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

lib/active_model_serializers/adapter/attributes.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ module Adapter
55
class Attributes < Base
66
def initialize(*)
77
super
8-
instance_options[:fieldset] ||= ActiveModel::Serializer::Fieldset.new(fields_to_fieldset(instance_options.delete(:fields)))
8+
9+
fields = instance_options.delete(:fields)
10+
fieldset = fields_to_fieldset(fields)
11+
instance_options[:fieldset] = ActiveModel::Serializer::Fieldset.new(fieldset) if fieldset
912
end
1013

1114
def serializable_hash(options = nil)

lib/active_model_serializers/adapter/json_api.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ def self.fragment_cache(cached_hash, non_cached_hash, root = true)
5353
def initialize(serializer, options = {})
5454
super
5555
@include_directive = JSONAPI::IncludeDirective.new(options[:include], allow_wildcard: true)
56-
@fieldset = options[:fieldset] || ActiveModel::Serializer::Fieldset.new(options.delete(:fields))
56+
option_fields = options.delete(:fields)
57+
@fieldset = ActiveModel::Serializer::Fieldset.new(option_fields) if option_fields
5758
end
5859

5960
# {http://jsonapi.org/format/#crud Requests are transactional, i.e. success or failure}
@@ -348,7 +349,8 @@ def data_for(serializer, include_slice)
348349
data.tap do |resource_object|
349350
next if resource_object.nil?
350351
# NOTE(BF): the attributes are cached above, separately from the relationships, below.
351-
requested_associations = fieldset.fields_for(resource_object[:type]) || '*'
352+
requested_fields = fieldset && fieldset.fields_for(resource_object[:type])
353+
requested_associations = requested_fields || '*'
352354
relationships = relationships_for(serializer, requested_associations, include_slice)
353355
resource_object[:relationships] = relationships if relationships.any?
354356
end

0 commit comments

Comments
 (0)