jpmorganchase / py-avro-schema

Generate Apache Avro schemas for Python types including standard library data-classes and Pydantic data models.
https://py-avro-schema.readthedocs.io/
Apache License 2.0
37 stars 6 forks source link

Date is converted to INT and not STRING #27

Closed Lazloo closed 2 years ago

Lazloo commented 2 years ago

Can I configure somehow the handling of datetime.date? Here is my use case:

I have a field in my pydantic model called start_date:

import datetime
from aifora.da.schema.customer.models._base import BaseModel
class MyModel(BaseModel):
   start_date: datetime.date 

When I let pydantic generate a JSON-Schema (MyModel.schema_json), this field is represented as string:

    "start_date": {
      "title": "Start Date",
      "type": "string",
      "format": "date"
    },

However, when I apply py-avro-schema to this field is represented as INT:

    {
      "name": "start_date",
      "type": {
        "type": "int",
        "logicalType": "date"
      },
    },

Is there any setting that dates in py-avro-schema are converted to strings?

faph commented 2 years ago

That is as per Avro specification. See https://avro.apache.org/docs/1.10.2/spec.html#Date

JSON does not have native support for dates. It just happens to be a convention to serialize dates as ISO strings.