Skip to content

Commit 8998d83

Browse files
authored
[elixir] fixes outer enum bug #16412 (#20587)
1 parent 68e7d49 commit 8998d83

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

modules/openapi-generator/src/main/resources/elixir/deserializer.ex.mustache

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,12 @@ defmodule {{moduleName}}.Deserializer do
9191
end
9292
end
9393

94-
defp to_struct(map_or_list, module)
94+
defp to_struct(value, module)
9595
defp to_struct(nil, _), do: nil
96+
97+
defp to_struct(binary, module) when is_binary(binary) and is_atom(module) do
98+
module.decode(binary)
99+
end
96100

97101
defp to_struct(list, module) when is_list(list) and is_atom(module) do
98102
Enum.map(list, &to_struct(&1, module))

samples/client/petstore/elixir/lib/openapi_petstore/deserializer.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,12 @@ defmodule OpenapiPetstore.Deserializer do
9393
end
9494
end
9595

96-
defp to_struct(map_or_list, module)
96+
defp to_struct(value, module)
9797
defp to_struct(nil, _), do: nil
98+
99+
defp to_struct(binary, module) when is_binary(binary) and is_atom(module) do
100+
module.decode(binary)
101+
end
98102

99103
defp to_struct(list, module) when is_list(list) and is_atom(module) do
100104
Enum.map(list, &to_struct(&1, module))
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
defmodule OuterEnumTest do
2+
use ExUnit.Case, async: true
3+
4+
alias OpenapiPetstore.Deserializer
5+
alias OpenapiPetstore.Model.EnumTest
6+
7+
@valid_json """
8+
{
9+
"enum_string": "UPPER",
10+
"outerEnum": "placed"
11+
}
12+
"""
13+
14+
@tag timeout: :infinity
15+
test "jason_decode/2 with valid JSON" do
16+
assert Deserializer.jason_decode(@valid_json, EnumTest) ==
17+
{:ok,
18+
%EnumTest{
19+
enum_string: "UPPER",
20+
outerEnum: "placed"
21+
}}
22+
end
23+
end

0 commit comments

Comments
 (0)