Skip to content

Commit 2d2e2e9

Browse files
committed
Warn for maps with atom keys
1 parent 61e5ea7 commit 2d2e2e9

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

lib/phoenix_html/form_data.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ defimpl Phoenix.HTML.FormData, for: [Plug.Conn, Atom, Map] do
7979

8080
Map ->
8181
defp name_params_and_opts(map, opts) do
82+
with {key, _, _} when is_atom(key) <- :maps.next(:maps.iterator(map)) do
83+
IO.warn(
84+
"a map with atom keys was given to a form. Maps are always considered " <>
85+
"parameters and therefore must have string keys, got: #{inspect(map)}"
86+
)
87+
end
88+
8289
case Keyword.pop(opts, :as) do
8390
{nil, opts} -> {nil, map, opts}
8491
{name, opts} -> {to_string(name), map, opts}

test/phoenix_html/form_test.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ defmodule Phoenix.HTML.FormTest do
112112
end
113113
end
114114

115+
test "warns on string keys" do
116+
assert ExUnit.CaptureIO.capture_io(:stderr, fn -> form(%{foo: 123}) end) =~
117+
"a map with atom keys was given to a form. Maps are always considered parameters and " <>
118+
"therefore must have string keys, got: %{foo: 123}"
119+
end
120+
115121
describe "input_value/2" do
116122
test "without form" do
117123
assert input_value(:search, :key) == nil

0 commit comments

Comments
 (0)