Skip to content

Commit 22acb18

Browse files
committed
Ensure empty binaries are trimmed
1 parent 0d3c992 commit 22acb18

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

lib/ecto/type.ex

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,9 +1001,8 @@ defmodule Ecto.Type do
10011001
defp same_duration(_), do: :error
10021002

10031003
@doc false
1004-
def empty_trimmed?(value, type) do
1005-
is_binary(value) and type != :binary and String.trim_leading(value) == ""
1006-
end
1004+
def empty_trimmed?(value, :binary), do: value == ""
1005+
def empty_trimmed?(value, _type), do: is_binary(value) and String.trim_leading(value) == ""
10071006

10081007
## Adapter related
10091008

test/ecto/changeset_test.exs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,11 @@ defmodule Ecto.ChangesetTest do
222222
test "cast/4: with binary empty values" do
223223
# <<9>> is a control character which should not be empty_trimmed
224224
# for a binary field
225-
params = %{"color" => <<9>>}
226-
struct = %Post{}
225+
changeset = cast(%Post{}, %{"color" => <<9>>}, ~w(color)a)
226+
assert changeset.changes == %{color: <<9>>}
227227

228-
changeset = cast(struct, params, ~w(color)a)
229-
assert changeset.changes == %{"color": <<9>>}
228+
changeset = cast(%Post{}, %{"color" => <<>>}, ~w(color)a)
229+
assert changeset.changes == %{}
230230
end
231231

232232
test "cast/4: with force_changes" do

0 commit comments

Comments
 (0)