Skip to content

Commit 01e39c6

Browse files
committed
Fix deserialization
1 parent 158436b commit 01e39c6

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

lib/active_model_serializers/adapter/json_api/deserialization.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,28 +182,28 @@ def parse_relationship(assoc_name, assoc_data, options)
182182
prefix_key = field_key(assoc_name, options).to_s.singularize
183183
hash =
184184
if assoc_data.is_a?(Array)
185-
{ "#{prefix_key}_ids".to_sym => assoc_data.map { |ri| ri[:id] } }
185+
{ "#{prefix_key}_ids".to_sym => assoc_data.map { |ri| ri['id'] } }
186186
else
187-
{ "#{prefix_key}_id".to_sym => assoc_data && assoc_data.is_a?(Hash) ? assoc_data[:id] : nil }
187+
{ "#{prefix_key}_id".to_sym => assoc_data ? assoc_data['id'] : nil }
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+
hash["#{prefix_key}_type".to_sym] = assoc_data['type'] if polymorphic
192192

193193
hash
194194
end
195195

196196
# @api private
197197
def parse_relationships(relationships, options)
198198
transform_keys(relationships, options)
199-
.map { |(k, v)| parse_relationship(k, v[:data], options) }
199+
.map { |(k, v)| parse_relationship(k, v['data'], options) }
200200
.reduce({}, :merge)
201201
end
202202

203203
# @api private
204204
def transform_keys(hash, options)
205-
transform = options[:key_transform] || :underscore
206-
KeyTransform.send(transform, hash)
205+
transform = options[:transform] || :underscore
206+
Transform.send(transform, hash)
207207
end
208208
end
209209
end

lib/active_model_serializers/transform.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,19 @@ def dash(value)
4747
end
4848
end
4949

50-
# Transforms keys to underscore.
50+
# Transforms values to underscore_case.
5151
# This is the default case for deserialization in the JsonApi adapter.
5252
#
5353
# @example:
5454
# "some-key" => "some_key",
5555
# @see {https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb#L89-L98 ActiveSupport::Inflector.underscore}
56-
def underscore(hash)
57-
hash.deep_transform_keys! { |key| key.to_s.underscore.to_sym }
56+
def underscore(value)
57+
case value
58+
when Hash then value.deep_transform_keys! { |key| underscore(key) }
59+
when Symbol then underscore(value.to_s).to_sym
60+
when String then value.underscore
61+
else value
62+
end
5863
end
5964

6065
# Returns the value unaltered

0 commit comments

Comments
 (0)