【发布时间】:2021-11-29 20:30:52
【问题描述】:
我在测试我的控制器发送参数时遇到了一些问题: 我正在发送以下地图:
params = %{
"filter" => %{
"type" => "all",
"conditions" => [
%{"column" => "price", "comparator" => "equals", "value" => "120", "type" => "number"}
]
}
}
在通话中:
conn = get conn, property_path(conn, :index, params)
但是在控制器中,当我检查接收到的参数时,我得到:
%{
"filter" => %{
"conditions" => [
%{"column" => "price"},
%{"comparator" => "equals"},
%{"type" => "number"},
%{"value" => "120"}
],
"type" => "all"
}
}
数组中的单个项目被转换为一个数组,每个键/值都是一个映射本身。有没有办法获得与控制器参数相同的地图?
【问题讨论】:
-
这可能是一个错误。这是 PHP 的
http_build_query为相同数据生成的查询 - 3v4l.org/Noi5v。 Phoenix 几乎正确地解析了这个(它返回一个键为“0”而不是列表的映射)。 -
Plug.Conn.Query.encode(%{a: [%{b: 2, c: 3}]}) == Plug.Conn.Query.encode(%{a: [%{b: 2}, %{c: 3}]}) #=> true -
有一个 pull request 显然可以解决这个问题:它不在当前数据的最新版本中,但应该在下一个:github.com/elixir-lang/plug/pull/52/files
-
这看起来像是 2 年前最初添加该功能的 PR。你的意思是链接到别的东西吗?
-
哦,该死的,只看到了日期和月份,而不是年份。大声笑