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

Pull request #386 introduced an issue on serialization #390

Open
sebasjimenez10 opened this issue Sep 17, 2021 · 1 comment
Open

Pull request #386 introduced an issue on serialization #390

sebasjimenez10 opened this issue Sep 17, 2021 · 1 comment

Comments

@sebasjimenez10
Copy link
Contributor

sebasjimenez10 commented Sep 17, 2021

Looking at https://github.com/JsonApiClient/json_api_client/pull/386/files, I see that there's shift towards using HashWithIndiferentAccess, however, this change causes a wrong serialization when calling as_json_api. Which is causing a lot of tests to break on my end. Here's an example:

The previous symbolize_keys approach would serialize like this:

resource.as_json_api
{"type"=>"resources", "relationships"=>{"location"=>{"data"=>{"type"=>"locations", "id"=>"1"}}}, "attributes"=>{"arrival-time"=>"...", "full-name"=>"a full name", "email"=>nil, "user-data"=>[{"field"=>"color", "value"=>"blue"}], "private-notes"=>nil, "guests"=>nil, "phone"=>nil}}

but with the new with_indiferent_access approach it serializes like this:

resource.as_json_api
{"type"=>"resources", "relationships"=>{"location"=>{"data"=>{"type"=>"locations", "id"=>"1"}}}, "attributes"=>{"arrival-time"=>"...", "full-name"=>"a full name", "email"=>nil, "user-data"=>[{"field"=>"color", "value"=>"blue"}], "private-notes"=>nil, "additional-guests"=>nil, "phone"=>nil, "location"=>#<Models::Location:@attributes={"type"=>"locations", "id"=>"1", "address_line_one"=>nil, "address_line_two"=>nil}>}}

It is adding location as an attribute and set it equals to the value of to_s. which is derived from using with_indiferent_access.

These results I got from getting 1.20.0 and manually reverting the symbolize keys change.

Would really appreciate any guidance here, although it seems that the version 1.19.0 added some unexpected behavior.

Thanks in advance for any help/comments

@sebasjimenez10
Copy link
Contributor Author

Here's an update.

When you do:

SomeResource.new(some_relationship: instance_of_that_relationship)

internally, it takes the relationship as an attribute instead of as a relationship.

This alternative seems to fix the issue:

SomeResource.new.tap do |r|
  r.some_relationship = instance_of_that_relationship
end

it is definitely weird, that it would take the relationships passed in .new as attributes, but seems consistent with the .with_indifferent_access change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant