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
214 stars 64 forks source link

Model generation issue with proper casing for Enum types #693

Closed sanjayjayaramu-cruise closed 1 month ago

sanjayjayaramu-cruise commented 1 month ago

Describe the bug The casing for an enum field is incorrect and does not create a Pascal cased class name causing model with syntax error when field name and enum names are same

To Reproduce

from dataclasses_avroschema import ModelGenerator

model_generator = ModelGenerator()

schema = {
        'type': 'record', 
        'doc': 'The 3D bounding box labels of this object.', 
        'name': 'msl_prism', 
        'fields': [
            {'doc': 'Source of the ingested segment, ie. ON_ROAD, SIMULATION, etc.', 
             'default': 'UNKNOWN', 
             'name': 'data_source', 
             'type': {
                 'default': 'UNKNOWN', 
                 'type': 'enum', 
                 'name': 'data_source', 
                 'symbols': ['SIMULATION', 'ON_ROAD', 'UNKNOWN']
              }
            }
        ]
    }
result = model_generator.render(schema=schema)

# save the result in a file
with open("models.py", mode="+w") as f:
    f.write(result)

Actual

from dataclasses_avroschema import AvroModel
import dataclasses
import enum

class data_source(enum.Enum):
    SIMULATION = "SIMULATION"
    ON_ROAD = "ON_ROAD"
    UNKNOWN = "UNKNOWN"

    class Meta:
        default = "UNKNOWN"

@dataclasses.dataclass
class MslPrism(AvroModel):
    """
    The 3D bounding box labels of this object.
    """
    data_source: data_source = dataclasses.field(metadata={'doc': 'Source of the ingested segment, ie. ON_ROAD, SIMULATION, etc.'}, default=data_source.UNKNOWN)

Expected behavior

from dataclasses_avroschema import AvroModel
import dataclasses
import enum

class DataSource(enum.Enum):
    SIMULATION = "SIMULATION"
    ON_ROAD = "ON_ROAD"
    UNKNOWN = "UNKNOWN"

    class Meta:
        default = "UNKNOWN"

@dataclasses.dataclass
class MslPrism(AvroModel):
    """
    The 3D bounding box labels of this object.
    """
    data_source: DataSource = dataclasses.field(metadata={'doc': 'Source of the ingested segment, ie. ON_ROAD, SIMULATION, etc.'}, default=DataSource.UNKNOWN)
sanjayjayaramu-cruise commented 1 month ago

Proposed fix https://github.com/sanjayjayaramu-cruise/dataclasses-avroschema/commit/bc8371e48b0db1c21c49088dbde2cb03b0c0f9c2