Skip to content

AMS root element configuration not respected #3

@ghost

Description

There is an issue when root element is configured this way :

ActiveSupport.on_load(:active_model_serializers) do
  # Disable for all serializers (except ArraySerializer)
  ActiveModel::Serializer.root = false
  # Disable for ArraySerializer
  ActiveModel::ArraySerializer.root = false
end

Actually the gem is doing that :

    def self.version(version, superclass = ActiveModel::Serializer, &block)
      base_class = self
      vklass = Class.new(superclass) do
        self.root(base_class._root)
        alias_method base_class._name.to_sym, :object

        singleton_class.class_eval do
          define_method(:to_s) do
            "(#{base_class.name} VERSION: #{version})"
          end
          alias inspect to_s
        end

It sets root (or not) directly based on parameters passed to the serializer :

[1] poc_api_app(#<Class>) »  superclass
=> ActiveModel::Serializer < Object
[2] poc_api_app(#<Class>) »  superclass._root?
=> false
[3] poc_api_app(#<Class>) »  base_class
=> UserSerializer < ActiveModel::VersionSerializer
[4] poc_api_app(#<Class>) »  base_class._root?
=> true

You can notice that it doesn't use my configuration and still includes the root element because it doesn't check if superclass._root? is false.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions