Skip to content

Commit

Permalink
fix: emit warnings about already validated actions in before_action h…
Browse files Browse the repository at this point in the history
…ooks too
  • Loading branch information
zachdaniel committed Nov 1, 2024
1 parent b88569a commit f80612e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 15 deletions.
23 changes: 18 additions & 5 deletions lib/ash/changeset/changeset.ex
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,13 @@ defmodule Ash.Changeset do
quote do
changeset = unquote(changeset)

if changeset.__validated_for_action__ && !changeset.context[:private][:in_before_action?] do
IO.warn("""
{:current_stacktrace, stacktrace} =
Process.info(self(), :current_stacktrace)

if changeset.__validated_for_action__ do
require Logger

Logger.warning("""
Changeset has already been validated for action #{inspect(changeset.__validated_for_action__)}.
For safety, we prevent any changes after that point because they will bypass validations or other action logic.. To proceed anyway,
Expand All @@ -340,15 +345,22 @@ defmodule Ash.Changeset do
|> Ash.Changeset.new()
|> Ash.Changeset.#{unquote(function)}(...)
|> Ash.Changeset.for_create(...)
#{Exception.format_stacktrace(stacktrace)}
""")
end
end
else
quote do
changeset = unquote(changeset)

if changeset.__validated_for_action__ && !changeset.context[:private][:in_before_action?] do
IO.warn("""
{:current_stacktrace, stacktrace} =
Process.info(self(), :current_stacktrace)

if changeset.__validated_for_action__ do
require Logger

Logger.warning("""
Changeset has already been validated for action #{inspect(changeset.__validated_for_action__)}.
For safety, we prevent any changes using `#{unquote(function)}/#{unquote(arity)}` after that point because they will bypass validations or other action logic.
Expand All @@ -358,6 +370,8 @@ defmodule Ash.Changeset do
|> Ash.Changeset.new()
|> Ash.Changeset.#{unquote(function)}(...)
|> Ash.Changeset.for_create(...)
#{Exception.format_stacktrace(stacktrace)}
""")
end
end
Expand Down Expand Up @@ -3918,7 +3932,6 @@ defmodule Ash.Changeset do
defp run_around_actions(%{around_action: []} = changeset, func) do
changeset =
changeset
|> put_context(:private, %{in_before_action?: true})
|> set_phase(:before_action)

result =
Expand Down
12 changes: 9 additions & 3 deletions lib/ash/query/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,13 @@ defmodule Ash.Query do
quote do
query = unquote(query)

if !is_atom(query) && query.__validated_for_action__ &&
!query.context[:private][:in_before_action?] do
IO.warn("""
if !is_atom(query) && query.__validated_for_action__ do
{:current_stacktrace, stacktrace} =
Process.info(self(), :current_stacktrace)

require Logger

Logger.warning("""
Query has already been validated for action #{inspect(query.__validated_for_action__)}.
For safety, we prevent any changes after that point because they will bypass validations or other action logic.
Expand All @@ -248,6 +252,8 @@ defmodule Ash.Query do
|> Ash.Query.new()
|> Ash.Query.#{unquote(function)}(...)
|> Ash.Query.for_read(...)
#{Exception.format_stacktrace(stacktrace)}
""")
end
end
Expand Down
3 changes: 0 additions & 3 deletions lib/ash/type/keyword.ex
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,6 @@ defmodule Ash.Type.Keyword do
{:error, Enum.map(errors, fn error -> Keyword.put(error, :field, field) end)}
end

{:error, error} when is_binary(error) ->
{:error, [[message: error, field: field]]}

{:error, error} ->
{:error, [error]}

Expand Down
3 changes: 0 additions & 3 deletions lib/ash/type/map.ex
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,6 @@ defmodule Ash.Type.Map do
{:error, Enum.map(errors, fn error -> Keyword.put(error, :field, field) end)}
end

{:error, error} when is_binary(error) ->
{:error, [[message: error, field: field]]}

{:error, error} ->
{:error, [error]}

Expand Down
2 changes: 1 addition & 1 deletion test/actions/belongs_to_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defmodule Ash.Test.Actions.BelongsToTest do
false ->
changeset
|> Ash.Changeset.manage_relationship(:reviewer, nil, type: :append_and_remove)
|> Ash.Changeset.change_attribute(:review_by_date, nil)
|> Ash.Changeset.force_change_attribute(:review_by_date, nil)
end
end)
end
Expand Down

0 comments on commit f80612e

Please sign in to comment.