dataclasses-avroschema
dataclasses-avroschema copied to clipboard
Customizing the name of array field
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"
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
@ardalan-nhd Now it is possible to define inner_name
for array
, maps
and fixed
. Check the documentation
Wow. Thanks for the update. Much appreciated