Skip to content

Commit

Permalink
[elixir] fixes outer enum bug #16412 (#20587)
Browse files Browse the repository at this point in the history
  • Loading branch information
montague authored Feb 4, 2025
1 parent 68e7d49 commit 8998d83
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
23 changes: 23 additions & 0 deletions samples/client/petstore/elixir/test/outer_enum_test.exs
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8998d83

Please sign in to comment.