Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request: Rename fields in AvroBaseModel #293

Closed
lzulauf opened this issue Apr 19, 2023 · 2 comments
Closed

Request: Rename fields in AvroBaseModel #293

lzulauf opened this issue Apr 19, 2023 · 2 comments

Comments

@lzulauf
Copy link

lzulauf commented Apr 19, 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:

{
    "schema": {
        "value": "my_schema_id"
    }
}

The desired avro and jsonschemas for this look like:

desired_avroschema = {
  "type": "record",
  "name": "MyModel",
  "fields": [
    {
      "name": "schema",
      "type": {
        "type": "record",
        "name": "Schema",
        "fields": [
          {
            "name": "value",
            "type": "string"
          }
        ]
      }
    }
  ]
}

desired_jsonschema = {
  "title": "MyModel",
  "type": "object",
  "properties": {
    "schema": {
      "title": "Schema",
      "type": "object",
      "properties": {
        "value": {
          "title": "Value",
          "type": "string"
        }
      },
      "required": [
        "value"
      ]
    }
  },
  "required": [
    "schema"
  ]
}

(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:

{
  "type": "record",
  "name": "MyModel",
  "fields": [
    {
      "name": "schema_",
      "type": {
        "type": "record",
        "name": "schema",
        "fields": [
          {
            "name": "value",
            "type": "string"
          }
        ]
      }
    }
  ]
}

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!

@lzulauf lzulauf changed the title Renaming fields in AvroBaseModel Request: Rename fields in AvroBaseModel Apr 20, 2023
@marcosschroh
Copy link
Owner

Hi @lzulauf

I will try to fix this asap

@kevinjacobs-delfi
Copy link
Contributor

FYI, Pydantic allows fields to be renamed using alias or serialization_alias field metadata.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants