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

feat: allow to set an orjson default (#64) #66

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/py_avro_schema/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"""

import importlib.metadata
from typing import Optional, Type
from typing import Any, Callable, Optional, Type, Union

import memoization
import orjson
Expand All @@ -50,6 +50,7 @@ def generate(
*,
namespace: Optional[str] = None,
options: Option = Option(0),
orjson_default: Union[Callable[[Any], Any], None] = None,
) -> bytes:
"""
Return an Avro schema as a JSON-formatted bytestring for a given Python class or instance
Expand All @@ -60,11 +61,12 @@ def generate(
:param namespace: The Avro namespace to add to schemas.
:param options: Schema generation options as defined by :class:`Option` enum values. Specify multiple values like
this: ``Option.INT_32 | Option.FLOAT_32``.
:param orjson_default: A function to serialize custom types with orjson.
"""
schema_dict = schema(py_type, namespace=namespace, options=options)
json_options = 0
for opt in JSON_OPTIONS:
if opt in options:
json_options |= opt.value
schema_json = orjson.dumps(schema_dict, option=json_options)
schema_json = orjson.dumps(schema_dict, option=json_options, default=orjson_default)
return schema_json
42 changes: 42 additions & 0 deletions tests/test_avro_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
# specific language governing permissions and limitations under the License.

import dataclasses
from typing import Any

import avro.schema
import orjson
import pydantic

import py_avro_schema as pas

Expand Down Expand Up @@ -43,3 +45,43 @@ class PyType:
json_data = pas.generate(PyType)
assert json_data == orjson.dumps(expected)
assert avro.schema.parse(json_data)


def test_pydantic_field_default():
class Default(pydantic.BaseModel):
"""My Default"""

foo: str = "foo"

class PyType(pydantic.BaseModel):
"""My PyType"""

default: Default = pydantic.Field(default_factory=Default)

def pydantic_serializer(value: Any) -> dict:
if isinstance(value, pydantic.BaseModel):
return value.model_dump(mode="json")
raise TypeError

expected = {
"type": "record",
"name": "PyType",
"fields": [
{
"name": "default",
"type": {
"type": "record",
"name": "Default",
"fields": [{"name": "foo", "type": "string", "default": "foo"}],
"namespace": "test_avro_schema",
"doc": "My Default",
},
"default": {"foo": "foo"},
}
],
"namespace": "test_avro_schema",
"doc": "My PyType",
}
json_data = pas.generate(PyType, orjson_default=pydantic_serializer)
assert json_data == orjson.dumps(expected)
assert avro.schema.parse(json_data)
Loading