Ensure valid jsonapi when blank relationship (#1930)

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
This commit is contained in:
Lee Richmond 2016-09-19 14:10:50 -04:00 committed by L. Preston Sego III
parent a032201a91
commit a77dfdaa85
3 changed files with 3 additions and 1 deletions

View File

@ -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) - [#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) - [#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) - [#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: Features:

View File

@ -24,6 +24,7 @@ module ActiveModelSerializers
meta = meta_for(association) meta = meta_for(association)
hash[:meta] = meta if meta hash[:meta] = meta if meta
hash[:meta] = {} if hash.empty?
hash hash
end end

View File

@ -54,7 +54,7 @@ module ActiveModelSerializers
end end
def test_relationship_data_not_included def test_relationship_data_not_included
test_relationship({}, options: { include_data: false }) test_relationship({ meta: {} }, options: { include_data: false })
end end
def test_relationship_simple_link def test_relationship_simple_link