Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
philipbjorge opened this issue May 5, 2021 · 4 comments
Closed

Comments

@philipbjorge
Copy link
Contributor

philipbjorge commented May 5, 2021

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:

  • OS: OSX
  • Python version: 3.9.4
  • datamodel-code-generator version: 0.11.3

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

@philipbjorge
Copy link
Contributor Author

@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
Copy link
Owner

koxudaxi commented May 5, 2021

@philipbjorge

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

@koxudaxi
Copy link
Owner

koxudaxi commented May 5, 2021

@philipbjorge
Wow, Thank you for sponsoring me 😆 🙇‍♂️

@philipbjorge
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants