-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Inheriting from ActiveModelSerializers::Model to support serialization of a PORO is a bad practice #1877
Comments
that's a good point. if you want to make a quick lil PR that extracts the body of AMS::Model into a module and update the docs for using it, that would be hugely appreciated. Maybe call it |
alright! |
Why AMS::Model is an executable documentation of the API. Why is inheritance a problem? |
Oh, you'd rather just be able to include a mixin. So, here's the issue: you might not want the precise implementation of AMS::Model. In fact, there's a bug in it. |
Also related |
@vasilakisfil Is there anything else you'd like to discuss here? FWIW, I use AMS::Model, but there is a known bug in it around the attributes hash and read attribute for serialization not returning the same thing, which is due to different implementation decisions in AM::Model and AM::Serializers::JSON.. I haven't resolved it yet |
No I think I am fine although I haven't seen the bug you are talking about. |
I rather agree with @vasilakisfil a mixin would be preferable. My understanding from this comment on #1873 is that the bug is: class Post < ActiveModelSerializers::Model
attr_accessor :title
end
# works
Post.new(title: 'my title').as_json # { title: 'my title' }
# does not work
post = Post.new
post.title = 'my title'
post.as_json # {} Quick guess on why this doesn't come up more often and maybe why @vasilakisfil hasn't seen it: Many of us use Virtus for POROs which has its own However, this seems a pretty simply fix. ActiveModelSerializers::Model gets its attributes from a @attributes attr_reader. This could be changed to something like: def read_attribute_for_serialization(key)
send(key) if respond_to?(key)
end @bf4 would you support something like this? |
@richmolj If you can solve this, you will be my hero! I've tried! In terms of being a mixin, we'd have to discuss what should be included and what not and what defaults. At this point, it might be best to first drop activemodel::model as a dependency and instead use the useful parts of it, get AMS::Model all fixed up, then move to a mixin. |
ref: #1903 |
@bf4 so I was able to add the additional accessor tests, get them all passing, and convert to a mixin. However, as I was finishing I thought a better solution would be to deprecate this class altogether and support serializing POROs and hashes with no extra work, subclass or mixin required. #1911. If I'm missing something the code to convert to a mixin is still mostly there. This was actually a lot trickier than I thought. One issue is changing to a mixin changes the load order. In the current code, we When converting to a mixin using the The fix for the accessor bug I believe was a combination of this and this. |
Expected behavior vs actual behavior
Expected:
Actual
What would happen if my SimpleObject was inheriting from another class?
Additonal helpful information
#1272
The text was updated successfully, but these errors were encountered: