Closed Masqueey closed 1 month ago
Both classes are called in the same way, should it not be something like
class MessageHeader(AvroBaseModel):
version: str
MessageType: str
class Message(AvroBaseModel):
MessageHeader: typing.Optional[typing.List[MessageHeader]]
MessageBody: str
When an avro schema
has fields that are clashing with others, for example a field name is the same as a record name the model generated might have problems as the one described in the issue
There are 3 ways to solve this problem:
class MessageHeader
, add a class Meta
and change the typing
where the type
is used. From the algorithmically point of view is complicated as we need to change the class name
, the typing
and add the class Meta
, plus it is more code to maintain. MessageHeader
and add an alias, for example:
message_header: typing.Optional[typing.List[MessageHeader]] = field(metadata={aliases=["MessageHeader"]})
. This is simpler but It will not reflect the original schematype
won't be defined any more in the class scope
, example: _MessageHeader = MessageHeader
. This solution is simpler, it implies less code and it seems more pythonic. This solution seems to be the preferred one for pydantic
users as well.I will process with solution number 3. The output model will be:
from dataclasses_avroschema.pydantic import AvroBaseModel
import pydantic
import typing
class MessageHeader(AvroBaseModel):
version: str
MessageType: str
_MessageHeader = MessageHeader
class Message(AvroBaseModel):
MessageBody: str
MessageHeader: typing.Optional[typing.List[_MessageHeader]] = None
print(Message.fake(), "\n")
>>> MessageBody='rrzzTDuvXOovfphUmjrY' MessageHeader=[MessageHeader(version='lMVdWZICUIkEFNbyUrck', MessageType='ciaNGFZdHPDwVEEdCpdN')]
Describe the bug A schema with a field name that is identical to a class name will fail to be parsed by Pydantic (and also create the wrong
fake()
data) when it is within a type definition (e.g. astyping.List[ExampleType]
).To Reproduce
AvroBaseModel
fromdataclasses_avroschema.pydantic
.As an example:
.fake()
on the model. It will generate eitherNone
or[None]
To Bypass
_
To bypass the error in the above example:
Expected behavior Have the model be usable in the same way when not using any typing specifications like optional or list without changing the name or having to add metadata to the model.