question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No Enum generated for underlying type of array

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
goodoldneoncommented, Jan 28, 2022

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

1reaction
goodoldneoncommented, Jan 27, 2022

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)
Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - What's the use-case for specifying the underlying type in ...
The underlying type specifies how much storage is allocated for each enumerator. However, an explicit cast is needed to convert from enum type...
Read more >
Enumeration declaration - cppreference.com
1) Declares an unscoped enumeration type whose underlying type is not fixed (in this case, the underlying type is an implementation-defined ...
Read more >
EnumBuilder Class (System.Reflection.Emit) | Microsoft Learn
Retrieves an array of the values of the underlying type constants of this enumeration type. (Inherited from Type).
Read more >
Use Enumerated Data in Simulink Models - MathWorks
An enumerated data type is a MATLAB ® class that defines a set of enumerated values. Each enumerated value consists of an enumerated...
Read more >
Handbook - Enums - TypeScript
Computed and constant members · It is the first member in the enum and it has no initializer, in which case it's assigned...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found