Ash FrameworkAF
Ash Framework4mo ago
12 replies
Herman

Problem with Ash.Type.dump_to_native/2 after upgrade to 3.5.41

After upgrading to 3.5.41 my tests fail when there is a "NOT IN" query, apparently where before dump_to_native cas called with the atom (:cancelled) now a string "cancelled" is used causing the postgres query to fail.

This is my custom type:
defmodule Admin.Resources.Types.TimerStatus do
  @moduledoc """
  Custom Ash type for timer status enum that maps atoms to integers
  """
  use Ash.Type

  @values [running: 0, complete: 1, cancelled: 2, invoiced: 3]
  @atom_to_int Map.new(@values)
  @int_to_atom Map.new(@values, fn {k, v} -> {v, k} end)

  @impl Ash.Type
  def storage_type(_), do: :integer

  @impl Ash.Type
  def cast_input(value, _) when is_atom(value) do
    if value in Map.keys(@atom_to_int) do
      {:ok, value}
    else
      :error
    end
  end

  def cast_input(value, _) when is_integer(value) do
    if Map.has_key?(@int_to_atom, value) do
      {:ok, Map.get(@int_to_atom, value)}
    else
      :error
    end
  end

  def cast_input(value, _) when is_binary(value) do
    case String.to_existing_atom(value) do
      atom when is_atom(atom) -> cast_input(atom, nil)
      _ -> :error
    end
  rescue
    _ -> :error
  end

  def cast_input(_, _), do: :error

  @impl Ash.Type
  def cast_stored(nil, _), do: {:ok, nil}

  def cast_stored(value, _) when is_integer(value) do
    case Map.get(@int_to_atom, value) do
      nil -> :error
      atom -> {:ok, atom}
    end
  end

  def cast_stored(_, _), do: :error

  @impl Ash.Type
  def dump_to_native(value, _) when is_atom(value) do
    case Map.get(@atom_to_int, value) do
      nil -> :error
      int -> {:ok, int}
    end
  end

  def dump_to_native(_, _), do: :error
end

and this is the now failing code:
  @spec list_active() :: [Timer.t()]
  def list_active do
    Timer
    |> Ash.Query.filter(status not in [:cancelled, :invoiced])
    |> Ash.Query.load([:customer])
    |> Ash.Query.sort(:id)
    |> Ash.read!()
  end

And the error in the test as attachment.
Was this page helpful?