Skip to content

Commit 1896e5a

Browse files
yevheneNullVoxPopuli
authored andcommitted
ActiveModelSerializers::Model successor initialized with string keys fix (#1881)
1 parent 5f3bdcc commit 1896e5a

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Features:
1212
Fixes:
1313

1414
- [#1833](https://github.com/rails-api/active_model_serializers/pull/1833) Remove relationship links if they are null (@groyoh)
15+
- [#1881](https://github.com/rails-api/active_model_serializers/pull/1881) ActiveModelSerializers::Model correctly works with string keys (@yevhene)
1516

1617
Misc:
1718

lib/active_model_serializers/model.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Model
99
attr_reader :attributes, :errors
1010

1111
def initialize(attributes = {})
12-
@attributes = attributes
12+
@attributes = attributes && attributes.symbolize_keys
1313
@errors = ActiveModel::Errors.new(self)
1414
super
1515
end

test/active_model_serializers/model_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,16 @@ class ModelTest < ActiveSupport::TestCase
77
def setup
88
@resource = ActiveModelSerializers::Model.new
99
end
10+
11+
def test_initialization_with_string_keys
12+
klass = Class.new(ActiveModelSerializers::Model) do
13+
attr_accessor :key
14+
end
15+
value = 'value'
16+
17+
model_instance = klass.new('key' => value)
18+
19+
assert_equal model_instance.read_attribute_for_serialization(:key), value
20+
end
1021
end
1122
end

0 commit comments

Comments
 (0)