Skip to content

Commit ea8fa20

Browse files
authored
Merge pull request #372 from inderps/fix/initializing-resources-with-associations-of-dashed-types
Fix: Cannot access associations of 'dashed' type for a newly initialised resource.
2 parents bae9d1f + 1af57e2 commit ea8fa20

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Changelog
22

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

56
## 1.17.1
67
- [#370](https://github.com/JsonApiClient/json_api_client/pull/370) - bigdecimal 2 support

lib/json_api_client/associations/base_association.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def from_result_set(result_set)
2424

2525
def load_records(data)
2626
data.map do |d|
27-
record_class = Utils.compute_type(klass, d["type"].classify)
27+
record_class = Utils.compute_type(klass, klass.key_formatter.unformat(d["type"]).classify)
2828
record_class.load id: d["id"]
2929
end
3030
end

lib/json_api_client/associations/has_one.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def from_result_set(result_set)
77
end
88

99
def load_records(data)
10-
record_class = Utils.compute_type(klass, data["type"].classify)
10+
record_class = Utils.compute_type(klass, klass.key_formatter.unformat(data["type"]).classify)
1111
record_class.load id: data["id"]
1212
end
1313
end

test/unit/association_test.rb

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@ def self.route_formatter
6161
end
6262
end
6363

64+
class DashedOwner < Formatted
65+
end
66+
67+
class DashedProperty < Formatted
68+
has_one :dashed_owner
69+
end
70+
71+
class DashedRegion < Formatted
72+
has_many :dashed_properties
73+
end
74+
6475
class Account < TestResource
6576
property :name
6677
property :is_active, default: true
@@ -256,6 +267,26 @@ def test_has_one_loads_nil
256267
assert_nil property.owner, "expected to be able to ask for explicitly declared association even if it's not present"
257268
end
258269

270+
def test_load_has_one_with_dasherized_key_type
271+
stub_request(:get, "http://example.com/dashed-owners/1")
272+
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
273+
data: [
274+
{
275+
id: 1,
276+
type: 'dashed-owners',
277+
attributes: {
278+
name: "Arjuna"
279+
}
280+
}
281+
],
282+
}.to_json)
283+
dashed_owner = DashedOwner.find(1).first
284+
dashed_property = DashedProperty.new(dashed_owner: dashed_owner)
285+
286+
assert_equal(DashedOwner, dashed_property.dashed_owner.class)
287+
assert_equal(1, dashed_property.dashed_owner.id)
288+
end
289+
259290
def test_has_one_fetches_relationship
260291
stub_request(:get, "http://example.com/properties/1")
261292
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
@@ -554,6 +585,27 @@ def test_load_has_many_single_entry
554585
assert_equal("123 Main St.", owner.properties.first.address)
555586
end
556587

588+
def test_load_has_many_with_dasherized_key_type
589+
stub_request(:get, "http://example.com/dashed-properties")
590+
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
591+
data: [
592+
{
593+
id: 1,
594+
type: 'dashed-properties',
595+
attributes: {
596+
address: "78 Street No. 9, Ludhiana"
597+
}
598+
}
599+
],
600+
}.to_json)
601+
602+
dashed_properties = DashedProperty.all
603+
dashed_region = DashedRegion.new(dashed_properties: dashed_properties)
604+
605+
assert_equal(1, dashed_region.dashed_properties.count)
606+
assert_equal(DashedProperty, dashed_region.dashed_properties[0].class)
607+
end
608+
557609
def test_respect_included_has_many_relationship_empty_data
558610
stub_request(:get, "http://example.com/owners/1?include=properties")
559611
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {

0 commit comments

Comments
 (0)