Skip to content

Commit

Permalink
fix: don't show a type error on nil for lists
Browse files Browse the repository at this point in the history
closes #1166
  • Loading branch information
zachdaniel committed May 14, 2024
1 parent 449da54 commit 3263ec2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/ash/query/operator/operator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,11 @@ defmodule Ash.Query.Operator do
end)
|> case do
nil ->
{:error, "Could not cast expression: #{inspect(mod)} #{inspect(left)} #{inspect(right)}"}
{:error,
Ash.Error.Query.InvalidFilterValue.exception(
value: struct(mod, left: left, right: right),
message: "No matching types. Possible types: #{inspect(mod.types())}"
)}

{:error, error} ->
{:error, error}
Expand Down
6 changes: 5 additions & 1 deletion lib/ash/query/type.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ defmodule Ash.Query.Type do
{:ok, val} -> {:ok, Enum.reverse(val)}
end
else
:error
if is_nil(list) do
{:ok, list}
else
:error
end
end
end

Expand Down
19 changes: 19 additions & 0 deletions test/filter/filter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ defmodule Ash.Test.Filter.FilterTest do
actions do
default_accept :*
defaults [:read, :destroy, create: :*, update: :*]

read :with_invalid_value_behind_is_nil_check do
argument :ids, {:array, :uuid}

filter expr(
if not is_nil(^arg(:ids)) do

Check warning on line 79 in test/filter/filter_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (Picosat) / mix credo --strict

Avoid negated conditions in if-else blocks.

Check warning on line 79 in test/filter/filter_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (SimpleSat) / mix credo --strict

Avoid negated conditions in if-else blocks.
id in ^arg(:ids)
else
true
end
)
end
end

attributes do
Expand Down Expand Up @@ -861,6 +873,13 @@ defmodule Ash.Test.Filter.FilterTest do
end
end

test "errors are not produced by eager evaluation" do
assert [] =
User
|> Ash.Query.for_read(:with_invalid_value_behind_is_nil_check, %{})
|> Ash.read!()
end

describe "calls in filters" do
test "calls are evaluated and can be used in predicates" do
post1 =
Expand Down

0 comments on commit 3263ec2

Please sign in to comment.