Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
3f1d04e3
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
3f1d04e3
编写于
9月 14, 2014
作者:
J
Jeremy Kemper
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix string/gid collision in job arguments
Serialize Global IDs as special objects, distinguishable from Strings
上级
391cfc20
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
38 addition
and
6 deletion
+38
-6
activejob/lib/active_job/arguments.rb
activejob/lib/active_job/arguments.rb
+27
-3
activejob/test/cases/argument_serialization_test.rb
activejob/test/cases/argument_serialization_test.rb
+11
-3
未找到文件。
activejob/lib/active_job/arguments.rb
浏览文件 @
3f1d04e3
...
...
@@ -35,12 +35,15 @@ def deserialize(arguments)
end
private
GLOBALID_KEY
=
'_aj_globalid'
.
freeze
private_constant
:GLOBALID_KEY
def
serialize_argument
(
argument
)
case
argument
when
*
TYPE_WHITELIST
argument
when
GlobalID
::
Identification
argument
.
to_global_id
.
to_s
{
GLOBALID_KEY
=>
argument
.
to_global_id
.
to_s
}
when
Array
argument
.
map
{
|
arg
|
serialize_argument
(
arg
)
}
when
Hash
...
...
@@ -61,16 +64,37 @@ def deserialize_argument(argument)
when
Array
argument
.
map
{
|
arg
|
deserialize_argument
(
arg
)
}
when
Hash
argument
.
each_with_object
({}.
with_indifferent_access
)
do
|
(
key
,
value
),
hash
|
hash
[
key
]
=
deserialize_argument
(
value
)
if
serialized_global_id?
(
argument
)
deserialize_global_id
argument
else
deserialize_hash
argument
end
else
raise
ArgumentError
,
"Can only deserialize primitive arguments:
#{
argument
.
inspect
}
"
end
end
def
serialized_global_id?
(
hash
)
hash
.
size
==
1
and
hash
.
include?
(
GLOBALID_KEY
)
end
def
deserialize_global_id
(
hash
)
GlobalID
::
Locator
.
locate
hash
[
GLOBALID_KEY
]
end
def
deserialize_hash
(
serialized_hash
)
serialized_hash
.
each_with_object
({}.
with_indifferent_access
)
do
|
(
key
,
value
),
hash
|
hash
[
key
]
=
deserialize_argument
(
value
)
end
end
RESERVED_KEYS
=
[
GLOBALID_KEY
,
GLOBALID_KEY
.
to_sym
]
private_constant
:RESERVED_KEYS
def
serialize_hash_key
(
key
)
case
key
when
*
RESERVED_KEYS
raise
SerializationError
.
new
(
"Can't serialize a Hash with reserved key
#{
key
.
inspect
}
"
)
when
String
,
Symbol
key
.
to_s
else
...
...
activejob/test/cases/argument_serialization_test.rb
浏览文件 @
3f1d04e3
...
...
@@ -31,12 +31,12 @@ class ArgumentSerializationTest < ActiveSupport::TestCase
end
test
'should convert records to Global IDs'
do
assert_arguments_roundtrip
[
@person
],
[
@person
.
to_gid
.
to_s
]
assert_arguments_roundtrip
[
@person
],
[
'_aj_globalid'
=>
@person
.
to_gid
.
to_s
]
end
test
'should dive deep into arrays and hashes'
do
assert_arguments_roundtrip
[
3
,
[
@person
]],
[
3
,
[
@person
.
to_gid
.
to_s
]]
assert_arguments_roundtrip
[{
'a'
=>
@person
}],
[{
'a'
=>
@person
.
to_gid
.
to_s
}.
with_indifferent_access
]
assert_arguments_roundtrip
[
3
,
[
@person
]],
[
3
,
[
'_aj_globalid'
=>
@person
.
to_gid
.
to_s
]]
assert_arguments_roundtrip
[{
'a'
=>
@person
}],
[{
'a'
=>
{
'_aj_globalid'
=>
@person
.
to_gid
.
to_s
}
}.
with_indifferent_access
]
end
test
'should stringify symbol hash keys'
do
...
...
@@ -51,6 +51,14 @@ class ArgumentSerializationTest < ActiveSupport::TestCase
assert_raises
ActiveJob
::
SerializationError
do
ActiveJob
::
Arguments
.
serialize
[
{
:a
=>
[{
2
=>
3
}]
}
]
end
assert_raises
ActiveJob
::
SerializationError
do
ActiveJob
::
Arguments
.
serialize
[
'_aj_globalid'
=>
1
]
end
assert_raises
ActiveJob
::
SerializationError
do
ActiveJob
::
Arguments
.
serialize
[
:_aj_globalid
=>
1
]
end
end
test
'should not allow non-primitive objects'
do
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录