[elixir] fixes outer enum bug #16412 (#20587)

This commit is contained in:
Ian Asaff 2025-02-04 04:52:20 -05:00 committed by GitHub
parent 68e7d49456
commit 8998d83f99
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 33 additions and 2 deletions

View File

@ -91,8 +91,12 @@ defmodule {{moduleName}}.Deserializer do
end
end
defp to_struct(map_or_list, module)
defp to_struct(value, module)
defp to_struct(nil, _), do: nil
defp to_struct(binary, module) when is_binary(binary) and is_atom(module) do
module.decode(binary)
end
defp to_struct(list, module) when is_list(list) and is_atom(module) do
Enum.map(list, &to_struct(&1, module))

View File

@ -93,8 +93,12 @@ defmodule OpenapiPetstore.Deserializer do
end
end
defp to_struct(map_or_list, module)
defp to_struct(value, module)
defp to_struct(nil, _), do: nil
defp to_struct(binary, module) when is_binary(binary) and is_atom(module) do
module.decode(binary)
end
defp to_struct(list, module) when is_list(list) and is_atom(module) do
Enum.map(list, &to_struct(&1, module))

View File

@ -0,0 +1,23 @@
defmodule OuterEnumTest do
use ExUnit.Case, async: true
alias OpenapiPetstore.Deserializer
alias OpenapiPetstore.Model.EnumTest
@valid_json """
{
"enum_string": "UPPER",
"outerEnum": "placed"
}
"""
@tag timeout: :infinity
test "jason_decode/2 with valid JSON" do
assert Deserializer.jason_decode(@valid_json, EnumTest) ==
{:ok,
%EnumTest{
enum_string: "UPPER",
outerEnum: "placed"
}}
end
end