Skip to content

Commit 5be33af

Browse files
committed
Fix deserialization of nil relationships
failing test use try for when the assoc_data is possibly nil rubocop test/action_controller/json_api/deserialization_test.rb -a attempt to work on rails-master account for rails/master having instead of nil for assoc_data added changelog
1 parent 96c5516 commit 5be33af

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Features:
3434
- [#1340](https://github.com/rails-api/active_model_serializers/pull/1340) Add support for resource-level meta. (@beauby)
3535

3636
Fixes:
37+
- [#1651](https://github.com/rails-api/active_model_serializers/pull/1651) Fix deserialization of nil relationships. (@NullVoxPopuli)
3738
- [#1480](https://github.com/rails-api/active_model_serializers/pull/1480) Fix setting of cache_store from Rails configuration. (@bf4)
3839
Fix uninentional mutating of value in memory cache store. (@groyoh)
3940
- [#1622](https://github.com/rails-api/active_model_serializers/pull/1622) Fragment cache changed from per-record to per-serializer.

lib/active_model_serializers/adapter/json_api/deserialization.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,9 @@ def parse_relationship(assoc_name, assoc_data, options)
188188
end
189189

190190
polymorphic = (options[:polymorphic] || []).include?(assoc_name.to_sym)
191-
hash["#{prefix_key}_type".to_sym] = assoc_data[:type] if polymorphic
191+
if polymorphic
192+
hash["#{prefix_key}_type".to_sym] = assoc_data.present? ? assoc_data[:type] : nil
193+
end
192194

193195
hash
194196
end

test/action_controller/json_api/deserialization_test.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,50 @@ def render_parsed_payload
99
parsed_hash = ActiveModelSerializers::Deserialization.jsonapi_parse(params)
1010
render json: parsed_hash
1111
end
12+
13+
def render_polymorphic_parsed_payload
14+
parsed_hash = ActiveModelSerializers::Deserialization.jsonapi_parse(
15+
params,
16+
polymorphic: [:restriction_for, :restricted_to]
17+
)
18+
render json: parsed_hash
19+
end
1220
end
1321

1422
tests DeserializationTestController
1523

24+
def test_deserialization_of_relationship_only_object
25+
hash = {
26+
'data' => {
27+
'type' => 'restraints',
28+
'relationships' => {
29+
'restriction_for' => {
30+
'data' => {
31+
'type' => 'discounts',
32+
'id' => '67'
33+
}
34+
},
35+
'restricted_to' => {
36+
'data' => nil
37+
}
38+
}
39+
},
40+
'restraint' => {}
41+
}
42+
43+
post :render_polymorphic_parsed_payload, params: hash
44+
45+
response = JSON.parse(@response.body)
46+
expected = {
47+
'restriction_for_id' => '67',
48+
'restriction_for_type' => 'discounts',
49+
'restricted_to_id' => nil,
50+
'restricted_to_type' => nil
51+
}
52+
53+
assert_equal(expected, response)
54+
end
55+
1656
def test_deserialization
1757
hash = {
1858
'data' => {

0 commit comments

Comments
 (0)