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

private method `read_attribute_for_serialization' called for #<Model #2457

Open
dorianmarie opened this issue Jan 3, 2024 · 4 comments
Open

Comments

@dorianmarie
Copy link

expected: no errors when serializing

actual: error when serializing

ModelSerializer.new(model).as_json(root: false)

active_model_serializers (0.10.14)

ruby 3.2.2 (2023-03-30 revision e51014f9c0) +YJIT [arm64-darwin22]

Darwin computer.dorianmarie.fr 23.2.0 Darwin Kernel Version 23.2.0: Wed Nov 15 21:53:18 PST 2023; root:xnu-10002.61.3~2/RELEASE_ARM64_T6000 arm64

Rails 7.1.2

     # /Users/dorianmariefr/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/activemodel-7.1.2/lib/active_model/attribute_methods.rb:486:in `method_missing'
     # /Users/dorianmariefr/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/pg_search-2.3.6/lib/pg_search/model.rb:42:in `method_missing'
     # /Users/dorianmariefr/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/active_model_serializers-0.10.14/lib/active_model/serializer.rb:398:in `read_attribute_for_serialization'
     # /Users/dorianmariefr/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/active_model_serializers-0.10.14/lib/active_model/serializer/field.rb:25:in `value'
     # /Users/dorianmariefr/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/active_model_serializers-0.10.14/lib/active_model/serializer.rb:341:in `block in attributes'
     # /Users/dorianmariefr/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/active_model_serializers-0.10.14/lib/active_model/serializer.rb:338:in `each'
     # /Users/dorianmariefr/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/active_model_serializers-0.10.14/lib/active_model/serializer.rb:338:in `each_with_object'
     # /Users/dorianmariefr/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/active_model_serializers-0.10.14/lib/active_model/serializer.rb:338:in `attributes'
     # /Users/dorianmariefr/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/active_model_serializers-0.10.14/lib/active_model/serializer.rb:409:in `attributes_hash'
     # /Users/dorianmariefr/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/active_model_serializers-0.10.14/lib/active_model/serializer.rb:372:in `serializable_hash'
     # /Users/dorianmariefr/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/active_model_serializers-0.10.14/lib/active_model/serializer.rb:381:in `as_json'
@dorianmarie
Copy link
Author

I have a patch but I can't reproduce it in the tests :(

diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb
index 5e347790..504b92be 100644
--- a/lib/active_model/serializer.rb
+++ b/lib/active_model/serializer.rb
@@ -395,7 +395,7 @@ module ActiveModel
       if respond_to?(attr)
         send(attr)
       else
-        object.read_attribute_for_serialization(attr)
+        object.send(:read_attribute_for_serialization, attr)
       end
     end

@dorianmarie
Copy link
Author

I verified it in my app and the patch fixes the issue

@OpakAlex
Copy link

+1

@bf4
Copy link
Member

bf4 commented Feb 29, 2024

Is this something which changed in Rails? Or is the model not valid per linter?

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

No branches or pull requests

3 participants