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

attribute class method doesn't work without a block #13

Open
dgmora opened this issue Apr 21, 2023 · 2 comments
Open

attribute class method doesn't work without a block #13

dgmora opened this issue Apr 21, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@dgmora
Copy link

dgmora commented Apr 21, 2023

I've noticed that if you have the following:

class PostSerializer < Oj::Serializer
  attribute :title
end

You get an error:

       undefined local variable or method `title' for #<PostSerializer:...>
     # (eval):9:in `render_as_hash'
     # /Users/david/.local/share/rtx/installs/ruby/3.0.5/lib/ruby/gems/3.0.0/gems/oj_serializers-2.0.3/lib/oj_serializers/serializer.rb:209:in `one_as_hash'
     # /Users/david/.local/share/rtx/installs/ruby/3.0.5/lib/ruby/gems/3.0.0/gems/oj_serializers-2.0.3/lib/oj_serializers/serializer.rb:169:in `render_as_hash'

This works fine if you have a block or you use attributes:

# works
attribute :title do
 ...
end

# this works too
attributes :title
  • I have AMS compat on, if that makes any difference
@ElMassimo ElMassimo added the enhancement New feature or request label Apr 21, 2023
@ElMassimo
Copy link
Owner

Hi David!

Version 1 supported the following usage:

attribute def title
  ...
end

You should use attributes :title instead.


Keeping this open as a reminder, because we could potentially support this use case as well, by extending attribute to add a :method attribute instead of a :serializer attribute if a name is passed, and the method is not defined in the serializer.

@dgmora
Copy link
Author

dgmora commented Apr 21, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants