-
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
[RFC] Automatic serializer lookup #1442
Comments
(cc @bf4, @joaomdmoura) |
Change to pr? |
Mention this is |
Mention 0.9 behavior w serializer_nanespace option to limit the cases ams had to handle. Could ve an option to serialization context |
I believe this is related to my ticket: #1337 I've had a use-case where I had In my case, |
@natematykiewicz It is indeed (and I haven't forgotted #1337, but thought I'd make an issue to broaden the scope a bit). |
I just edited the issue to add your suggestion. If this RFC gets a bit of traction, I'll turn it into a PR so that everyone can chip in easily. |
@beauby my app actually isn't Thanks for acknowledging #1337! |
@natematykiewicz I see. Note that in the scenario I described, it does not make a difference what your controller/action is called, since only its namespace is used. |
Oh, right. 👍🏻 |
I think the line mentioning "all of the calling controller's prefix namespaces" addresses my use-case in #1338. Just to bring that inline here: #1338 adds support for modifying the lookup chain for a serializer's relationships based on its own namespace. Concretely, given: class Public::V1::PostSerializer
belongs_to :author
end
class Public::V1::AuthorSerializer
end we will wind up with the following items in the lookup chain for an ['Public::V1::PostSerializer::AuthorSerializer',
'Public::V1::AuthorSerializer', 'Public::AuthorSerializer',
'::AuthorSerializer'] This preserves the functionality of the single-purpose serializers by leaving them highest in the chain but allows for sibling serializers that are not nested below the active serializer to also be utilized. |
@beauby correct me if I'm wrong but the proposal still seems to rely on the controller's namespace instead of the serializer's own namespace. Also it does not sound like sibling lookups are handled when serializing an association. The last part of your proposal says
and I would expect this instead:
My request/proposal also accounts for walking further up the namespaces. |
I'd very much appreciate it if we would make the serializer lookup configurable e.g.: module CustomLookup
module_function
def self.lookup(resource, options= {})
# some custom lookup
end
end
ActiveModelSerializers.config.automatic_lookup = CustomLookup As described at #879 (comment), in our code base we almost don't use Rails and our serializers have the following namespacing: module Serializer
module V1
class Comment < ActiveModel::Serializer; end
class Post < ActiveModel::Serializer
has_many :comments, serializer: Comment
end
end
module V2
class Comment < ActiveModel::Serializer; end
class Post < ActiveModel::Serializer
has_many :comments, serializer: Comment
end
end
end so the normal lookup doesn't work. This might completely solve the issue I described by simply creating our own custom lookup module: module CustomLookup
module_function
def self.lookup(resource, options= {})
namespace = options[:namespace]
"#{namespace}::#{resource.class}".classify
end
end and then simply specify the |
Nice, also see Trek's issue re implicit lookup disabling On Sun, Feb 28, 2016 at 8:31 AM Yohan Robert notifications@github.com
|
@groyoh I really like that idea. If @rails-api/ams is unable to justify implementing lookup logic by default, maybe if we provided some un-official examples in the docs of how to customize for common scenarios, that would be sufficient? |
@groyoh I like allowing passing in |
@bf4 that's also a good idea. Someone could just write a mixin that retrieved the namespace and added it to the render call. ez |
For |
I don't know what others think, but it makes sense to me that the lookup chain would include |
I'm running into the automatic lookup issue as well. I have a |
@bf4 is there any update on serializer lookup support? As things stand today on 0.10.2 I'm still in need of my monkey patched version of |
@NullVoxPopuli is that the final result/direction from this RFC? I notice that is not merged yet either so still unclear what the ultimate direction will be. |
I'm not sure. We've had a hard time deciding on a proper API for this. I'm in favor of one that's configured via the initializer (like in #1757). |
if you have any opinions, please share! :-) |
My own solution is rather simple: I have a method in the controller that drrives the record class and passes that in to render, so it's not a pressure I'm feeling. It's def on the todo list, just needs a simpl impl B mobile phone
|
I thought my implementation was pretty simple :-\ |
Ben that works for the top level but unless something else is wrong for me this fails to carry through to associations. The top level serializer gets the namespace fine but associations on that serializer don't respect the namespacing without my monkeypatch (which I submitted as a pr ages ago). ~ Brian McManus
|
@NullVoxPopuli i believe that works for my case as I just wound up monkeypatching
Pretty sure your implementation allows me to still do that but w/out monkeypatching. |
Any update on this? It would be great to namespace with Api::V1::CoolResourceSerializer and Api::V1::CoolResourceController and have it work via auto lookup. |
@rossholdway there is this: #1757 |
TL;DR
What should be the lookup mechanism when inferring a serializer for a resource?
Proposal
What seems to make most sense is the following:
Implemented in #1436.
Current situation
Work has been done to get us to a more flexible situation in this regard.
The whole automatic lookup can be disabled by setting
ActiveModelSerializers.config.automatic_lookup = false
from within an initializer.Currently, for a resource
CoolNamespace::CoolResource
, AMS will look for aCoolResourceSerializer
in the following namespaces:OtherNamespace::OtherResourceSerializer
, first look forOtherNamespace::OtherResourceSerializer::CoolResourceSerializer
(this is designed so that defining single-purpose serializers for associations is less cumbersome),CoolNamespace::CoolResourceSerializer
(this is kind of the "normal" lookup),CoolResource
.Other possible places we might want to look at:
render json: @posts
insideApi::V1::PostsController#index
, look forApi::V1::PostSerializer
,render json: @cool_resources
insideApi::V1::CoolResourcesController#index
, look forApi::V1::CoolNamespace::CoolResourceSerializer
(@natematykiewicz),::Api::V1
,::Api
, and::
,CoolResource
as an association throughOtherNamespace::OtherResourceSerializer
, look forOtherNamespace::CoolResourceSerializer
(so that when you dorender @post, serializer: Experimental::CoolFeature::PostSerializer
, the post's author can be inferred to beExperimental::CoolFeature::AuthorSerializer
),Possible API for user-provided paths
Please chip in with your ideas, use-cases and strong opinions!
The text was updated successfully, but these errors were encountered: