Skip to content

Commit f733239

Browse files
committed
nil check + test
1 parent 0b9ef9c commit f733239

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

parser/lib/jsonapi/parser.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module JSONAPI
2323
# objects in the primary data must have an id.
2424
# @return [JSONAPI::Parser::Document]
2525
def parse(document, options = {})
26+
raise Parser::InvalidDocument, 'document cannot be nil' if document.nil?
2627
hash = document.is_a?(Hash) ? document : JSON.parse(document)
2728

2829
Parser::Document.new(hash, options)

parser/spec/parser_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,10 @@
9494
expect(document.data.first.relationships.author.data.to_hash).to eq @author_data_hash
9595
expect(document.data.first.relationships.comments.data.map(&:to_hash)).to eq @comments_data_hash
9696
end
97+
98+
it 'raises InvalidDocument on nil input' do
99+
expect {
100+
JSONAPI.parse(nil)
101+
}.to raise_error JSONAPI::Parser::InvalidDocument
102+
end
97103
end

0 commit comments

Comments
 (0)