Page MenuHomePhorge

No OneTemporary

Size
2 KB
Referenced Files
None
Subscribers
None
diff --git a/lib/open_api_spex/cast/enum.ex b/lib/open_api_spex/cast/enum.ex
index 336ee6c..b78c146 100644
--- a/lib/open_api_spex/cast/enum.ex
+++ b/lib/open_api_spex/cast/enum.ex
@@ -1,48 +1,30 @@
defmodule OpenApiSpex.Cast.Enum do
@moduledoc false
alias OpenApiSpex.Cast
- def cast(%Cast{schema: %{enum: []}} = ctx) do
- Cast.error(ctx, {:invalid_enum})
- end
-
- def cast(%Cast{schema: %{enum: [value | _]}, value: value}) do
- {:ok, value}
- end
-
- # Special case: convert binary to atom enum
- def cast(ctx = %Cast{schema: schema = %{enum: [atom_value | tail]}, value: value})
- when is_binary(value) and is_atom(atom_value) do
- if value == to_string(atom_value) do
- {:ok, atom_value}
- else
- cast(%{ctx | schema: %{schema | enum: tail}})
+ def cast(ctx = %Cast{schema: %{enum: enum}, value: value}) do
+ case Enum.find(enum, {:error, :invalid_enum}, &equivalent?(&1, value)) do
+ {:error, :invalid_enum} -> Cast.error(ctx, {:invalid_enum})
+ found -> {:ok, found}
end
end
- # Special case: convert string-keyed map to atom-keyed map enum
- def cast(ctx = %Cast{schema: schema = %{enum: [enum_map = %{} | tail]}, value: value = %{}}) do
- if maps_equivalent?(value, enum_map) do
- {:ok, enum_map}
- else
- cast(%{ctx | schema: %{schema | enum: tail}})
- end
- end
+ defp equivalent?(x, x), do: true
- def cast(ctx = %Cast{schema: schema = %{enum: [_ | tail]}}) do
- cast(%{ctx | schema: %{schema | enum: tail}})
+ # Special case: atoms are equivalent to their stringified representation
+ defp equivalent?(left, right) when is_atom(left) and is_binary(right) do
+ to_string(left) == right
end
- defp maps_equivalent?(x, x), do: true
-
# an explicit schema should be used to cast to enum of structs
- defp maps_equivalent?(_left, %_struct{}), do: false
+ defp equivalent?(_x, %_struct{}), do: false
- defp maps_equivalent?(left = %{}, right = %{}) when map_size(left) == map_size(right) do
- Enum.all?(right, fn {k, v} ->
- maps_equivalent?(Map.get(left, to_string(k)), v)
+ # Special case: Atom-keyed maps are equivalent to their string-keyed representation
+ defp equivalent?(left, right) when is_map(left) and is_map(right) do
+ Enum.all?(left, fn {k, v} ->
+ equivalent?(v, Map.get(right, to_string(k)))
end)
end
- defp maps_equivalent?(_left, _right), do: false
+ defp equivalent?(_left, _right), do: false
end

File Metadata

Mime Type
text/x-diff
Expires
Fri, Nov 29, 6:40 PM (1 d, 16 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41272
Default Alt Text
(2 KB)

Event Timeline