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

Fix ActiveJob::DeserializationError #7

Merged
merged 1 commit into from
Oct 31, 2023

Conversation

natematykiewicz
Copy link
Contributor

@natematykiewicz natematykiewicz commented Oct 31, 2023

"Error while trying to deserialize arguments: undefined method `publisher' for "id":String (ActiveJob::DeserializationError)"

GlobalId::Locator.locate calls primary_key on the model class.
https://github.com/rails/globalid/blob/488ab6c67d17219ba3b23fc5ade434a1faaa915f/lib/global_id/locator.rb#L204

In this case, the model_class is an ActiveRecord::AssociatedObject. ActiveRecord::AssociatedObject does not delegate primary_key to record_class, and therefore the method_missing gets called.

if !record_klass.respond_to?(method) then super else
record_klass.public_send(method, ...).then do |value|
value.respond_to?(:each) ? value.map(&attribute_name) : value&.public_send(attribute_name)
end
end

This method_missing code is then essentially doing: Post.primary_key.publisher, which is "id".publisher

This, of course, errors. By delegating the primary_key method, this is now defined, returns "id", and deserializes just fine.

Note: I had to bundle update nokogiri, because I kept getting:

Fetching gem metadata from https://rubygems.org/...........
Your bundle is locked to nokogiri (1.13.4-x86_64-linux) from rubygems repository https://rubygems.org/ or installed locally, but that version can no longer be found in that source. That means the author of nokogiri (1.13.4-x86_64-linux) has removed it.
You'll need to update your bundle to a version other than nokogiri (1.13.4-x86_64-linux) that hasn't been removed in order to install.

"Error while trying to deserialize arguments: undefined method
`publisher' for "id":String (ActiveJob::DeserializationError)"

GlobalId::Locator.locate calls `primary_key` on the model class.
https://github.com/rails/globalid/blob/v1.2.1/lib/global_id/locator.rb#L204

In this case, the model_class is an ActiveRecord::AssociatedObject.
ActiveRecord::AssociatedObject does not delegate `primary_key` to
record_class, and therefore the method_missing gets called.

https://github.com/kaspth/active_record-associated_object/blob/67f509dbd58bd30fcce6f83503d46cbabf849d08/lib/active_record/associated_object.rb#L22-L26

This method_missing code is then essentially doing:
`Post.primary_key.publisher`, which is `"id".publisher`

This, of course, errors. By delegating the `primary_key` method,
this is now defined, returns "id", and deserializes just fine.
@kaspth
Copy link
Owner

kaspth commented Oct 31, 2023

Ah, looks like that changed in late August in GlobalID. Thank you for the fix!

This also reminds me that I should check AssociatedObjects support for composite primary keys.

@kaspth kaspth merged commit eb4b471 into kaspth:main Oct 31, 2023
2 checks passed
@natematykiewicz natematykiewicz deleted the fix_deserialization_error branch October 31, 2023 20:36
@natematykiewicz
Copy link
Contributor Author

Yeah, I was seeing that. I had a hard time pinpointing exactly what changed recently, but I figured that they changed something to help support composite primary keys better.

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