Skip to content
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

Prevent creation of unnecessary fieldset #2370

Open
wants to merge 1 commit into
base: 0-10-stable
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
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