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

WIP: Serializer doesn't behave well with an attribute called options #1165

Conversation

thiagogabriel
Copy link

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"}

@thiagogabriel
Copy link
Author

@joaomdmoura is there any temporary recommendation (workaround) for this case different than rename my options attribute?

@thiagogabriel thiagogabriel changed the title SerializableResource doesn't behave well with an attribute called options WIP: SerializableResource doesn't behave well with an attribute called options Sep 17, 2015
@thiagogabriel thiagogabriel changed the title WIP: SerializableResource doesn't behave well with an attribute called options WIP: Serializer doesn't behave well with an attribute called options Sep 17, 2015
@thiagogabriel
Copy link
Author

Update:
The problem is not exactly with SerializableResource, but was introduced on rc3.

@bf4
Copy link
Member

bf4 commented Sep 17, 2015

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!

@thiagogabriel
Copy link
Author

@bf4 Sure. Thanks 👍
Maybe it is better to move or include new tests on the proper place, since the bug is not exactly on SerializableResource.

@bf4
Copy link
Member

bf4 commented Sep 17, 2015

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)
Copy link
Member

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. :(

Copy link
Member

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

Copy link
Author

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"}

@thiagogabriel
Copy link
Author

@bf4 I changed where my problem is happening but it broke many tests. Can you take a look on the new broken tests? Replacing try! with try fixes all the broken tests but the options values are ignored.

@@ -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
Copy link
Member

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?)

Copy link
Author

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.

@bf4
Copy link
Member

bf4 commented Sep 17, 2015

@thiagogabriel see #1165 (comment) for the reason for the test failure

Also, attributes and attribute both define methods on the Serializer that delegate to the object, so there should be no need to check the object for the method

@thiagogabriel
Copy link
Author

@bf4 I'll take a better look on you PR #1165 tomorrow.

@bf4
Copy link
Member

bf4 commented Sep 17, 2015

@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.

@thiagogabriel
Copy link
Author

@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"}`
@thiagogabriel thiagogabriel force-pushed the accept-options-attribute-on-serializable-resource branch from 3a661c8 to e20daf3 Compare September 21, 2015 23:44
@thiagogabriel thiagogabriel deleted the accept-options-attribute-on-serializable-resource branch September 22, 2015 00:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants