dataclasses-avroschema icon indicating copy to clipboard operation
dataclasses-avroschema copied to clipboard

Customizing the name of array field

Open ardalan-nhd opened this issue 1 year ago • 1 comments

Describe the bug Can't customize the name of array fields to match the given avro schema

To Reproduce I tried to recreate this avro schema

{
    "type": "record",
    "name": "DeliveryBatch",
    "fields": [
        {
            "name": "receivers_payload",
            "type": {
                "type": "array",
                "items": "string",
                "name": "my_custom_name"
            }
        }
    ],
    "namespace": "app.delivery.email"
}

Using this code:

@dataclass
class TransactionalEmailDTO(AvroModel):
    receivers_payloads: List[str]

    class Meta:
        schema_name = "DeliveryBatch"
        namespace = "app.delivery.email"

But the output will be:

{
    "type": "record",
    "name": "DeliveryBatch",
    "fields": [
        {
            "name": "receivers_payload",
            "type": {
                "type": "array",
                "items": "string",
                "name": "receivers_payload"
            }
        }
    ],
    "namespace": "app.delivery.email"
}

As much as I know, in array fields for the inner name of the field, the library just removes the plural "s" at the end of the name to make it singular. So you can't customize the name of array field at all.

Expected behavior I think we should be able to customize the name of array fields like the way Enum fields are done. For example:

class my_custom_name(ListType): # ListType is something custom that should be defined in library
    class Meta:
        item_type = str

@dataclass
class TransactionalEmailDTO(AvroModel):
    receivers_payloads: my_custom_name

    class Meta:
        schema_name = "DeliveryBatch"
        namespace = "app.delivery.email"

ardalan-nhd avatar Feb 07 '24 12:02 ardalan-nhd

Hi @ardalan-nhd

We should have a way to do it. What about something like:

from dataclasses import dataclass, field


@dataclass
class TransactionalEmailDTO(AvroModel):
    receivers_payloads: List[str] = field(metadata={"inner-name": "my_custom_name"})

Then inner-name will be use to generate the internal field name. The same should happens for maps, fixed and enum

marcosschroh avatar Feb 16 '24 11:02 marcosschroh

@ardalan-nhd Now it is possible to define inner_name for array, maps and fixed. Check the documentation

marcosschroh avatar Mar 14 '24 15:03 marcosschroh

Wow. Thanks for the update. Much appreciated

ardalan-nhd avatar Mar 14 '24 23:03 ardalan-nhd