marcosschroh / dataclasses-avroschema

Generate avro schemas from python classes. Code generation from avro schemas. Serialize/Deserialize python instances with avro schemas
https://marcosschroh.github.io/dataclasses-avroschema/
MIT License
213 stars 64 forks source link

Support for python class to be generated for enum type AVRO schema (without `fields`) #664

Closed vsresearchgate closed 1 month ago

vsresearchgate commented 3 months ago

Is your feature request related to a problem? Please describe. I have a valid AVRO schema which I am unable to translate into a Python class. Example:

{
    "type": "enum",
    "name": "Color",
    "symbols": [
      "red",
      "blue"
    ]
}

running the following fails:

from dataclasses_avroschema import ModelGenerator, ModelType
schema = {
    "type": "enum",
    "name": "Color",
    "symbols": [
      "red",
      "blue"
    ]
} # this is actually loaded from the AVSC file. Pasting the dict for simplicity.
model_generator = ModelGenerator()
python_class = model_generator.render(schema=schema, model_type=ModelType.DATACLASS.value)

with the error:

  File "<location>/.venv/lib/python3.9/site-packages/dataclasses_avroschema/model_generator/generator.py", line 67, in render
    return self.render_module(
  File "<location>/.venv/lib/python3.9/site-packages/dataclasses_avroschema/model_generator/generator.py", line 97, in render_module
    return model_generator.render(schemas=schemas)
  File "<location>/.venv/lib/python3.9/site-packages/dataclasses_avroschema/model_generator/lang/python/base.py", line 66, in render
    classes = "\n".join(self.render_class(schema=schema) for schema in schemas)
  File "<location>/.venv/lib/python3.9/site-packages/dataclasses_avroschema/model_generator/lang/python/base.py", line 66, in <genexpr>
    classes = "\n".join(self.render_class(schema=schema) for schema in schemas)
  File "<location>/.venv/lib/python3.9/site-packages/dataclasses_avroschema/model_generator/lang/python/base.py", line 143, in render_class
    record_fields: typing.List[JsonDict] = schema["fields"]
KeyError: 'fields'

Describe the solution you'd like It would be great to support all valid AVRO schemas to be supported and converted to python classes, also when fields is missing

Describe alternatives you've considered

Additional context Add any other context or screenshots about the feature request here.

marcosschroh commented 1 month ago

Thanks for reporting it @vsresearchgate. I will try to fix it

marcosschroh commented 1 month ago

You can check the documentation