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 issues when schema contains reference record types #692

Closed sanjayjayaramu-cruise closed 1 month ago

sanjayjayaramu-cruise commented 1 month ago

Describe the bug If the schema contains more than one record type fields of the same type, the generated field's type in the dataclass does not inherit the class' pascal case but instead uses the type defined in the schema.

To Reproduce

from dataclasses_avroschema import ModelGenerator

model_generator = ModelGenerator()

schema = {
        'type': 'record', 
        'doc': 'The 3D bounding box labels of this object.', 
        'name': 'prism', 
        'fields': [
            {'doc': 'the pose for this object.', 
             'name': 'pose', 
             'type': {
                 'type': 'record', 
                 'doc': 'Represents a transformation from one frame to another as a translation vector and a rotation quaternion.', 
                 'name': 'pose', 
                 'fields': [{
                     'name': 'pose_name', 
                     'type': 'string'
                     }]
                }
            }, 
            {
                'doc': 'The object’s Pose in Chassis Frame', 
                'name': 'object_to_chassis', 
                'type': 'pose'
                }
            ]
        }

result = model_generator.render(schema=schema)

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

Generated model

from dataclasses_avroschema import AvroModel
import dataclasses

@dataclasses.dataclass
class Pose(AvroModel):
    """
    Represents a transformation from one frame to another as a translation vector and a rotation quaternion.
    """
    pose_name: str

@dataclasses.dataclass
class Prism(AvroModel):
    """
    The 3D bounding box labels of this object.
    """
    pose: Pose = dataclasses.field(metadata={'doc': 'the pose for this object in cruiseframe.'})
    object_to_chassis: pose = dataclasses.field(metadata={'doc': 'The object’s Pose in Chassis Frame'})

Expected behavior Expected the type of the field to match the generated class name of the Avro record

from dataclasses_avroschema import AvroModel
import dataclasses

@dataclasses.dataclass
class Pose(AvroModel):
    """
    Represents a transformation from one frame to another as a translation vector and a rotation quaternion.
    """
    pose_name: str

@dataclasses.dataclass
class Prism(AvroModel):
    """
    The 3D bounding box labels of this object.
    """
    pose: Pose = dataclasses.field(metadata={'doc': 'the pose for this object in cruiseframe.'})
    object_to_chassis: Pose = dataclasses.field(metadata={'doc': 'The object’s Pose in Chassis Frame'})
sanjayjayaramu-cruise commented 1 month ago

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

marcosschroh commented 1 month ago

closed by https://github.com/marcosschroh/dataclasses-avroschema/pull/694