You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
I would like a way to set the name of fields in AvroBaseModels separate from the field name defined in python.
I'm trying to use AvroBaseModel to represent payloads like the following:
{
"schema": {
"value": "my_schema_id"
}
}
The desired avro and jsonschemas for this look like:
(note that the jsonschema can use a definition reference for the section with "title": "Schema")
In order to model this, I might write code like this:
class Schema(AvroBaseModel):
value: str
class MyModel(AvroBaseModel):
schema: Schema
but that would result in the following error from pydantic:
NameError: Field name "schema" shadows a BaseModel attribute; use a different field name with "alias='schema'".
We can fix this by giving the field a different name and then using a pydantic Config fields dictionary to set the name that the schema should use:
class Schema(AvroBaseModel):
value: str
class MyModel(AvroBaseModel):
schema_: Schema
class Config:
fields = {"schema_": "schema"}
This fixes the jsonschema that we get with MyModel.json_schema(), but the avro schema we get with MyModel.avro_schema() still uses the name of the python field (schema_), resulting in the following:
Describe the solution you'd like
I would like a mechanism that allows renaming of fields, similar to what can be done for the jsonschema via pydantic Config classes. Something like:
class MyModel(AvroBaseModel):
schema_: Schema
class Config:
fields = {"schema_": "schema"}
class Meta:
fields = {"schema_": "schema"}
(or ideally, a single config class that drives both systems so that they always produce equivalent schemas)
Describe alternatives you've considered
I've tried setting schema_name and alias_nested_items, but these set the name of the record, not the name of the field referencing the record.
Additional context
Apologies if there is a mechanism to do this already, I could not find it. Thank you!
The text was updated successfully, but these errors were encountered:
lzulauf
changed the title
Renaming fields in AvroBaseModel
Request: Rename fields in AvroBaseModel
Apr 20, 2023
Is your feature request related to a problem? Please describe.
I would like a way to set the name of fields in AvroBaseModels separate from the field name defined in python.
I'm trying to use AvroBaseModel to represent payloads like the following:
The desired avro and jsonschemas for this look like:
(note that the jsonschema can use a definition reference for the section with
"title": "Schema"
)In order to model this, I might write code like this:
but that would result in the following error from pydantic:
We can fix this by giving the field a different name and then using a pydantic Config fields dictionary to set the name that the schema should use:
This fixes the jsonschema that we get with
MyModel.json_schema()
, but the avro schema we get withMyModel.avro_schema()
still uses the name of the python field (schema_
), resulting in the following:Describe the solution you'd like
I would like a mechanism that allows renaming of fields, similar to what can be done for the jsonschema via pydantic Config classes. Something like:
(or ideally, a single config class that drives both systems so that they always produce equivalent schemas)
Describe alternatives you've considered
I've tried setting
schema_name
andalias_nested_items
, but these set the name of the record, not the name of the field referencing the record.Additional context
Apologies if there is a mechanism to do this already, I could not find it. Thank you!
The text was updated successfully, but these errors were encountered: