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

Return null resource object identifier for blank id #2119

Merged
merged 1 commit into from
May 1, 2017

Conversation

bf4
Copy link
Member

@bf4 bf4 commented May 1, 2017

Fix regression from 6e41528 where relationships with null data shows up with :data=>{:id=>"", :type=>"locations"}

       -:data => {:id=>"123", :type=>"places", :attributes=>{:status=>"live"}, :relationships=>{:location=>{:data=>nil}}},
       +:data => {:id=>"123", :type=>"places", :attributes=>{:status=>"live"}, :relationships=>{:location=>{:data=>{:id=>"", :type=>"locations"}}}},

Also, fix test where attributes were included when id was ""

  1) Failure:
  ActionController::Serialization::AdapterSelectorTest#test_render_using_adapter_override
  [test/action_c$ntroller/adapter_selector_test.rb:53]:
  --- expected
  +++ actual
  @@ -1 +1 @@
  -"{\"data\":{\"id\":\"\",\"type\":\"profiles\",\"attributes\":{\"name\":\"Name 1\",\"description\":\"Description 1\"}}}"
  +"{\"data\":null}"

Also, fix test where attributes were included when id was ""

```
  1) Failure:
  ActionController::Serialization::AdapterSelectorTest#test_render_using_adapter_override
  [test/action_c$ntroller/adapter_selector_test.rb:53]:
  --- expected
  +++ actual
  @@ -1 +1 @@
  -"{\"data\":{\"id\":\"\",\"type\":\"profiles\",\"attributes\":{\"name\":\"Name 1\",\"description\":\"Description 1\"}}}"
  +"{\"data\":null}"
```
@mention-bot
Copy link

@bf4, thanks for your PR! By analyzing the history of the files in this pull request, we identified @benedikt, @domitian and @richmolj to be potential reviewers.

@bf4 bf4 merged commit 004e0dc into rails-api:master May 1, 2017
@bf4 bf4 deleted the exclude_empty_relationships branch May 1, 2017 04:45
@@ -295,20 +295,8 @@ def attributes_for(serializer, fields)

# {http://jsonapi.org/format/#document-resource-objects Document Resource Objects}
def resource_object_for(serializer, include_slice = {})
resource_object = serializer.fetch(self) do
Copy link
Member Author

Choose a reason for hiding this comment

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

refactored to data_for to decrease method size, per style

@@ -322,7 +310,10 @@ def resource_object_for(serializer, include_slice = {})
# prs:
# https://github.com/rails-api/active_model_serializers/pull/1247
# https://github.com/rails-api/active_model_serializers/pull/1018
resource_object[:links] = links if links.any?
if (links = links_for(serializer)).any?
resource_object ||= {}
Copy link
Member Author

Choose a reason for hiding this comment

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

resource_object may be nil

@InteNs
Copy link

InteNs commented May 12, 2017

what if i want to serialize an object via json_api adapter that doesn't have an ID?
I need this functionality for testing my PATCH requests.
Isn't it possible to just leave the ID part out of the serialized json, or is this against the spec?

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

Successfully merging this pull request may close these issues.

None yet

3 participants