Properly deserialize dasherized keys

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.
This commit is contained in:
Moritz Lawitschka
2016-03-29 22:03:28 +02:00
parent c7b2916f37
commit afe786d19a
3 changed files with 37 additions and 8 deletions

View File

@@ -20,7 +20,10 @@ module ActionController
'id' => 'zorglub',
'attributes' => {
'title' => 'Ember Hamster',
'src' => 'http://example.com/images/productivity.png'
'src' => 'http://example.com/images/productivity.png',
'image-width' => '200',
'imageHeight' => '200',
'ImageSize' => '1024'
},
'relationships' => {
'author' => {
@@ -34,6 +37,12 @@ module ActionController
{ 'type' => 'comments', 'id' => '1' },
{ 'type' => 'comments', 'id' => '2' }
]
},
'related-images' => {
'data' => [
{ 'type' => 'image', 'id' => '7' },
{ 'type' => 'image', 'id' => '8' }
]
}
}
}
@@ -46,9 +55,13 @@ module ActionController
'id' => 'zorglub',
'title' => 'Ember Hamster',
'src' => 'http://example.com/images/productivity.png',
'image_width' => '200',
'image_height' => '200',
'image_size' => '1024',
'author_id' => nil,
'photographer_id' => '9',
'comment_ids' => %w(1 2)
'comment_ids' => %w(1 2),
'related_image_ids' => %w(7 8)
}
assert_equal(expected, response)