Skip to content

Commit

Permalink
Ensure valid jsonapi when blank relationship
Browse files Browse the repository at this point in the history
If you specify include_data false, and do not have any links for this
relationship, we would output something like:

`{ relationships: { comments: {} } }`

This is not valid jsonapi. We will now render

`{ relationships: { comments: { meta: {} } } }`

Instead.

Relevant jsonapi spec: http://jsonapi.org/format/#document-resource-object-relationships
  • Loading branch information
Lee Richmond committed Sep 19, 2016
1 parent a032201 commit 3a9d372
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Fixes:
- [#1887](https://github.com/rails-api/active_model_serializers/pull/1887) Make the comment reflect what the function does (@johnnymo87)
- [#1890](https://github.com/rails-api/active_model_serializers/issues/1890) Ensure generator inherits from ApplicationSerializer when available (@richmolj)
- [#1922](https://github.com/rails-api/active_model_serializers/pull/1922) Make railtie an optional dependency in runtime (@ggpasqualino)
- [#1930](https://github.com/rails-api/active_model_serializers/pull/1930) Ensure valid jsonapi when relationship has no links or data (@richmolj)

Features:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def as_json

meta = meta_for(association)
hash[:meta] = meta if meta
hash[:meta] = {} if hash.empty?

hash
end
Expand Down
2 changes: 1 addition & 1 deletion test/adapter/json_api/relationship_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def test_relationship_with_data_array
end

def test_relationship_data_not_included
test_relationship({}, options: { include_data: false })
test_relationship({ meta: {} }, options: { include_data: false })
end

def test_relationship_simple_link
Expand Down

0 comments on commit 3a9d372

Please sign in to comment.