datamodel-code-generator icon indicating copy to clipboard operation
datamodel-code-generator copied to clipboard

BaseClass for enums by enum type

Open adaamz opened this issue 3 years ago • 0 comments

Is your feature request related to a problem? Please describe. The problem is that when I want to serialize int/str enum that is child of enum.Enum i got this error

value is not a valid enumeration member; permitted: 1, 2, 3

for code

Asdf(enum_abc=EnumAbc.A)

because the EnumAbc is child of enum.Enum and not enum.IntEnum (i have no more detailed description for this problem, but it just works)

class EnumAbc(Enum):
  A = 1
  B = 2
  C = 3

Describe the solution you'd like I would like to specify (implicitely deducted by type or somehow explicitely) the parent of each enum.

components:
  schemas:
    EnumAbc:
      type: integer
      enum: [1, 2, 3]
      x-enum-varnames: [A, B, C]

should generate this python code:

from enum import IntEnum

class EnumAbc(IntEnum):
    A = 1
    B = 2
    C = 3

or for string enums:

components:
  schemas:
    StringEnumAbc:
      type: string
      enum: ["a", "b", "c"]

should generate this python code:

from enum import Enum

class StringEnumAbc(str, Enum):
    A = "a"
    B = "b"
    C = "c"

(or lowercased names, i already changed this in my template 😁)

Describe alternatives you've considered implicitely or explicitely as stated before :) by explicitely i mean sth like this

components:
  schemas:
    EnumAbc:
      type: integer
      x-base-class: enum.IntEnum
      enum: [1, 2, 3]
      x-enum-varnames: [A, B, C]

respectively

components:
  schemas:
    StringEnumAbc:
      type: string
      x-base-class: str
      enum: ["a", "b", "c"]

adaamz avatar Nov 19 '21 10:11 adaamz