-
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
Cannot control root option at serializer level anymore in 0.10.0rc5 #1683
Comments
Would you mind clarifying what you expect the root option to be doing and how you're trying to use it. Please review the issue template so you can help me help you. |
I think I should be able to add some additional detail. Expected behavior vs actual behaviorExpectedin a serializer, Actual
Steps to reproduceOn, 0.10.0.rc5, class MySerializer < ActiveModel::Serializer
self.config.adapter = :json
end Use it: serializer = MySerializer.new(profile)
adapter = ActiveModel::Serializer::Adapter.create(serializer)
adapter.as_json See that there is no EnvironmentRails 4.2.3
ActiveModelSerializers Version (commit ref if not on tag):
Output of
OS Type & Version:
Integrated application and version (e.g., Rails, Grape, etc):
Backtrace(e.g., provide any applicable backtraces from your application)
Additonal helpful informationThis is basically a request for discussion around setting the default adapter in the serializer. Right now this does not seem possible. I work with @bolthar and we are describing the same issue. |
Ah, got it. I honestly didn't even consider that was an API a serializer instance would use. I've actually been meaning to move it off the serializer class entirely as I thought of it as a misplaced global config How would you image it working with associations when it belongs to another serializer or has one or more? If no adapter option passed into SerializableResource then it has an affect only when it is a top level resource? B mobile phone
|
Yeah, in principle I agree with you. It is more of an adapter concern than a serializer concern.
I imagine it would work the same way |
So, if it helps, attributes adapter basically means no root, and json means yes root... Would you mind sharing a complete (pseudocode ok) example where a serializer would be the place to configure serialization default? I'm having a hard time imagining, partly out of separation of concerns and partly just as a simple option or method override B mobile phone
|
Totally. So I have a serializer running on 0.8.x like, class MySerializer < ActiveModel::Serializer
attributes :id, :name
self.root = true
end which outputs: and another, class MyOtherSerializer < ActiveModel::Serializer
attributes :id, :eye_color
self.root = false
end which outputs: After updating to 0.10.0.rc5, I am able to set the The only work around I could think of is calling To be clear, I think we already have a great interface for changing the adapter from the controller or at the instance level. A helpful addition would be a default at the class level.
Is that the detail you were looking for? |
You are correct that the current "right" way is via the |
Closing this for now, but let's keep talking about workarounds here. |
Sounds good. Thank you! Right now we are looking into how hard it will be to track down all the places serializers with I really appreciate your help. |
you mean in 0.10? or in your code? In rc1 I once listed a bunch #936 (comment) but they're not true anymore |
@HParker Any upgrade notes you can share back here as comments or a PR would be much appreciated! |
@bf4 I mean in our code. I am hoping we can find an easy way to move to the new standard while still feeling confident our external API is the same. |
In 0.8, it was possible to determine if the serializer should include a root element or not via the
root
option, like so:This has been removed in 0.10, and setting
self.config.adapter = :attributes
instead does nothing. Actually, it seems that the settings at the class level are ignored altogether.By taking a look at https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model/serializer/configuration.rb , one would expect that it should be possible to change the settings at the class level (there's even a comment that mentions it), but it doesn't seem to be possible. The options hash on classes deriving from
ActiveModel::Serializer
is always empty ({}
), while I would have expected to see the default values in it. If I do something like:then
MyTestClass.config
has the expected values in it.I can change the settings in an initializer, but that's global.
Would it be possible to change the root behavior at the class level again?
The text was updated successfully, but these errors were encountered: