mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Support strings as attribute names
When generating the `_fast_attributes` method, attribute names that could not be represented as symbols (at least without escaping) would throw parsing errors.
This commit is contained in:
parent
ab5d40f38b
commit
a900d31041
@ -451,7 +451,7 @@ module ActiveModel
|
|||||||
method << " h = {}\n"
|
method << " h = {}\n"
|
||||||
|
|
||||||
_attributes.each do |name,key|
|
_attributes.each do |name,key|
|
||||||
method << " h[:#{key}] = read_attribute_for_serialization(:#{name}) if send #{INCLUDE_METHODS[name].inspect}\n"
|
method << " h[:\"#{key}\"] = read_attribute_for_serialization(:\"#{name}\") if send #{INCLUDE_METHODS[name].inspect}\n"
|
||||||
end
|
end
|
||||||
method << " h\nend"
|
method << " h\nend"
|
||||||
|
|
||||||
|
|||||||
@ -51,6 +51,17 @@ class SerializerTest < ActiveModel::TestCase
|
|||||||
}, hash)
|
}, hash)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_attributes_method_with_unsymbolizable_key
|
||||||
|
user = User.new
|
||||||
|
user_serializer = UserAttributesWithUnsymbolizableKeySerializer.new(user, :scope => {})
|
||||||
|
|
||||||
|
hash = user_serializer.as_json
|
||||||
|
|
||||||
|
assert_equal({
|
||||||
|
:user_attributes_with_unsymbolizable_key => { :first_name => "Jose", :"last-name" => "Valim", :ok => true }
|
||||||
|
}, hash)
|
||||||
|
end
|
||||||
|
|
||||||
def test_attribute_method_with_name_as_serializer_prefix
|
def test_attribute_method_with_name_as_serializer_prefix
|
||||||
object = SomeObject.new("something")
|
object = SomeObject.new("something")
|
||||||
object_serializer = SomeSerializer.new(object, {})
|
object_serializer = SomeSerializer.new(object, {})
|
||||||
|
|||||||
@ -70,6 +70,14 @@ class UserAttributesWithSomeKeySerializer < ActiveModel::Serializer
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class UserAttributesWithUnsymbolizableKeySerializer < ActiveModel::Serializer
|
||||||
|
attributes :first_name, :last_name => :"last-name"
|
||||||
|
|
||||||
|
def serializable_hash
|
||||||
|
attributes.merge(:ok => true).merge(options[:scope])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class DefaultUserSerializer < ActiveModel::Serializer
|
class DefaultUserSerializer < ActiveModel::Serializer
|
||||||
attributes :first_name, :last_name
|
attributes :first_name, :last_name
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user