Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 8 additions & 6 deletions lib/ecto/adapters/postgres/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2020,12 +2020,10 @@ if Code.ensure_loaded?(Postgrex) do
end

defp escape_json(value) when is_binary(value) do
escaped =
value
|> escape_string()
|> :binary.replace("\"", "\\\"", [:global])

[?", escaped, ?"]
value
|> json_library().encode_to_iodata!()
|> IO.iodata_to_binary()
|> escape_string()
end

defp escape_json(value) when is_integer(value) do
Expand All @@ -2035,6 +2033,10 @@ if Code.ensure_loaded?(Postgrex) do
defp escape_json(true), do: ["true"]
defp escape_json(false), do: ["false"]

defp json_library do
Application.get_env(:postgrex, :json_library, Jason)
end

# To allow columns in json paths, we use the array[...] syntax
# which requires special handling for strings and column references.
# We still keep the escape_json/1 variant for strings because it is
Expand Down
16 changes: 16 additions & 0 deletions test/ecto/adapters/postgres_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,22 @@ defmodule Ecto.Adapters.PostgresTest do
query = Schema |> where([s], s.meta["id"] == "123") |> select(true) |> plan()
assert all(query) == ~s|SELECT TRUE FROM "schema" AS s0 WHERE ((s0."meta"@>'{"id": "123"}'))|

query = Schema |> where([s], s.meta["k"] == "a\\b") |> select(true) |> plan()
assert all(query) == ~S|SELECT TRUE FROM "schema" AS s0 WHERE ((s0."meta"@>'{"k": "a\\b"}'))|

query = Schema |> where([s], s.meta["k"] == "l1\nl2") |> select(true) |> plan()

assert all(query) ==
~S|SELECT TRUE FROM "schema" AS s0 WHERE ((s0."meta"@>'{"k": "l1\nl2"}'))|

query = Schema |> where([s], s.meta["k"] == "a\\q") |> select(true) |> plan()
assert all(query) == ~S|SELECT TRUE FROM "schema" AS s0 WHERE ((s0."meta"@>'{"k": "a\\q"}'))|

query = Schema |> where([s], s.meta["a\\b"] == "value") |> select(true) |> plan()

assert all(query) ==
~S|SELECT TRUE FROM "schema" AS s0 WHERE ((s0."meta"@>'{"a\\b": "value"}'))|

query = Schema |> where([s], s.meta["tags"][0]["name"] == "123") |> select(true) |> plan()

assert all(query) ==
Expand Down
Loading