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

No Enum generated for underlying type of array

Open thorin-schiffer opened this issue 3 years ago • 5 comments

If a field in a component has type array and subtype enum, no enum class is generated for this enum as it is for simple, non-composite enum field.

To Reproduce

Example schema:


      "arrayFieldWithEnum": {

        "items": {
          "enum": [
            "enum_value_1",
            "enum_value_2",
          ],
          "maxLength": 30,
          "minLength": 1,
          "pattern": "^.*$",
          "type": "string"
        },
        "maxItems": 3,
        "minItems": 1,
        "nullable": true,
        "type": "array",
        "uniqueItems": true,
      },


Generated with python:

    generate(
        input_=Path(schema_path),
        output=Path(models_path),
        reuse_model=True,
        validation=True,
        custom_template_dir=Path(templates_path),
        disable_timestamp=True,
        use_schema_description=True,
    )

Expected behavior A separate Enum class is generated for underlying type

Version:

  • OS: Ubuntu 18.04
  • Python version: 3.7
  • datamodel-code-generator version: 0.11.14

thorin-schiffer avatar Jan 24 '22 13:01 thorin-schiffer

Can you elaborate on what your schema is expressing? If you're saying arrayFieldWithEnum is an array of enums, why do you need to specify min length, max length, and a regex pattern?

amh4r avatar Jan 25 '22 01:01 amh4r

I guess this comes from the StopLight, the UI for editing OAS. I tried to remove those and models are generated as expected. Is there any way to ignore those?

thorin-schiffer avatar Jan 25 '22 14:01 thorin-schiffer

datamodel-code-generator seems to ignore the maxLength/minLength/pattern stuff sometimes, but not other times.

For example, the following OpenAPI:

openapi: "3.1.0"
components:
  schemas:
    Foo:
      type: object
      properties:
        bar:
          type: array
          items:
            enum:
              - hello
              - goodbye
            maxLength: 5
            minLength: 1
            type: string
            pattern: "^.*$"

Generates the following models:

class BarEnum(Enum):
    hello = 'hello'
    goodbye = 'goodbye'


class Foo(BaseModel):
    bar: Optional[List[BarEnum]] = None

But just adding maxItems causes the enum to be lost:

openapi: "3.1.0"
components:
  schemas:
    Foo:
      type: object
      properties:
        bar:
          type: array
          items:
            enum:
              - hello
              - goodbye
            maxLength: 5
            minLength: 1
            type: string
            pattern: "^.*$"
          maxItems: 3
class BarItem(BaseModel):
    __root__: constr(regex=r'^.*$', min_length=1, max_length=5)


class Foo(BaseModel):
    bar: Optional[List[BarItem]] = Field(None, max_items=3)

amh4r avatar Jan 27 '22 02:01 amh4r

Should we consider it a non-bug?

thorin-schiffer avatar Jan 28 '22 15:01 thorin-schiffer

Seems like a bug to me. Ignoring those properties when there's an enum makes sense to me, but they should be consistently ignored

amh4r avatar Jan 28 '22 17:01 amh4r

@thorin-schiffer @goodoldneon I'm sorry for my too-late reply.

I have released the new version as 0.14.1.

koxudaxi avatar Dec 31 '22 02:12 koxudaxi

Great! Thank you very much!

On Sat, 31 Dec 2022 at 03:03, Koudai Aono @.***> wrote:

@thorin-schiffer https://github.com/thorin-schiffer @goodoldneon https://github.com/goodoldneon I'm sorry for my too-late reply.

I have released the new version as 0.14.1.

— Reply to this email directly, view it on GitHub https://github.com/koxudaxi/datamodel-code-generator/issues/690#issuecomment-1368146824, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA2XDHGMGRH63ANXUIYKBKTWP6H6PANCNFSM5MVK2NHQ . You are receiving this because you were mentioned.Message ID: @.***>

thorin-schiffer avatar Jan 04 '23 16:01 thorin-schiffer