diff --git a/lib/jsonapi/parser/document.rb b/lib/jsonapi/parser/document.rb index 82c31cc..2e95db4 100644 --- a/lib/jsonapi/parser/document.rb +++ b/lib/jsonapi/parser/document.rb @@ -36,7 +36,7 @@ def self.parse!(document) # @api private def self.parse_data!(data) if data.is_a?(Hash) - parse_resource!(data) + parse_primary_resource!(data) elsif data.is_a?(Array) data.each { |res| parse_resource!(res) } elsif data.nil? diff --git a/spec/response_spec.rb b/spec/response_spec.rb index d36071f..92af914 100644 --- a/spec/response_spec.rb +++ b/spec/response_spec.rb @@ -72,7 +72,23 @@ expect { JSONAPI.parse_response!(payload) }.to_not raise_error end - it 'fails when an element is missing type or id' do + it 'fails when a top-level data array resource object is missing id' do + payload = { + 'data' => [ + { + 'type' => 'articles', + 'attributes' => {'title' => 'JSON API paints my bikeshed!'} + } + ] + } + + expect { JSONAPI.parse_response!(payload) }.to raise_error( + JSONAPI::Parser::InvalidDocument, + 'A resource object must have an id.' + ) + end + + it 'fails when a relationship object is missing id' do payload = { 'data' => [ { @@ -92,4 +108,28 @@ 'A resource identifier object MUST contain ["id", "type"] members.' ) end + + it 'fails when the top-level resource object has no type' do + payload = { + 'data' => { + 'id' => '1' + } + } + + expect { JSONAPI.parse_response!(payload) }.to raise_error( + JSONAPI::Parser::InvalidDocument, + 'A resource object must have a type.' + ) + end + + it 'passes when the top-level resource object has no id' do + payload = { + 'data' => { + 'type' => 'articles', + 'attributes' => {'title' => 'JSON API paints my bikeshed!'} + } + } + + expect { JSONAPI.parse_response!(payload) }.to_not raise_error + end end