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

Prevent undefined method for Array:Class error #2329

Closed
wants to merge 2 commits into from
Closed

Prevent undefined method for Array:Class error #2329

wants to merge 2 commits into from

Conversation

jpawlyn
Copy link

@jpawlyn jpawlyn commented May 1, 2019

Purpose

Fix NoMethodError: undefined method 'model_name' for Array:Class. This occurs when serializing an Array object.

This originates from adding fieldset support - #2223.

Changes

Add greater defensiveness when calling json_key.

Caveats

Related GitHub issues

Additional helpful information

This came about when adding fieldset support - #2223
@wasifhossain
Copy link
Member

Thank you @jpawlyn for your PR. would like to know your feedback on my comment above.

@jpawlyn
Copy link
Author

jpawlyn commented May 2, 2019

Thanks @wasifhossain for the explanation and example. What I did for the array serialization test was to simplify a serializer from our app but I think I oversimplified it!

I've added another top level attribute to better show the problem we're facing. If there's a solution that doesn't involve changing the code that would be really great.

class Serializer
class SerializationArrayTest < ActiveSupport::TestCase
class ArraySerializer < ActiveModel::Serializer
attributes :total, :items
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jpawlyn can you put a type option here with any name you like and rerun all the tests again. I believe it should pass the tests.

type 'array'

attributes :total, :items # you can add any number of fields you like

The type value is used at the top level of the json if you use the :json adapter, but skipped for the :attributes adapter.

You can find the details of this option here: https://github.com/rails-api/active_model_serializers/blob/0-10-stable/docs/general/serializers.md#type

Though it says this option is not useful with the :attributes adapter, but providing it in the non-resource serializers like yours will help overcome the issue that you are having.

Let me know if you still find any issue after providing this option. Thanks.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's great, thanks a lot for your help with this @wasifhossain, much appreciated. I'll close the PR now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are most welcome! glad that it worked for you 🙌

setup do
@authors = [Author.new(id: 1, name: 'Blog Author')]
@serializer_instance = ArraySerializer.new(@authors)
@serializable = ActiveModelSerializers::SerializableResource.new(@authors, serializer: ArraySerializer, adapter: :attributes)
Copy link
Member

@wasifhossain wasifhossain May 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#2223 introduces the fields option for the :attributes adapter. So we can further trim down the fields that we want to render.

@serializable = ActiveModelSerializers::SerializableResource.new(
  @authors,
  serializer: ArraySerializer,
  adapter: :attributes,
  fields: [:items]
)

But now if we run the tests again with the code above, this change

if fieldset && fieldset.fields.any?
will not be enough to prevent the issue:

NoMethodError: undefined method `model_name'

generated from

object.class.model_name.to_s.underscore

@jpawlyn
Copy link
Author

jpawlyn commented May 3, 2019

No need for PR since using type option solves the problem

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

Successfully merging this pull request may close these issues.

2 participants