Skip to content

Serializer.attributes fails with unclear message when an attribute is :method #2043

Open
@dongtong

Description

@dongtong

Expected behavior vs actual behavior

If attributes list has keyword, the serializer will be failed, the error is

Rendered ActiveModel::Serializer::CollectionSerializer with ActiveModelSerializers::Adapter::Attributes ArgumentError: wrong number of arguments (given 0, expected 1)

class ApiSerializer < ApplicationSerializer
  attributes :id,
             :group_name,
             :name,
             :endpoint,
             :method,
             :version,
             :created_at,
             :updated_at,
             :description
  #...
end

Solutions

class ApiSerializer < ApplicationSerializer
  attributes :id,
             :group_name,
             :name,
             :endpoint,
             :version,
             :created_at,
             :updated_at,
             :description
  attribute :method

  def method
     object.method
  end
end

I think active_model_serializers should tell the developer that attributes should not include keywords, otherwise, the developer will not know the error's detail information. If I have time, I will create PR, but I want the library creator could improve this issue.

Thanks a lot

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions