Skip to content

Commit 4d7c245

Browse files
Dennis Stummbf4
Dennis Stumm
authored andcommitted
Fix deserialization of polymorphic relationships (#2200)
* Classify the relationship type for polymorphic associations
1 parent 715a702 commit 4d7c245

File tree

4 files changed

+4
-3
lines changed

4 files changed

+4
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Features:
1616
Fixes:
1717

1818
- [#2022](https://github.com/rails-api/active_model_serializers/pull/2022) Mutation of ActiveModelSerializers::Model now changes the attributes. Originally in [#1984](https://github.com/rails-api/active_model_serializers/pull/1984). (@bf4)
19+
- [#2200](https://github.com/rails-api/active_model_serializers/pull/2200) Fix deserialization of polymorphic relationships. (@dennis95stumm)
1920

2021
Misc:
2122

lib/active_model_serializers/adapter/json_api/deserialization.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def parse_relationship(assoc_name, assoc_data, options)
189189

190190
polymorphic = (options[:polymorphic] || []).include?(assoc_name.to_sym)
191191
if polymorphic
192-
hash["#{prefix_key}_type".to_sym] = assoc_data.present? ? assoc_data['type'] : nil
192+
hash["#{prefix_key}_type".to_sym] = assoc_data.present? ? assoc_data['type'].classify : nil
193193
end
194194

195195
hash

test/action_controller/json_api/deserialization_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def test_deserialization_of_relationship_only_object
4545
response = JSON.parse(@response.body)
4646
expected = {
4747
'restriction_for_id' => '67',
48-
'restriction_for_type' => 'discounts',
48+
'restriction_for_type' => 'Discount',
4949
'restricted_to_id' => nil,
5050
'restricted_to_type' => nil
5151
}

test/adapter/json_api/parse_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def test_polymorphic
125125
src: 'http://example.com/images/productivity.png',
126126
author_id: nil,
127127
photographer_id: '9',
128-
photographer_type: 'people',
128+
photographer_type: 'Person',
129129
comment_ids: %w(1 2)
130130
}
131131
assert_equal(expected, parsed_hash)

0 commit comments

Comments
 (0)