koxudaxi / datamodel-code-generator

Pydantic model and dataclasses.dataclass generator for easy conversion of JSON, OpenAPI, JSON Schema, and YAML data sources.
https://koxudaxi.github.io/datamodel-code-generator/
MIT License
2.74k stars 299 forks source link

Nullable Enum w/ `--enum-field-as-literal all` option is incompatible #424

Closed philipbjorge closed 3 years ago

philipbjorge commented 3 years ago

Describe the bug We are creating a nullable enum type as described here and supported here.

To Reproduce

Example schema:

Foo:
  type: object
  properties:
    bar:
      type: string
      nullable: true
      enum:
        - fizz
        - buzz
        - null

Used commandline:

$ datamodel-codegen --input repro.yaml --output repro.py --enum-field-as-literal all

Expected behavior I expected my models to be generated looking like...

class MyObject(BaseModel):
    my_nullable_enum: Optional[Literal['foo', 'bar']] = Field(...)

Error

Traceback (most recent call last):
  File "/Users/philipbjorge/.cache/pre-commit/repor4mhqibo/py_env-python3.9/lib/python3.9/site-packages/datamodel_code_generator/__main__.py", line 407, in main
    generate(
  File "/Users/philipbjorge/.cache/pre-commit/repor4mhqibo/py_env-python3.9/lib/python3.9/site-packages/datamodel_code_generator/__init__.py", line 334, in generate
    results = parser.parse()
  File "/Users/philipbjorge/.cache/pre-commit/repor4mhqibo/py_env-python3.9/lib/python3.9/site-packages/datamodel_code_generator/parser/base.py", line 393, in parse
    self.parse_raw()
  File "/Users/philipbjorge/.cache/pre-commit/repor4mhqibo/py_env-python3.9/lib/python3.9/site-packages/datamodel_code_generator/parser/openapi.py", line 31, in parse_raw
    self.parse_raw_obj(
  File "/Users/philipbjorge/.cache/pre-commit/repor4mhqibo/py_env-python3.9/lib/python3.9/site-packages/datamodel_code_generator/parser/jsonschema.py", line 1023, in parse_raw_obj
    self.parse_obj(name, JsonSchemaObject.parse_obj(raw), path)
  File "/Users/philipbjorge/.cache/pre-commit/repor4mhqibo/py_env-python3.9/lib/python3.9/site-packages/datamodel_code_generator/parser/jsonschema.py", line 1033, in parse_obj
    self.parse_object(name, obj, path)
  File "/Users/philipbjorge/.cache/pre-commit/repor4mhqibo/py_env-python3.9/lib/python3.9/site-packages/datamodel_code_generator/parser/jsonschema.py", line 556, in parse_object
    fields=self.parse_object_fields(
  File "/Users/philipbjorge/.cache/pre-commit/repor4mhqibo/py_env-python3.9/lib/python3.9/site-packages/datamodel_code_generator/parser/jsonschema.py", line 509, in parse_object_fields
    field_type = self.parse_item(modular_name, field, [*path, field_name])
  File "/Users/philipbjorge/.cache/pre-commit/repor4mhqibo/py_env-python3.9/lib/python3.9/site-packages/datamodel_code_generator/parser/jsonschema.py", line 633, in parse_item
    return self.data_type(literals=item.enum)
  File "/Users/philipbjorge/.cache/pre-commit/repor4mhqibo/py_env-python3.9/lib/python3.9/site-packages/datamodel_code_generator/types.py", line 197, in __init__
    super().__init__(**values)  # type: ignore
  File "/Users/philipbjorge/.cache/pre-commit/repor4mhqibo/py_env-python3.9/lib/python3.9/site-packages/datamodel_code_generator/reference.py", line 41, in __init__
    super().__init__(**values)
  File "pydantic/main.py", line 400, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for ContextDataType
literals -> 2
  none is not an allowed value (type=type_error.none.not_allowed)

Version:


Thanks for taking a look at this, really appreciate all the great work gone into this library!

philipbjorge commented 3 years ago

@koxudaxi -- Thank you so much for being so responsive on this project! Thanks for cutting a release too! I didn't even have to explain to my team why we're taking a fork :)

koxudaxi commented 3 years ago

@philipbjorge

I know the quick release is worth it. :smile: Thank you very much.

koxudaxi commented 3 years ago

@philipbjorge Wow, Thank you for sponsoring me :laughing: :bowing_man:

philipbjorge commented 3 years ago

@koxudaxi You deserve at least a 🍕 for lunch for this great project 🙇