Skip to content

Fragment cache + array payload compounds non-cached keys #904

Closed
@apolzon

Description

If you define a serializer with a fragment cache, such as

class TestSerializer < ActiveModel::Serializer
  cache key: "test", except: [:field1]
  attributes :field1, :field2
end

and then render in your controller, an array:

render json: resources, each_serializer: TestSerializer

the fragment cache's call to attribute (line 52) will continually add field1, such that if resources has 20 objects, by the end, field1 will be called 20 times for the last invocation of the TestSerializer.

I believe ActiveModel::Serializer.attribute should only concat the key onto @_attributes if it is not already present.
i.e., line 45 (in master) should be:

      @_attributes.concat [key] unless @_attributes.include? key

Also, since the @_attributes variable is at the class level, subsequent requests compound the issue even further.

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions