From 2ee226ff3816dd72a845ef86d13b5699e22d3c91 Mon Sep 17 00:00:00 2001 From: Scott Ames-Messinger Date: Fri, 19 Mar 2021 10:04:08 -0400 Subject: [PATCH 1/2] fix: Load values as strings to serialize correctly Previously, when an enum would be loaded, it would be loaded as the. module name. When the schema was serialized, the module name wasn't changed to the string, causing the returned JSON to be something like: ``` %{ status: "Elixir.MyApp.Status.Published" } ``` --- lib/enum_type.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/enum_type.ex b/lib/enum_type.ex index c663078..f1dd166 100644 --- a/lib/enum_type.ex +++ b/lib/enum_type.ex @@ -121,7 +121,7 @@ defmodule EnumType do def cast(unquote(option)), do: {:ok, unquote(option)} def cast(unquote(value)), do: {:ok, unquote(option)} - def load(unquote(value)), do: {:ok, unquote(option)} + def load(unquote(value)), do: {:ok, unquote(option).value} # Allow both querying by Module and setting a value to the Module when updating or inserting. def dump(unquote(option)), do: {:ok, unquote(option).value} From 3a617feb0d9b60cf3a7ffa728f7e3ed3bb157957 Mon Sep 17 00:00:00 2001 From: Scott Ames-Messinger Date: Wed, 31 Mar 2021 10:43:54 -0400 Subject: [PATCH 2/2] fix: Dump the values if either a module or a string --- lib/enum_type.ex | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/enum_type.ex b/lib/enum_type.ex index f1dd166..e59e6c1 100644 --- a/lib/enum_type.ex +++ b/lib/enum_type.ex @@ -121,10 +121,12 @@ defmodule EnumType do def cast(unquote(option)), do: {:ok, unquote(option)} def cast(unquote(value)), do: {:ok, unquote(option)} - def load(unquote(value)), do: {:ok, unquote(option).value} + def load(unquote(value)), do: {:ok, unquote(value)} # Allow both querying by Module and setting a value to the Module when updating or inserting. - def dump(unquote(option)), do: {:ok, unquote(option).value} + def dump(unquote(option)), do: {:ok, unquote(value)} + def dump(unquote(value)), do: {:ok, unquote(value)} + end end end