Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't send dynamic map keys to the database #4327

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/ecto/query/builder/select.ex
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ defmodule Ecto.Query.Builder.Select do
{k, params_acc}
end

defp escape_key({:^, _, [k]}, params_acc, _vars, _env) do
checked = quote do: Ecto.Query.Builder.Select.map_key!(unquote(k))
{checked, params_acc}
end

defp escape_key(k, params_acc, vars, env) do
escape(k, params_acc, vars, env)
end
Expand Down Expand Up @@ -174,6 +179,20 @@ defmodule Ecto.Query.Builder.Select do
end
end

@doc """
Called at runtime to verify a map key
"""
def map_key!(key) when is_binary(key), do: key
def map_key!(key) when is_integer(key), do: key
def map_key!(key) when is_float(key), do: key
def map_key!(key) when is_atom(key), do: key

def map_key!(other) do
Builder.error(
"interpolated map keys in `:select` can only be atoms, strings or numbers, got: #{inspect(other)}"
)
end

# atom list sigils
defp take?({name, _, [_, modifiers]}) when name in ~w(sigil_w sigil_W)a do
?a in modifiers
Expand Down
31 changes: 30 additions & 1 deletion test/ecto/query/builder/select_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,16 @@ defmodule Ecto.Query.Builder.SelectTest do
%Ecto.Query{} |> select([], 1) |> select([], 2)
end
end

test "supports interpolated map keys" do
key = :test_key

q = from p in "posts", select: %{^key => 1}
assert {:%{}, [], [test_key: 1]} = q.select.expr

q = from p in "posts", select: %{^:test_key => 1}
assert {:%{}, [], [test_key: 1]} = q.select.expr
end
end

describe "select_merge" do
Expand Down Expand Up @@ -574,7 +584,7 @@ defmodule Ecto.Query.Builder.SelectTest do
end)

assert Macro.to_string(query.select.expr) ==
"merge(merge(%{comments: count(&2.id())}, %{^0 => &0.name()}), %{^1 => &1.author()})"
"%{comments: count(&2.id()), name: &0.name(), author: &1.author()}"
end

test "supports '...' in binding list with no prior select" do
Expand Down Expand Up @@ -672,5 +682,24 @@ defmodule Ecto.Query.Builder.SelectTest do
select_merge: %{t: p.title, b: p.body}
assert Macro.to_string(query.select.expr) =~ "merge"
end

test "supports interpolated map keys" do
shared_key = :shared
merge_key = :merge

q =
from p in "posts",
select: %{^shared_key => :old},
select_merge: %{^shared_key => :new, ^merge_key => :merge}

assert {:%{}, [], [shared: :new, merge: :merge]} = q.select.expr

q =
from p in "posts",
select: %{^:shared => :old},
select_merge: %{^:shared => :new, ^:merge => :merge}

assert {:%{}, [], [shared: :new, merge: :merge]} = q.select.expr
end
end
end