Skip to content
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

Properly deserialize dasherized keys #1629

Merged

Conversation

lawitschka
Copy link
Contributor

Purpose

The JSON API adapater dasherizes every key, but the deserializer left the keys
unaltered. Thus, the client had to send underscored keys in the request body in
order for Rails to properly match sent values to model attributes.

The changes make overwriting keyForAttribute() and keyForRelationship() in
Ember.js apps obselete, because Active Model Serializer properly deserializes
the request.

Changes

This commit adds automatic key transformation on deserialization. Per default the
deserializer transforms the keys to underscore, but this behaviour can also be
changed by including key_transform in the deserializer options.

Caveats

I did not include tests for the KeyTransform#underscore, because the tests for
key transform only tested the transformation in controller context.

Related GitHub issues

#1612

The JSON API adapater dasherizes every key, but the deserializer left the keys
unaltered. Thus, the client had to send underscored keys in the request body in
order for Rails to properly match sent values to model attributes.

This commit adds automatic key transformation on deserialization. Per default the
deserializer transforms the keys to underscore, but this behaviour can also be
changed by including `key_transform` in the deserializer options.
@lawitschka lawitschka force-pushed the key-transform-on-deserialization branch from 1e9d4fc to afe786d Compare March 29, 2016 20:46
@@ -182,23 +182,29 @@ def parse_relationship(assoc_name, assoc_data, options)
prefix_key = field_key(assoc_name, options).to_s.singularize
hash =
if assoc_data.is_a?(Array)
{ "#{prefix_key}_ids".to_sym => assoc_data.map { |ri| ri['id'] } }
{ "#{prefix_key}_ids".to_sym => assoc_data.map { |ri| ri[:id] } }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because KeyTransform#underscore (and every other method in KeyTransform) transforms all keys to symbols, so string key access does not work here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just FYI, this is likely going to change very soon. I've been altering the transform stuff to be more flexible. Stay tuned for that.

@NullVoxPopuli
Copy link
Contributor

I'm good with this, anybody ele,? I'd like. To getiit merged

@remear
Copy link
Member

remear commented Mar 31, 2016

Seems good to me.

@remear remear merged commit b73b780 into rails-api:master Mar 31, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants