dataclasses-avroschema icon indicating copy to clipboard operation
dataclasses-avroschema copied to clipboard

Parsing of logicalType timestamp-millis failing with default: null

Open bengels97 opened this issue 1 year ago • 2 comments

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

bengels97 avatar Aug 23 '24 13:08 bengels97

Does not seem to be related to issue:#726 (as far as I can see). The issue is still present with PR:https://github.com/marcosschroh/dataclasses-avroschema/pull/734 implemented

bengels97 avatar Aug 27 '24 07:08 bengels97

The PR will not solve the issue, but it is related in the sense that there are not nested type when declaring logicalTypes. Now it is possible to achieve optional logicalTypes defined type twice

marcosschroh avatar Aug 27 '24 09:08 marcosschroh