-
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
Allow users to globally opt out of automatic lookup #1295
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,10 @@ def serialization_scope | |
end | ||
|
||
def get_serializer(resource, options = {}) | ||
unless options[:serializer] || options[:each_serializer] || ActiveModel::Serializer.config.automatic_lookup | ||
return resource | ||
end | ||
|
||
if !use_adapter? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose you could also make an argument to bring back There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (or add |
||
warn 'ActionController::Serialization#use_adapter? has been removed. '\ | ||
"Please pass 'adapter: false' or see ActiveSupport::SerializableResource.new" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@trek (hi!)
actually, you can pass
adapter: false
in your render (oroptions[:adapter] = false
if you're outside a controller) and it won't look up a serializer or use an adapter.ref:
active_model_serializers/lib/active_model/serializable_resource.rb
Lines 54 to 58 in f3403c3
active_model_serializers/lib/action_controller/serialization.rb
Lines 23 to 40 in f3403c3
That should work for you, and would still be useful to include in this PR, to call that out :)
It should also be mentioned in https://github.com/rails-api/active_model_serializers/blob/master/docs/ARCHITECTURE.md, I think
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If that's not sufficient, that's the area of the public API that should be affected.. would love to see a code example of what you're trying to do
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, #1294 was merged, does that address this issue or should we revert it and continue here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's unclear if that PR addresses the issue here.
My feeling is that it may be a usage issue (the reason for this PR), but we need more information.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bf4 (hey!)
#1294 addresses 1 of the 2 problem cases. I think the root problem is that automatic lookup is too aggressive of a strategy for larger apps that want to introduce AMS only for new versions of an existing API. It'd be nice – and frankly safer – to opt out globally and opt-in locally in these cases.
Another riff on this PR is to make that an explicit flag on render calls:
But I already personally prefer supplying the specific serializer to the render call. Yes, it's repetitive, but removes the "where the heck is this object turning into this JSON?!?" problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right now AMS automatically includes the ActionController::Serialization mixin. What if you could opt out of that and mix it in to controllers as desired. Would that work? (As opposed to passing in
adapter: false
in most of your controllers)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bf4 are you think that could be an initialization configuration option? something like:
include_action_controller_serialization = true (by default)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@trek re:
In rails/rails#21496 I have proposed removing
use_renderers
since it appears never to have been used or tested. Since your suggestion here has a similar name, I was wondering if it might be related to theuse_renderers
method in the Rails Renderer, or just coincidental