Skip to content

Fix: Cannot access associations of 'dashed' type for a newly initialised resource. #372

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

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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## Unreleased
- [#372](https://github.com/JsonApiClient/json_api_client/pull/372) - Fix handling of dashed-types associations correctly

## 1.17.1
- [#370](https://github.com/JsonApiClient/json_api_client/pull/370) - bigdecimal 2 support
Expand Down
2 changes: 1 addition & 1 deletion lib/json_api_client/associations/base_association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def from_result_set(result_set)

def load_records(data)
data.map do |d|
record_class = Utils.compute_type(klass, d["type"].classify)
record_class = Utils.compute_type(klass, klass.key_formatter.unformat(d["type"]).classify)
record_class.load id: d["id"]
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/json_api_client/associations/has_one.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def from_result_set(result_set)
end

def load_records(data)
record_class = Utils.compute_type(klass, data["type"].classify)
record_class = Utils.compute_type(klass, klass.key_formatter.unformat(data["type"]).classify)
record_class.load id: data["id"]
end
end
Expand Down
52 changes: 52 additions & 0 deletions test/unit/association_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ def self.route_formatter
end
end

class DashedOwner < Formatted
end

class DashedProperty < Formatted
has_one :dashed_owner
end

class DashedRegion < Formatted
has_many :dashed_properties
end

class Account < TestResource
property :name
property :is_active, default: true
Expand Down Expand Up @@ -256,6 +267,26 @@ def test_has_one_loads_nil
assert_nil property.owner, "expected to be able to ask for explicitly declared association even if it's not present"
end

def test_load_has_one_with_dasherized_key_type
stub_request(:get, "http://example.com/dashed-owners/1")
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
data: [
{
id: 1,
type: 'dashed-owners',
attributes: {
name: "Arjuna"
}
}
],
}.to_json)
dashed_owner = DashedOwner.find(1).first
dashed_property = DashedProperty.new(dashed_owner: dashed_owner)

assert_equal(DashedOwner, dashed_property.dashed_owner.class)
assert_equal(1, dashed_property.dashed_owner.id)
end

def test_has_one_fetches_relationship
stub_request(:get, "http://example.com/properties/1")
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
Expand Down Expand Up @@ -554,6 +585,27 @@ def test_load_has_many_single_entry
assert_equal("123 Main St.", owner.properties.first.address)
end

def test_load_has_many_with_dasherized_key_type
stub_request(:get, "http://example.com/dashed-properties")
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
data: [
{
id: 1,
type: 'dashed-properties',
attributes: {
address: "78 Street No. 9, Ludhiana"
}
}
],
}.to_json)

dashed_properties = DashedProperty.all
dashed_region = DashedRegion.new(dashed_properties: dashed_properties)

assert_equal(1, dashed_region.dashed_properties.count)
assert_equal(DashedProperty, dashed_region.dashed_properties[0].class)
end

def test_respect_included_has_many_relationship_empty_data
stub_request(:get, "http://example.com/owners/1?include=properties")
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
Expand Down