Datamodel-code-generator: Array of enum not generating code properly

Created on 12 Jun 2020  路  2Comments  路  Source: koxudaxi/datamodel-code-generator

Describe the bug
Hi! It seems that when using an array of enum (string type), the code is not being generated properly. Instead of making a List of enums, it always allows any string because it makes a List[str].

To Reproduce

Example schema:

openapi: 3.0.0
info:
  title: datamodel-code-generator bug example
components:
  schemas:
    Type1:
      type: array
      items:
        type: string
        enum:
          - enumOne
          - enumTwo

    Type2:
      type: string
      enum:
        - enumFour
        - enumFive

Code generated:

from __future__ import annotations

from enum import Enum
from typing import List

from pydantic import BaseModel


class Type1(BaseModel):
    __root__: List[str]


class Type2(Enum):
    enumFour = 'enumFour'
    enumFive = 'enumFive'

Used commandline:

$ datamodel-codegen --input input.yaml --output tmp.py

Expected behavior
Expected behaviour would be that inside of Type1, there should be List[Enum] where Enum is a class that has the correct enum types allowed.

Version:

  • OS: MacOS 10.15
  • Python Verison: 3.7.7
  • datamodel-code-generator Version: 0.5.4

Or is there an alternative way of achieving the above that I am missing?
The goal is to essentially have a class that accepts a List of enum.
Thanks!

enhancement released

Most helpful comment

Thank you for quickly fixing that, going to close this issue!

All 2 comments

@jaydhulia

Thank you for creating this issue.
I have fixed it and released a new version as 0.5.5

Thank you for quickly fixing that, going to close this issue!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

languitar picture languitar  路  3Comments

MikeZaharov picture MikeZaharov  路  3Comments

fzirker picture fzirker  路  6Comments

elonzh picture elonzh  路  4Comments

FlorianLudwig picture FlorianLudwig  路  3Comments