Skip to content
Open
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
Empty file added rath/json/__init__.py
Empty file.
31 changes: 31 additions & 0 deletions rath/json/encoders.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import json
from datetime import datetime
from json import JSONEncoder
from typing import Any

from pydantic import BaseModel


class PydanticJsonEncoder(JSONEncoder):
"""
A JSONEncoder that can serialize pydantic models.
"""
def default(self, o: Any) -> Any:
if isinstance(o, BaseModel):
return o.dict(by_alias=True)
if isinstance(o, datetime):
return o.isoformat("T")
return super().default(o)


def dumps(value: Any):
"""
Serialize an object to a JSON formatted string using PydanticJsonEncoder.

Arguments:
value (Any): The object to serialize.

Returns:
str: The serialized string.
"""
return json.dumps(value, cls=PydanticJsonEncoder)
7 changes: 6 additions & 1 deletion rath/links/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from typing import Any, Dict, List

import aiohttp
from aiohttp.typedefs import JSONEncoder
from graphql import OperationType
from rath.json.encoders import dumps
from pydantic import Field
from rath.operation import GraphQLException, GraphQLResult, Operation
from rath.links.base import AsyncTerminatingLink
Expand All @@ -20,9 +22,12 @@ class AIOHttpLink(AsyncTerminatingLink):
)

_session = None
_json_encoder: JSONEncoder = dumps

async def __aenter__(self) -> None:
self._session = await aiohttp.ClientSession().__aenter__()
self._session = await aiohttp.ClientSession(
json_serialize=self._json_encoder
).__aenter__()

async def __aexit__(self, *args, **kwargs) -> None:
await self._session.__aexit__(*args, **kwargs)
Expand Down
51 changes: 51 additions & 0 deletions tests/test_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from datetime import datetime
from typing import Dict

import pytest
from pydantic import BaseModel, Field

from rath.json.encoders import dumps


class TestInput(BaseModel):
start_time: datetime = Field(alias="startTime")
some_dict: Dict[str, float] = Field(alias="someDict")


@pytest.fixture
def example_input():
return TestInput(
startTime=datetime.utcnow(),
someDict={
"a": 1.0,
"b": 2.0,
},
)


def test_json_dumps(example_input):
"""
Tests that extended json dumps() method does not throw exception when
serializing a pydantic model
:param example_input: Example pydantic model.
"""
dumps(example_input)


def test_json_dumps_throws_for_unknown_type():
"""
Tests that extended json dumps() method still raises TypeError for unknown type
"""
with pytest.raises(TypeError):
dumps(bytes([0x01, 0x02, 0x03, 0x04, 0x05, 0x06]))


def test_json_roundtrip(example_input):
"""
Tests that extended json dumps() method produces output
that matches input on a round trip
:param example_input: Example pydantic model.
"""
json_string = dumps(example_input)
round_trip = TestInput.parse_raw(json_string)
assert example_input == round_trip