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

fix top-level resource optional id & add test cases for type/id requirements #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/jsonapi/parser/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
42 changes: 41 additions & 1 deletion spec/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
{
Expand All @@ -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