Skip to content

Commit 0eca3c0

Browse files
committed
better handling of enums
1 parent 19af19e commit 0eca3c0

9 files changed

Lines changed: 18 additions & 13 deletions

File tree

modules/openapi-generator/src/main/resources/elixir/model.mustache

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@
1414
@primary_key false
1515
embedded_schema do
1616
{{#ectoFields}}
17+
{{#isEnum}}
18+
field {{#atom}}{{&baseName}}{{/atom}}, Ecto.Enum, values: [{{#allowableValues}}{{#values}}{{#atom}}{{.}}{{/atom}}{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}]
19+
{{/isEnum}}
20+
{{^isEnum}}
1721
field {{#atom}}{{&baseName}}{{/atom}}, {{{ectoType}}}
22+
{{/isEnum}}
1823
{{/ectoFields}}
1924
{{#ectoEmbeds}}
2025
{{^isArray}}embeds_one{{/isArray}}{{#isArray}}embeds_many{{/isArray}} {{#atom}}{{&baseName}}{{/atom}}, {{&moduleName}}.Model.{{^isArray}}{{{baseType}}}{{/isArray}}{{#isArray}}{{{items.baseType}}}{{/isArray}}

samples/client/petstore/elixir/lib/openapi_petstore/model/child_with_nullable.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ defmodule OpenapiPetstore.Model.ChildWithNullable do
1616
@derive {JSON.Encoder, only: [:type, :nullableProperty, :otherProperty]}
1717
@primary_key false
1818
embedded_schema do
19-
field :type, :string
19+
field :type, Ecto.Enum, values: [:ChildWithNullable]
2020
field :nullableProperty, :string
2121
field :otherProperty, :string
2222
end

samples/client/petstore/elixir/lib/openapi_petstore/model/enum_arrays.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ defmodule OpenapiPetstore.Model.EnumArrays do
1515
@derive {JSON.Encoder, only: [:just_symbol, :array_enum]}
1616
@primary_key false
1717
embedded_schema do
18-
field :just_symbol, :string
19-
field :array_enum, {:array, :string}
18+
field :just_symbol, Ecto.Enum, values: [:">=", :"$"]
19+
field :array_enum, Ecto.Enum, values: [:fish, :crab]
2020
end
2121

2222
@spec changeset(t(), map()) :: Ecto.Changeset.t()

samples/client/petstore/elixir/lib/openapi_petstore/model/enum_test.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ defmodule OpenapiPetstore.Model.EnumTest do
2121
@derive {JSON.Encoder, only: [:enum_string, :enum_string_required, :enum_integer, :enum_number, :outerEnum, :outerEnumInteger, :outerEnumDefaultValue, :outerEnumIntegerDefaultValue]}
2222
@primary_key false
2323
embedded_schema do
24-
field :enum_string, :string
25-
field :enum_string_required, :string
26-
field :enum_integer, :integer
27-
field :enum_number, :float
24+
field :enum_string, Ecto.Enum, values: [:UPPER, :lower, :""]
25+
field :enum_string_required, Ecto.Enum, values: [:UPPER, :lower, :""]
26+
field :enum_integer, Ecto.Enum, values: [:"1", :"-1"]
27+
field :enum_number, Ecto.Enum, values: [:"1.1", :"-1.2"]
2828
embeds_one :outerEnum, OpenapiPetstore.Model.OuterEnum
2929
embeds_one :outerEnumInteger, OpenapiPetstore.Model.OuterEnumInteger
3030
embeds_one :outerEnumDefaultValue, OpenapiPetstore.Model.OuterEnumDefaultValue

samples/client/petstore/elixir/lib/openapi_petstore/model/map_test.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ defmodule OpenapiPetstore.Model.MapTest do
1818
@primary_key false
1919
embedded_schema do
2020
field :map_map_of_string, :map
21-
field :map_of_enum_string, :map
21+
field :map_of_enum_string, Ecto.Enum, values: [:UPPER, :lower]
2222
field :direct_map, :map
2323
field :indirect_map, :map
2424
end

samples/client/petstore/elixir/lib/openapi_petstore/model/order.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ defmodule OpenapiPetstore.Model.Order do
2323
field :petId, :integer
2424
field :quantity, :integer
2525
field :shipDate, :utc_datetime
26-
field :status, :string
26+
field :status, Ecto.Enum, values: [:placed, :approved, :delivered]
2727
field :complete, :boolean
2828
end
2929

samples/client/petstore/elixir/lib/openapi_petstore/model/parent_with_nullable.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ defmodule OpenapiPetstore.Model.ParentWithNullable do
1515
@derive {JSON.Encoder, only: [:type, :nullableProperty]}
1616
@primary_key false
1717
embedded_schema do
18-
field :type, :string
18+
field :type, Ecto.Enum, values: [:ChildWithNullable]
1919
field :nullableProperty, :string
2020
end
2121

samples/client/petstore/elixir/lib/openapi_petstore/model/pet.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ defmodule OpenapiPetstore.Model.Pet do
2222
field :id, :integer
2323
field :name, :string
2424
field :photoUrls, {:array, :string}
25-
field :status, :string
25+
field :status, Ecto.Enum, values: [:available, :pending, :sold]
2626
embeds_one :category, OpenapiPetstore.Model.Category
2727
embeds_many :tags, OpenapiPetstore.Model.Tag
2828
end

samples/client/petstore/elixir/test/deserializer_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ defmodule DeserializerTest do
2424
"name": "sea"
2525
}
2626
],
27-
"status": "foo"
27+
"status": "available"
2828
}
2929
"""
3030

@@ -41,7 +41,7 @@ defmodule DeserializerTest do
4141
name: "Nagga",
4242
photoUrls: ["https://example.com/nagga1.jpg", "https://example.com/nagga2.jpg"],
4343
tags: [%Tag{id: 99, name: "dragon"}, %Tag{id: 23, name: "sea"}],
44-
status: "foo"
44+
status: :available
4545
}
4646
end
4747

0 commit comments

Comments
 (0)