-
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
enable key_transform option outside of controller #1613
Comments
Thanks for the clear issue report. #1572 should fix this. Could you try it out with this branch maybe and let us know if it works for you? |
@groyoh I'm seeing the same results with the PR branch. In my case I'm setting the |
My bad. It seems like the Seems like a fix would be: diff --git a/lib/active_model_serializers/adapter/base.rb b/lib/active_model_serializers/adapter/base.rb
index cc6092d..6fe9121 100644
--- a/lib/active_model_serializers/adapter/base.rb
+++ b/lib/active_model_serializers/adapter/base.rb
@@ -64,13 +64,12 @@ module ActiveModelSerializers
# @param serialization_context [Object] the SerializationContext
# @return [Symbol] the transform to use
def key_transform(serialization_context)
- serialization_context.key_transform ||
+ serialization_context && serialization_context.key_transform ||
ActiveModelSerializers.config.key_transform ||
default_key_transform
end
def transform_key_casing!(value, serialization_context)
- return value unless serialization_context
transform = key_transform(serialization_context)
KeyTransform.send(transform, value)
end @jachenry would you like to provide a PR for fixing this? 😄 @remear what was the idea behind putting the option into the |
The goal was to allow specifying a key transform on a per-request basis. If an adapter option can achieve the same thing, then I have no specific objection to it. However, this is probably a better question for @bf4 as many older issues regarding key transformation mention that it should be implemented in the |
It would indeed achieve a per-request transformation. Actually, changing it to an adapter option wouldn't even change the interface when used within a controller e.g. |
@groyoh would you still like me to issue a PR or are we changing to an adapter option? |
Sounds good
sure, let it go straight to the adapter |
@remear I believe your PR solved this issue so I am going to close. Please let me know if there is a reason to reopen. |
ActiveModel::SerializableResource.new(resource).serializable_hash
Expected behavior vs actual behavior
Expected: keys should be transformed using ActiveModelSerializers.config.key_transform
Actual: keys are not transformed
Steps to reproduce
ActiveModel::SerializableResource.new(resource).serializable_hash
Environment
ActiveModelSerializers Version: 61412d8
Ruby version: ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin14]
OS Version: Mac OS X El Capitan (10.11.3)
Integrated application: Rails
The text was updated successfully, but these errors were encountered: