Skip to content

Commit

Permalink
Prevent creation of unnecessary fieldset(mirror changes from rails-ap…
Browse files Browse the repository at this point in the history
…i#2370)

* refactor: instance_options[:fieldset] must be nil as it's not listed in ADAPTER_OPTION_KEYS
  • Loading branch information
liijunwei committed Aug 12, 2023
1 parent 9fa9373 commit 176c31f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
12 changes: 9 additions & 3 deletions lib/active_model_serializers/adapter/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ module Adapter
class Attributes < Base
def initialize(*)
super
instance_options[:fieldset] ||= ActiveModel::Serializer::Fieldset.new(fields_to_fieldset(instance_options.delete(:fields)))

fields = instance_options.delete(:fields)
fieldset = fields_to_fieldset(fields)
instance_options[:fieldset] = ActiveModel::Serializer::Fieldset.new(fieldset) if fieldset
end

def serializable_hash(options = nil)
Expand All @@ -19,17 +22,20 @@ def serializable_hash(options = nil)
private

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

resource_fields = []
relationship_fields = {}

fields.each do |field|
case field
when Symbol, String then resource_fields << field
when Hash then relationship_fields.merge!(field)
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)

relationship_fields.merge(serializer.json_key.to_sym => resource_fields)
end
end
end
Expand Down
9 changes: 6 additions & 3 deletions lib/active_model_serializers/adapter/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ 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))
option_fields = options.delete(:fields)
@fieldset = ActiveModel::Serializer::Fieldset.new(option_fields) if option_fields
end

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

def data_for(serializer, include_slice)
requested_fields = fieldset && fieldset.fields_for(resource_object[:type])

data = serializer.fetch(self) do
resource_object = ResourceIdentifier.new(serializer, instance_options).as_json
break nil if resource_object.nil?

requested_fields = fieldset && fieldset.fields_for(resource_object[:type])
attributes = attributes_for(serializer, requested_fields)
resource_object[:attributes] = attributes if attributes.any?
resource_object
end

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 = requested_fields || '*'
relationships = relationships_for(serializer, requested_associations, include_slice)
resource_object[:relationships] = relationships if relationships.any?
end
Expand Down

0 comments on commit 176c31f

Please sign in to comment.