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

Don't explode when parsing {"data": null} #191

Merged
merged 1 commit into from
Jul 4, 2016
Merged
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/json_api_client/parsers/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def handle_data(result_set, data)

# we will treat everything as an Array
results = [results] unless results.is_a?(Array)
resources = results.map do |res|
resources = results.compact.map do |res|
resource = result_set.record_class.load(parameters_from_resource(res))
resource.last_result_set = result_set
resource
Expand Down
3 changes: 2 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class TestResource < JsonApiClient::Resource

class Article < TestResource
has_many :comments
has_one :author
end

class Person < TestResource
Expand All @@ -32,4 +33,4 @@ class User < TestResource

class UserPreference < TestResource
self.primary_key = :user_id
end
end
33 changes: 33 additions & 0 deletions test/unit/parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,37 @@ def test_can_parse_single_record
assert_equal "Rails is Omakase", article.title
end

def test_can_parse_null_data
# Per http://jsonapi.org/format/#fetching-resources, it is sometimes
# appropriate for an API to respond with a 200 status and null data
# when the requested URL is one that might correspond to a single
# resource, but doesn’t currently

stub_request(:get, "http://example.com/articles/1")
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
data: {
type: "articles",
id: "1",
attributes: { title: "Rails is Omakase" },
relationships: {
author: {
links: {
related: "http://example.com/articles/1/author"
}
}
}
}
}.to_json)

stub_request(:get, "http://example.com/articles/1/author")
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
data: nil
}.to_json)

article = Article.find(1).first
author = article.author

assert_equal nil, author
end

end