diff --git a/src/postgrest/src/postgrest/types.py b/src/postgrest/src/postgrest/types.py index 748f87e4..91b7a144 100644 --- a/src/postgrest/src/postgrest/types.py +++ b/src/postgrest/src/postgrest/types.py @@ -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 @@ -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): diff --git a/src/postgrest/tests/test_types.py b/src/postgrest/tests/test_types.py new file mode 100644 index 00000000..28f508ec --- /dev/null +++ b/src/postgrest/tests/test_types.py @@ -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}, + } + ] + }