Skip to content

Parsing of logicalType timestamp-millis failing with default: null #727

@bengels97

Description

@bengels97

Describe the bug
A clear and concise description of what the bug is.

Parsing of default_repr failing in BaseGenerator.get_field_default and unable generate a model to with the following schema:

{
  "type": "record",
  "name": "TestEvent",
  "namespace": "com.example",
  "doc": "Bla bla",
  "fields": [
    {
      "logicalType": "timestamp-millis",
      "default": null,
      "name": "timeStamp",
      "type": [
        "null",
        "long"
      ]
    }
  ]
}

To Reproduce
Steps to reproduce the behavior

With the schema provided above, running the code snippet results in a TypeError.

model_generator.render(schema=schema, model_type=ModelType.PYDANTIC)
  File "/Users/xxx/Documents/Development/xxx/xxx/.venv/lib/python3.11/site-packages/dataclasses_avroschema/model_generator/lang/python/base.py", line 176, in <listcomp>
    self.render_field(field=field, model_name=name) for field in record_fields
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/xxx/Documents/Development/xxx/xxx/.venv/lib/.venv/lib/python3.11/site-packages/dataclasses_avroschema/model_generator/lang/python/base.py", line 278, in render_field
    default_generated = self.get_field_default(
                        ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/xxx/Documents/Development/xxx/xxx/.venv/lib/.venv/lib/python3.11/site-packages/dataclasses_avroschema/model_generator/lang/python/base.py", line 616, in get_field_default
    python_type = func(default)
                  ^^^^^^^^^^^^^
  File "/Users/xxx/Documents/Development/xxx/xxx/.venv/lib/.venv/lib/python3.11/site-packages/dataclasses_avroschema/model_generator/lang/python/avro_to_python_utils.py", line 40, in <lambda>
    field_utils.TIMESTAMP_MILLIS: lambda value: datetime.datetime.fromtimestamp(value / 1000, tz=datetime.timezone.utc),
                                                                                ~~~~~~^~~~~~
TypeError: unsupported operand type(s) for /: 'NoneType' and 'int'

Expected behavior
A clear and concise description of what you expected to happen.

ModelGenerator.render(model_type=ModelType.PYDANTIC) should successfully parse the provided schema into a DataModel that would look like:

class TestEvent(BaseModel):
    timeStamp: typing.Optional[datetime.datetime] = None

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions