Skip to content

Commit

Permalink
Prevent creation of unnecessary fieldset
Browse files Browse the repository at this point in the history
  • Loading branch information
abcang committed Aug 10, 2023
1 parent 9fa9373 commit 7ab98a9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/active_model_serializers/adapter/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Adapter
class Attributes < Base
def initialize(*)
super
instance_options[:fieldset] ||= ActiveModel::Serializer::Fieldset.new(fields_to_fieldset(instance_options.delete(:fields)))
instance_options[:fieldset] ||= fields_to_fieldset(instance_options.delete(:fields))
end

def serializable_hash(options = nil)
Expand All @@ -29,7 +29,7 @@ def fields_to_fieldset(fields)
else fail ArgumentError, "Unknown conversion of fields to fieldset: '#{field.inspect}' in '#{fields.inspect}'"
end
end
relationship_fields.merge!(serializer.json_key.to_sym => resource_fields)
ActiveModel::Serializer::Fieldset.new(relationship_fields.merge!(serializer.json_key.to_sym => resource_fields))
end
end
end
Expand Down
12 changes: 10 additions & 2 deletions lib/active_model_serializers/adapter/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def self.fragment_cache(cached_hash, non_cached_hash, root = true)
def initialize(serializer, options = {})
super
@include_directive = JSONAPI::IncludeDirective.new(options[:include], allow_wildcard: true)
@fieldset = options[:fieldset] || ActiveModel::Serializer::Fieldset.new(options.delete(:fields))
@fieldset = options[:fieldset] || fields_to_fieldset(options.delete(:fields))
end

# {http://jsonapi.org/format/#crud Requests are transactional, i.e. success or failure}
Expand Down Expand Up @@ -335,6 +335,7 @@ def resource_object_for(serializer, include_slice = {})
resource_object
end

# rubocop:disable Metrics/CyclomaticComplexity
def data_for(serializer, include_slice)
data = serializer.fetch(self) do
resource_object = ResourceIdentifier.new(serializer, instance_options).as_json
Expand All @@ -348,11 +349,12 @@ def data_for(serializer, include_slice)
data.tap do |resource_object|
next if resource_object.nil?
# NOTE(BF): the attributes are cached above, separately from the relationships, below.
requested_associations = fieldset.fields_for(resource_object[:type]) || '*'
requested_associations = (fieldset && fieldset.fields_for(resource_object[:type])) || '*'
relationships = relationships_for(serializer, requested_associations, include_slice)
resource_object[:relationships] = relationships if relationships.any?
end
end
# rubocop:enable Metrics/CyclomaticComplexity

# {http://jsonapi.org/format/#document-resource-object-relationships Document Resource Object Relationship}
# relationships
Expand Down Expand Up @@ -529,6 +531,12 @@ def pagination_links_for(serializer)
def meta_for(serializer)
Meta.new(serializer).as_json
end

def fields_to_fieldset(fields)
return fields if fields.nil?

ActiveModel::Serializer::Fieldset.new(fields)
end
end
end
end
Expand Down

0 comments on commit 7ab98a9

Please sign in to comment.