-
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
Transform keys referenced in values in serialized documents #1645
Conversation
:unaltered | ||
end | ||
class << self | ||
def default_transform |
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.
should there be docs on this method?
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.
More docs than mentioning that there are default transforms specified for each adapter? https://github.com/remear/active_model_serializers/blob/transforms/docs/general/key_transform.md#adapter-default
Which reminds me, I need to rename that md file.
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.
I just mean something real quick for devs browsing the code, in case they haven't discovered that we do key transforms yet
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.
Yes, there should be some docs in the code.
@remear this might be a good one to benchmark. esp compare to other libs that do the same thing e.g. https://github.com/cerebris/jsonapi-resources/blob/d2a9d0f40105e246b0fcffd9e1cf1d3b634053f1/lib/jsonapi/formatter.rb#L64-L103 |
@bf4, is there an easy way to run your benchmarker? like...
or something |
Each adapter has a default transform configured: | ||
|
||
- `Json` - `:unaltered` | ||
- `JsonApi` - `:dash` |
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.
you can use a table here and above for better layout. see https://github.com/rails-api/active_model_serializers/tree/master/docs#integrations
# @return [Symbol] the transform to use | ||
def transform(options) | ||
return options[:transform] if options && options[:transform] | ||
ActiveModelSerializers.config.transform || default_transform |
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.
Let's raise a deprecation notice here.
|
@@ -8,7 +8,7 @@ class PostController < ActionController::Base | |||
else | |||
comments = [Comment.new(id: 1, body: 'ZOMG A COMMENT')] | |||
end | |||
author = Author.new(id: 42, name: 'Joao Moura.') | |||
author = Author.new(id: 42, first_name: 'Joao', last_name: 'Moura') |
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.
lulz
9583963
to
becf31a
Compare
spec.post_install_message = <<-EOF | ||
NOTE: The default key case for the JsonApi adapter has changed to dashed. | ||
See https://github.com/rails-api/active_model_serializers/blob/master/docs/general/key_transform.md | ||
for more information on configuring this behavior. |
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.
This message will remain until the next rc5 or 0.10.0 whicher comes first
?
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.
Mehhhh, the post_install_message
is not a good way to issue a deprecation I think. I mean, there is even a gem for ignoring post install messages that is pretty popular.... https://github.com/tpope/gem-shut-the-fuck-up
Why not just issue a real deprecation warning?
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.
@ianks as our deprecation champion for this change, how would you recommend we do this? it's a changed default, not method, so we can't reroute.. right? or you think the change you be reverted for now?
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.
Champion, eh?! I guess I have a vested interest now 😆
The only time that this issue really affects people is when the options is nil, where it falls back to the default for the adapter. I would recommend that instead of accepting nil
silently, we issue a deprecation notice when the option is not set.
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.
Basically, issue a deprecation in the default_key_transform
method.
@@ -4,7 +4,7 @@ class Json < Base | |||
def serializable_hash(options = nil) | |||
options ||= {} | |||
serialized_hash = { root => Attributes.new(serializer, instance_options).serializable_hash(options) } | |||
transform_key_casing!(serialized_hash, options[:serialization_context]) | |||
self.class.transform_key_casing!(serialized_hash, options) |
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.
how come this is a class method on the adapter?
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.
(philosophical/stategic/curious)
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.
To facilitate things like https://github.com/rails-api/active_model_serializers/pull/1645/files#diff-65abd6a4fd7fd55fe487ff138e0841dbR8. A class method here seemed like the best option at the time. I figured if there's a better way we could discuss refactoring and handle that in another PR instead of increasing the scope of this one.
c2c04e5
to
039d08c
Compare
Purpose
Apply key transforms to keys referenced in values in serialized documents.
Changes
dashed
key transform todash
Related GitHub issues
#1613
#1625
#1574
Additional helpful information
Some helpful tests were provided by master...rthbound:bug-in-1574
Benchmark report