Skip to content

Commit 32cbeba

Browse files
authored
Escape backslashes in MySQL JSON path keys (#759)
1 parent 254792d commit 32cbeba

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

‎lib/ecto/adapters/myxql/connection.ex‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1629,8 +1629,9 @@ if Code.ensure_loaded?(MyXQL) do
16291629

16301630
defp escape_json_key(value) when is_binary(value) do
16311631
value
1632+
|> :binary.replace("\\", "\\\\", [:global])
1633+
|> :binary.replace("\"", "\\\"", [:global])
16321634
|> escape_string()
1633-
|> :binary.replace("\"", "\\\\\"", [:global])
16341635
end
16351636

16361637
defp ecto_cast_to_db(:id, _query), do: "unsigned"

‎test/ecto/adapters/myxql_test.exs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,12 @@ defmodule Ecto.Adapters.MyXQLTest do
824824

825825
query = Schema |> select([s], json_extract_path(s.meta, ["\"a"])) |> plan()
826826
assert all(query) == ~s{SELECT json_extract(s0.`meta`, '$."\\\\"a"') FROM `schema` AS s0}
827+
828+
query = Schema |> select([s], json_extract_path(s.meta, ["a\\b"])) |> plan()
829+
assert all(query) == ~S{SELECT json_extract(s0.`meta`, '$."a\\\\b"') FROM `schema` AS s0}
830+
831+
query = Schema |> select([s], json_extract_path(s.meta, ["a\\"])) |> plan()
832+
assert all(query) == ~S{SELECT json_extract(s0.`meta`, '$."a\\\\"') FROM `schema` AS s0}
827833
end
828834

829835
test "nested expressions" do

0 commit comments

Comments
 (0)