fix: False default value for argument is nil in changeset (#364)

This commit is contained in:
Darren Black 2022-08-23 00:41:25 +10:00 committed by GitHub
parent 3a69b3104e
commit 548247a8b7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 1 deletions

View file

@ -1400,7 +1400,11 @@ defmodule Ash.Changeset do
@doc "Gets the value of an argument provided to the changeset"
@spec get_argument(t, atom) :: term
def get_argument(changeset, argument) when is_atom(argument) do
Map.get(changeset.arguments, argument) || Map.get(changeset.arguments, to_string(argument))
if Map.has_key?(changeset.arguments, argument) do
Map.get(changeset.arguments, argument)
else
Map.get(changeset.arguments, to_string(argument))
end
end
@doc "fetches the value of an argument provided to the changeset or `:error`"

View file

@ -25,6 +25,16 @@ defmodule Ash.Test.Changeset.ChangesetTest do
allow_nil? false
end
argument :false_optional_argument, :boolean do
allow_nil? false
default false
end
argument :true_optional_argument, :boolean do
allow_nil? false
default true
end
validate confirm(:name, :confirm_name)
end
end
@ -855,6 +865,15 @@ defmodule Ash.Test.Changeset.ChangesetTest do
|> Api.create!(action: :create_with_confirmation)
end
end
test "optional arguments should use the default" do
changeset =
Category
|> Changeset.for_create(:create_with_confirmation)
assert Changeset.get_argument(changeset, :true_optional_argument) == true
assert Changeset.get_argument(changeset, :false_optional_argument) == false
end
end
describe "for_<action>" do