-
Notifications
You must be signed in to change notification settings - Fork 326
Add key_transform :dash for JSON API
#445
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
Conversation
|
Oups, some tests are failing with older versions of ruby, I'll rewrite the code to avoid using |
|
Thanks for this @vinc - can we deprecate but not remove :underscore ? |
|
See this sorta thing being useful in a variety of ways 😎 As you mentioned apis may send json properties as |
|
@edtjones by keeping but deprecating @zacharywelch I can indeed see this code as a separate middleware, it'd keep the main code lean, especially if I add all the transformations and make it recursive for nested attributes! But would it be possible to use the same middleware for doing the inverse operation when we serialize the data to be transmitted to the API? |
|
@vinc yeah - that's what I meant. That's fine. I only skimmed the PR on a mobile so I didn't clock that you were introducing an optional method call :-) Re middleware - that would be the best way, I agree with @zacharywelch - and you can do transformations in both directions. Various examples here https://github.com/lostisland/faraday_middleware |
|
(@vinc just to say I won't merge this for now, pending refactoring into a middleware - hope ok) |
|
Hi @edtjones and @zacharywelch, thanks for your suggestions! I'll look into writting a middleware to handle this. |
|
I'm going to close this pull request since you'll be creating a separate middleware. Good luck and feel free to provide a link here once it's complete 😺 Thanks! |
In ruby we use underscore for symbols, but dash are prefered for keys with JSON API.
By default, Active Model Serializers transforms underscore into dash when JSON API is used, otherwise it leaves the keys unaltered: https://github.com/rails-api/active_model_serializers/blob/v0.10.6/docs/general/key_transforms.md
This PR is a request for comments regarding the transformation of keys during serialization and deserialization.
If we have a JSON API exposing a
userresource with afirst-nameattribute, this allow us to access it byuser.first_namein Rails if we addkey_transform :dashto the model.So far this PR is quite restricted:
:dashoptionkey_transformcould also be applied to regular APIs, otherwise it should maybe be renommedjson_api_key_transform?