rails-api / active_model_serializers

ActiveModel::Serializer implementation and Rails hooks
MIT License
5.33k stars 1.39k forks source link

Deserialization gives an empty hash with JSON Api #1640

Closed GCorbel closed 8 years ago

GCorbel commented 8 years ago

Expected behavior vs actual behavior

With the current master branch, I tried to deserialize like in the doc with a simple copy/paste. Instead of

ActiveModelSerializers::Deserialization.jsonapi_parse(document)
#=>
# {
#   title: 'Title 1',
#   date: '2015-12-20',
#   author_id: 2,
#   second_author_id: nil
#   comment_ids: [3, 4]
# }

I have this :

ActiveModelSerializers::Deserialization.jsonapi_parse(document)
#=> { }

Steps to reproduce

(e.g., detailed walkthrough, runnable script, example application)

Copy and paste from the doc

Environment

Integrated application and version (e.g., Rails, Grape, etc):

Rails 5.0.0.beta2

GCorbel commented 8 years ago

The code stops at this line. payload['data'] gives nil but payload[:data] works.

Calling deep_stringify_keys works. It sill don't work for parameters because I can't apply this function, it's done when the action is triggered.

bf4 commented 8 years ago

I think this will be fixed by https://github.com/rails-api/active_model_serializers/pull/1645 cc @remear

GCorbel commented 8 years ago

I gave a try with the current code of this PR and its not resolved yet.

GCorbel commented 8 years ago

I tried to do a fork and I added document.deep_stringify_keys! on the parse method. Now, I have {:title=>"Title 1", :date=>"2015-12-20", :id=>1}.

I don't understand how parameters are parsed. When I do a request with this data {"data":{"attributes":{"allow_multiple_payment":false},"type":"settings"}}, I have this kind of params Parameters: {"fomat"=>:json, "setting"=>{}}.

bf4 commented 8 years ago

For what it's worth, we have labeled deserialization as experimental

On Sat, Apr 2, 2016 at 2:49 PM Guirec Corbel notifications@github.com wrote:

I tried to do a fork and I added document.deep_stringify_keys! on the parse method. Now, I have {:title=>"Title 1", :date=>"2015-12-20", :id=>1}.

I don't understand how parameters are parsed. When I do a request with this data {"data":{"attributes":{"allow_multiple_payment":false},"type":"settings"}}, I have this kind of params Parameters: {"fomat"=>:json, "setting"=>{}}.

— You are receiving this because you commented. Reply to this email directly or view it on GitHub https://github.com/rails-api/active_model_serializers/issues/1640#issuecomment-204789189

beauby commented 8 years ago

The jsonapi_parse method is intended for deserializing JSON hashes, for which the keys will always be strings.

beauby commented 8 years ago

@GCorbel I just fixed the doc according to my previous comment. Closing this, thank you for spotting it!