-
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 Key Format to Be Overridden and Have Sensible Defaults #1029
Allow Key Format to Be Overridden and Have Sensible Defaults #1029
Conversation
I looked at the build and everything passed. No idea why it marked it as a failure. |
@@ -170,6 +170,35 @@ class PostSerializer < ActiveModel::Serializer | |||
end | |||
``` | |||
|
|||
### Overriding the Key Format |
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 would be awesome if along with this PR you could add some Docs to our new documentation we have being working on before release.
Maybe event add some article about integrating it with ember.js using our How to
section.
👍 to the PR itself, I made some comments after reviewing it and we also need to check the tests. |
@joaomdmoura I'll get on it and comment back here on Monday or Tuesday. Thanks! |
Just wanted to drop by and 👍 this, this looks like a nice addition. |
@joaomdmoura ok, I hit a bit of a snag. I found a bug. :-/ The problem is that we don't want to convert keys for hashes that are inside the values for attributes. Currently it just goes recursively as deep as it needs to to make sure that everything is converted. For example if we have this: @second_post = Post.new(
id: 2,
title: 'New Post',
body: 'Body',
multi_word_attribute: {
multiple_words: 'here are some words'
}) we don't want Maybe an abstraction is missing or a refactoring is required to make this the "easy" change? I don't have enough familiarity with the codebase so I thought you or someone else might be able to provide some insight. I think that the end goal is that the following should be converted, but nothing else:
Those should be converted wherever they are in the hash. Anything else:
should be left alone. And obviously this is going to vary from adapter to adapter (eg If you can point me in the right direction for implementation, I'll take a crack at it, but I fear I'm going to have to start from square one for the most part to get this right. Which is fine. I'd rather do it right. |
Get it @jfelchner, indeed the behavior you described would be most accurate. |
delegate :each, to: :@objects | ||
|
||
attr_reader :root, :meta, :meta_key | ||
attr_reader :root, :meta, :meta_key, :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.
It doesn't appear the you're using the options reader in any public contexts. Perhaps we can make it private? (It's just so the KeyFormat mixin can do options[:key_format]
, right?)
Hey @jfelchner, would be awesome if you could read through #1052. I merged it for now but I would like to handle it on this PR following the same approach as well. Let me know if there is anything I can do in order to make it easier for you 😄 |
@joaomdmoura perfect! I'll take a look. Just been busy the last few days. 😊 |
BTW @joaomdmoura I'm absolutely open to pairing on this. 👍🏻 |
Awesome I did just a few comments, because I know the implementation might change after some changes you will do.
Cool! Find on Twitter as @joaomdmoura, I will send you my email and we can try to schedule something using screenhero or whatever. But don't wait for me 😁 |
end | ||
|
||
def format_key(formattable_key) | ||
case key_format |
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.
Assume we have namespaced object Admin::User
then key should be user
instead of admin/user
. What do you think about adding something like formattable_key.to_s.demodulize
?
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.
@timurvafin Do you have a ref for Admin::User
-> user
? I'm not sure what I would do in such a case.
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.
@timurvafin underscore does that by default. |
@timurvafin oh I see what you're saying, you want the opposite. No I don't think that would be a good idea. They are two different classes and therefore two different types. If someone wants to do that they can override manually by overriding |
@jfelchner probably AMS by default should generate something more json-like for |
@timurvafin we're using AMS with an app with multiple Rails engines and are using |
might be worth creating an issue in https://github.com/json-api/json-api/ and linking here |
@bf4 👍 |
The JSON API adapter follows the JSON API spec and therefore should follow JSON API recommendations for naming * http://jsonapi.org/format/#document-member-names * http://jsonapi.org/recommendations/#naming
Just for completeness
|
||
For example, the JSON adapter defaults to `lower_camel` whereas the JSON API | ||
adapter defaults to `dasherize`. Any other adapter will default to `unaltered`. | ||
|
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 think the JSON API adapter should default to lower_camel
as well to avoid huge confusion on upgrade.
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.
@beauby I am very much in the other camp on that. The JSON API standard recommends dasherized keys and the couple JSON API JS libraries I've seen default to expecting incoming requests to be formatted with dasherized keys.
In the vein of making this Just Work with JSON API, I think it should absolutely be dasherized. Otherwise almost everyone who uses it will need to add the global config option to their projects.
I'll bow to the majority opinion, but I just wanted to voice my concern.
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're right, if JSON API recommends dashes, we should do dashes 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.
Yes, see #1029 (comment) and json-api/json-api#850
This will allow users to override the key format from the controller. Example: ```ruby class MyController < ActionController::Base def index render json: resource, key_format: :underscore end end ```
@@ -11,6 +13,7 @@ class Adapter | |||
autoload :Null | |||
autoload :FlattenJson | |||
autoload :CachedSerializer | |||
include Configuration |
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, in https://github.com/rails-api/active_model_serializers/pull/1050/files#diff-ae1e38e96a8bf28ae37918a65c448c73R11 I just use e.g. ActiveModel::Serializer.config.jsonapi_toplevel_meta = {}
the require could also be a problem since it's incompatible with rails autoload
We want to be able to allow the user to override the adapter defaults on a project-wide basis.
Rubocop fixes are self-explanatory. The changes to the tests mainly involve reformatting the hash to use hashrockets. These are required on Ruby 1.8 when there are keys that contain characters which cannot be symbolized. Example: ```ruby { foo: 'bar', 'baz-qux': 'foobar', } { :foo => 'bar', :'baz-qux' => 'foobar', } ```
Isolated test methods are going to be long and that's ok. Pushing for tests to be DRY makes individual tests less readable.
Related: #1213 |
Any updates on this? Am looking forward to dasherized format as well |
I'm also interested in seeing this make it through. Is there anything I can do to contribute? |
I came across this PR while looking for a way to specify |
No, this would be a feature request to pass a naked attributes hash directly into the adapter, i think B mobile phone
|
Any updates on this? |
Resolved by #1574 |
What is the final results now? This thing is being added to gem? |
Overriding the Key Format
You can specify that serializers use a different style for the format of the
keys that are sent over. This can be done at the config, class or instance
level.
Currently Supported Key Formats
:unaltered
:lower_camel
:underscore
:dasherize
The default key format will be set depending on the adapter that you specify.
For example, the JSON adapter defaults to
lower_camel
whereas the JSON APIadapter defaults to
dasherize
. Any other adapter will default tounaltered
.References: #534, emberjs/data#3455
Closes: #974
Merry Christmas in July everyone. 😀 🎄 🎅