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

Duplicate rendering of nested dataclasses which use the same field name and type #709

Closed sanjayjayaramu-cruise closed 1 month ago

sanjayjayaramu-cruise commented 1 month ago

Describe the bug When rendering the schema from a dataclass which has nested record fields and where two fields (belonging to different child fields) with same name and type, the same field type is rendered twice instead of referencing from the first definition

To Reproduce

from dataclasses_avroschema import ModelGenerator

model_generator = ModelGenerator()

schema ={
    "type": "record",
    "name": "UserDetails",
    "fields": [
        {
            "name": "address",
            "type": {
                "type" : "record",
                "name" : "address",
                "fields" : [
                    {
                        "name": "streetaddress", "type": "string"
                    },
                    {
                        "name": "city", "type": {
                            "type": "record",
                            "name": "city",
                            "fields" : [
                                { "name": "city_name", "type": "string" },
                                { "name": "zip_code", "type": "int"}
                            ]
                        }
                    }
                ]
            }
        },
        {
            "name": "work_location",
            "type": {
                "type" : "record",
                "name" : "work_location",
                "fields" : [
                    {
                        "name": "company_name", "type": "string"
                    },
                    {
                        "name": "city", "type": "city"
                    }
                ]
            }
        }
    ]
  }

result = model_generator.render(schema=schema)

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

Expected behavior Expected below schema

{
    "type": "record",
    "name": "UserDetails",
    "fields": [
        {
            "name": "address",
            "type": {
                "type" : "record",
                "name" : "address",
                "fields" : [
                    {
                        "name": "streetaddress", "type": "string"
                    },
                    {
                        "name": "city", "type": {
                            "type": "record",
                            "name": "city",
                            "fields" : [
                                { "name": "city_name", "type": "string" },
                                { "name": "zip_code", "type": "int"}
                            ]
                        }
                    }
                ]
            }
        },
        {
            "name": "work_location",
            "type": {
                "type" : "record",
                "name" : "work_location",
                "fields" : [
                    {
                        "name": "company_name", "type": "string"
                    },
                    {
                        "name": "city", "type": "city"
                    }
                ]
            }
        }
    ]
  }

But got

{
    "type": "record",
    "name": "UserDetails",
    "fields": [
        {
            "name": "address",
            "type": {
                "type" : "record",
                "name" : "address",
                "fields" : [
                    {
                        "name": "streetaddress", "type": "string"
                    },
                    {
                        "name": "city", "type": {
                            "type": "record",
                            "name": "city",
                            "fields" : [
                                { "name": "city_name", "type": "string" },
                                { "name": "zip_code", "type": "int"}
                            ]
                        }
                    }
                ]
            }
        },
        {
            "name": "work_location",
            "type": {
                "type" : "record",
                "name" : "work_location",
                "fields" : [
                    {
                        "name": "company_name", "type": "string"
                    },
                    {
                        "name": "city", "type": {
                            "type": "record",
                            "name": "city",
                            "fields" : [
                                { "name": "city_name", "type": "string" },
                                { "name": "zip_code", "type": "int"}
                            ]
                        }
                    }
                ]
            }
        }
    ]
  }