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

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

Open
bengels97 opened this issue Aug 23, 2024 · 2 comments
Open

Comments

@bengels97
Copy link
Contributor

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
Copy link
Contributor Author

Does not seem to be related to issue:#726 (as far as I can see). The issue is still present with PR:#734 implemented

@marcosschroh
Copy link
Owner

marcosschroh commented Aug 27, 2024

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

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