-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
WIP: Serializer doesn't behave well with an attribute called options
#1165
WIP: Serializer doesn't behave well with an attribute called options
#1165
Conversation
@joaomdmoura is there any temporary recommendation (workaround) for this case different than rename my |
options
options
options
options
Update: |
I think I know what this is. There's some confusion in the code between options locals vs. instance variables/methods.. Want me to take a stab at it? Thanks for the tests! |
@bf4 Sure. Thanks 👍 |
And you said this broke between rc2 and rc3? I haven't isolated it as quickly as I thought |
@resource = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) | ||
@serializer = ProfileSerializer.new(@resource) | ||
@resource = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1', options: 'one' }) | ||
@serializer = ProfileWithOptionsSerializer.new(@resource) | ||
@adapter = ActiveModel::Serializer::Adapter.create(@serializer) | ||
@serializable_resource = ActiveModel::SerializableResource.new(@resource) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR passes if we correct
+ @serializer = ProfileWithOptionsSerializer.new(@resource)
@adapter = ActiveModel::Serializer::Adapter.create(@serializer)
- @serializable_resource = ActiveModel::SerializableResource.new(@resource)
+ @serializable_resource = ActiveModel::SerializableResource.new(@resource, serializer: ProfileWithOptionsSerializer)
So, I'm inclined to think this test doesn't reproduce your issue. :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a chance this PR might fix it #1166
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right about my wrong test. The test passed but the assertions are both wrong
[1] pry(#<ActiveModel::SerializableResourceTest>)> @adapter.serializable_hash(options)
=> {:name=>"Name 1", :description=>"Description 1", :options=>{}}
[2] pry(#<ActiveModel::SerializableResourceTest>)> @serializable_resource.serializable_hash(options)
=> {:name=>"Name 1", :description=>"Description 1", :options=>{}}
It is fixed on master, probably #1166
@adapter.serializable_hash(options)
=> {:name=>"Name 1", :description=>"Description 1", :options=>"one"}
[2] pry(#<ActiveModel::SerializableResourceTest>)> @serializable_resource.serializable_hash(options)
=> {:name=>"Name 1", :description=>"Description 1", :options=>"one"}
@bf4 I changed where my problem is happening but it broke many tests. Can you take a look on the new broken tests? Replacing |
@@ -136,7 +136,7 @@ def attributes(options = {}) | |||
|
|||
attributes.each_with_object({}) do |name, hash| | |||
unless self.class._fragmented | |||
hash[name] = send(name) | |||
hash[name] = @object.try!(name) || public_send(name) | |||
else |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
such meta (why try!
over try
?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because try
checks for @object.respond_to?
which returns false.
@thiagogabriel see #1165 (comment) for the reason for the test failure Also, |
@thiagogabriel cool, and see if we can get a good failing test.. you might need to bring over more of your business logic, as well, as your Ruby version, Rails version, AMS version, backtrace, etc. |
@bf4 I'm checking master now. Sorry for my delay. |
…tions` I have an attribute called options being serialized, but it causes a `stack level too deep` on my controller and it started on 0.10.0.rc3. Using AMS test suit I checked it happens because of `options` attribute. The expected is: `{:name=>"Name 1", :description=>"Description 1", :options=>'one'}` but with @adapter.serializable_hash(options) I get: `{:name=>"Name 1", :description=>"Description 1", :options=>{}}` and with @serializable_resource.serializable_hash(options) I get: `{:name=>"Name 1", :description=>"Description 1"}`
3a661c8
to
e20daf3
Compare
I have an attribute called options being serialized, but it causes a
stack level too deep
on my controller and it started on 0.10.0.rc3.Using AMS test suit I checked it happens because of
options
attribute.The expected is:
{:name=>"Name 1", :description=>"Description 1", :options=>'one'}
but with @adapter.serializable_hash(options) I get:
{:name=>"Name 1", :description=>"Description 1", :options=>{}}
and with @serializable_resource.serializable_hash(options) I get:
{:name=>"Name 1", :description=>"Description 1"}