-
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
Bug in LookupChain::BY_NAMESPACE #2333
Comments
I think I've found an answer – when we open Not sure how to work around this yet. Perhaps instead of demodulizing, we should connect the namespaces of controller and model by ensuring there're no duplicates, like Also, there's a similar issue with the
Instead, it wants to find child |
do you think that explicitly passing the |
Indeed it will, but here I am talking about the current lookup chain implementation and its unexpected behavior. Currently, there's a problem with namespaces when using class ProfileSerializer
module Market
class AccountSerializer; end
end
module Public
class AccountSerializer; end
end
has_many :market_accounts
has_many :public_accounts
end None of these child serializers will be discovered by I ended up with the following configuration: ActiveModelSerializers::LookupChain::BY_PARENT_SERIALIZER_CUSTOM = lambda do |resource_class, serializer_class, _namespace|
return if serializer_class == ActiveModel::Serializer
"#{serializer_class}::#{resource_class.name}Serializer"
end
ActiveModelSerializers::LookupChain::BY_NAMESPACE_CUSTOM = lambda do |resource_class, _serializer_class, namespace|
if namespace
namespace_chain = namespace.name.split("::")
resource_name_chain = resource_class.name.split("::")
if resource_name_chain.size > 1 and namespace_chain.last == resource_name_chain.first
namespace_chain.pop
end
"#{(namespace_chain + resource_name_chain).join("::")}Serializer"
end
end
ActiveModelSerializers.config.serializer_lookup_chain = [
ActiveModelSerializers::LookupChain::BY_PARENT_SERIALIZER_CUSTOM,
ActiveModelSerializers::LookupChain::BY_NAMESPACE_CUSTOM,
ActiveModelSerializers::LookupChain::BY_RESOURCE_NAMESPACE,
ActiveModelSerializers::LookupChain::BY_RESOURCE
] Which works well with the namespaces for both models and controllers. Not sure if any action is required in the gem, just letting you know about the discovered issues. |
I have 2 models:
Profile
andPublic::Profile
and 2 serializers:The first
Profile
could have manyPublic::Profiles
so in the first serializer we havehas_many :public_profiles
.As result AMS tries to use the first serializer for both, while the expected behavior would be using a separate serializer for
Public::Profile
.Some debug information:
Absolutely unclear to me why
resource_class_name
demodulizes the name.The text was updated successfully, but these errors were encountered: