Netflix / fast_jsonapi

No Longer Maintained - A lightning fast JSON:API serializer for Ruby Objects.
Apache License 2.0
5.07k stars 425 forks source link

has_one and has_many association throw[ wrong number of arguments (given 1, expected 0)] when passed with proc shortcut #399

Open mayankkumarji opened 5 years ago

mayankkumarji commented 5 years ago

I had rails 4.2 and ruby 2.3.5 and I'm upgrading it to rails 5.2 with ruby 2.6.1. With older rails version(4.2) my UserAuthenticatedSerializer is working fine and was giving proper response. After upgrading to rails 5.2 my Serializer throwing argument error. I debugged it and it's throwing from relationship.rb file, method name fetch_id and fetch_associated_object Here in both methods, object_block.call(record, params) throwing me argument error. If I remove second parameter params it works fine but passing this two arguments causing me error. This same association work with rails 4.2 but it's not working in rails 5.2. Here is my code snap : response = UserAuthenticatedSerializer.new(@user, { params: { domain: current_domain } }).to_json error = ArgumentError (wrong number of arguments (given 1, expected 0))

user_authenticated_serializer.rb ===>

class UserAuthenticatedSerializer
  include FastJsonapi::ObjectSerializer
  has_one :user_profile, serializer: UserProfileSerializer, &:user_profile
  has_many :user_topic_label_order, &:user_topic_label_order
end

user.rb ====>

Relationship in user model:

has_one  :user_profile, dependent: :destroy
has_many :user_topic_label_order, dependent: :destroy

Rails version : 5.2.2 Ruby version : ruby 2.6.1 fast_jsonapi gem version : fast_jsonapi (1.5)

shishirmk commented 5 years ago

Hi @mayankkumarji,

Could you explain what are &:user_topic_label_order and &:user_profile params being passed for? I have a feeling those are causing issues.

mayankkumarji commented 5 years ago

Hi @shishirmk : Yeah, I have explained in title that this proc shortcuts(&:user_profile, &:user_topic_label_order) is causing me error, you can see both are the associate model with User model and relation is like has_one :user_profile, dependent: :destroy has_many :user_topic_label_order, dependent: :destroy

shishirmk commented 5 years ago

@mayankkumarji this is going to be difficult to debug without looking at the proc definitions and more details. Also you never changed the version of fast_jsonapi? Was it always 1.5?

mayankkumarji commented 5 years ago

@shishirmk : Okay, I need to provide you a working demo. Yes version of fast_jsonapi was always 1.5.

mayankkumarji commented 5 years ago

@shishirmk : Please find working demo here fast-json I have created.

Setup Instruction link and issue reproduce https://github.com/mayankkumarji/fast-json-demo/wiki/Setup-Instruction

shishirmk commented 5 years ago

I was able to reproduce the issue. I am looking into it. It might have been a change in behavior from active record. Why are you passing the proc? It looks like it works if you just don't pass the proc at all. If you dont have custom logic in this proc i don't understand the need to even pass it.

class UserAuthenticatedSerializer
  include FastJsonapi::ObjectSerializer
  attributes :name, :address

  has_one :user_profile
end