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

Incorrect behavior with fields named after object methods #2002

Open
idoa01 opened this issue Dec 15, 2016 · 0 comments
Open

Incorrect behavior with fields named after object methods #2002

idoa01 opened this issue Dec 15, 2016 · 0 comments

Comments

@idoa01
Copy link

idoa01 commented Dec 15, 2016

Expected behavior vs actual behavior

When using an attribute with an object method name (in my case display, but any other will cause the bug) the serializers will get the method's output instead of the value sent for serialization.

Steps to reproduce

(e.g., detailed walkthrough, runnable script, example application)

require 'active_model_serializers'

class SomeResource < ActiveModelSerializers::Model
  attr_accessor :id, :display, :first_name, :hash
end

class SomeSerializer < ActiveModel::Serializer
  attributes :id, :display, :first_name, :hash
end

> resource = SomeResource.new(id: 1, display: "Chuck Norris", first_name: "Chuck", hash: "1234")
> puts SomeSerializer.new(resource).to_json

<SomeSerializer:0x007f898fb03860>{"id":1,"display":null,"first_name":"Chuck","hash":-2648756963900511786}

> puts resource.attributes
{:id=>1, :display=>"Chuck Norris", :first_name=>"Chuck", :hash=>"1234"}

Environment

ActiveModelSerializers Version (commit ref if not on tag):
ActiveModel::Serializer::VERSION => "0.10.3"

Output of ruby -e "puts RUBY_DESCRIPTION":
ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-darwin14.0]

OS Type & Version:
MacOS Sierra 10.12.1

Integrated application and version (e.g., Rails, Grape, etc):
None

Backtrace

(e.g., provide any applicable backtraces from your application)

Additonal helpful information

(e.g., Gemfile.lock, configurations, PR containing a failing test, git bisect results)

looking into github, the bug is here:

if respond_to?(attr)
send(attr)

inserted in commit 6370e5c

maybe switching the order of the if/else will fix this?
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant