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
3 changes: 2 additions & 1 deletion src/postgrest/src/postgrest/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from httpx import AsyncClient, BasicAuth, Client, Headers, QueryParams
from pydantic import TypeAdapter
from pydantic.types import JsonValue
from typing_extensions import TypeAliasType
from yarl import URL

Expand All @@ -18,7 +19,7 @@
JSON = TypeAliasType(
"JSON", "Union[None, bool, str, int, float, Sequence[JSON], Mapping[str, JSON]]"
)
JSONAdapter: TypeAdapter = TypeAdapter(JSON)
JSONAdapter: TypeAdapter = TypeAdapter(JsonValue)


class CountMethod(StrEnum):
Expand Down
25 changes: 25 additions & 0 deletions src/postgrest/tests/test_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from pydantic import TypeAdapter
from pydantic.types import JsonValue

from postgrest.types import JSONAdapter


def test_json_adapter_schema_matches_pydantic_json_value():
assert JSONAdapter.core_schema == TypeAdapter(JsonValue).core_schema


def test_json_adapter_validates_nested_json_bytes():
payload = (
b'{"items":[{"id":1,"tags":["a","b"],'
b'"meta":{"score":0.9,"active":true,"note":null}}]}'
)

assert JSONAdapter.validate_json(payload) == {
"items": [
{
"id": 1,
"tags": ["a", "b"],
"meta": {"score": 0.9, "active": True, "note": None},
}
]
}