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

Field with Optional[Enum] does not have correct Avro representation after using .avro_schema() #229

Closed
bartpijn opened this issue Jan 5, 2023 · 0 comments
Assignees

Comments

@bartpijn
Copy link

bartpijn commented Jan 5, 2023

Describe the bug
When describing a (pydantic) dataclass with a field that either is None or some Enum-value, the generated avro schema for this field does not have the correct types.

To Reproduce
Consider the following data model:

from enum import Enum
from typing import Optional

from dataclasses_avroschema.avrodantic import AvroBaseModel


class Locale(str, Enum):
    en_us = 'en_us'
    de_de = 'de_de'


class Translation(AvroBaseModel):
    locale: Locale
    value: str
    localization_source: Optional[str]
    original_locale: Optional[Locale]


print(Translation.avro_schema())

The print:

{
  "type": "record",
  "name": "Translation",
  "fields": [
    {
      "name": "locale",
      "type": {
        "type": "enum",
        "name": "locale",
        "symbols": [
          "en_us",
          "de_de"
        ]
      }
    },
    {
      "name": "value",
      "type": "string"
    },
    {
      "name": "localization_source",
      "type": [
        "null",
        "string"
      ],
      "default": null
    },
    {
      "name": "original_locale",
      "type": {
        "type": "enum",
        "name": "original_locale",
        "symbols": [
          "en_us",
          "de_de"
        ]
      },
      "default": null
    }
  ]
}

Expected behavior
Expected output:

{
  "type": "record",
  "name": "Translation",
  "fields": [
    {
      "name": "locale",
      "type": {
        "type": "enum",
        "name": "locale",
        "symbols": [
          "en_us",
          "de_de"
        ]
      }
    },
    {
      "name": "value",
      "type": "string"
    },
    {
      "name": "localization_source",
      "type": [
        "null",
        "string"
      ],
      "default": null
    },
    {
      "name": "original_locale",
      "type": {
        "type": [
          "null", \\ THIS IS THE CHANGE
          "enum"
        ],
        "name": "original_locale",
        "symbols": [
          "en_us",
          "de_de"
        ]
      },
      "default": null
    }
  ]
}

My environment
Python 3.9.15
Pydantic 1.10.2
dataclasses-avroschema 0.34.3
avro 1.11.1

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

2 participants