From 8baf5a5feeebead2dbf14a07c2d5be55867f00ee Mon Sep 17 00:00:00 2001 From: snoopuppy582 Date: Tue, 12 May 2026 10:09:21 +0900 Subject: [PATCH 1/2] fix: speed up postgrest JSON validation --- src/postgrest/src/postgrest/types.py | 3 ++- src/postgrest/tests/test_types.py | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 src/postgrest/tests/test_types.py 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..1826429c --- /dev/null +++ b/src/postgrest/tests/test_types.py @@ -0,0 +1,24 @@ +from postgrest.types import JSONAdapter + + +def test_json_adapter_uses_pydantic_json_value_schema(): + assert JSONAdapter.core_schema["schema"]["schema_ref"].startswith( + "pydantic.types.JsonValue:" + ) + + +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}, + } + ] + } From e7449eb5581894410782ff0926536889b454dcc6 Mon Sep 17 00:00:00 2001 From: snoopuppy582 Date: Wed, 13 May 2026 22:12:16 +0900 Subject: [PATCH 2/2] test: compare JSON adapter schema --- src/postgrest/tests/test_types.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/postgrest/tests/test_types.py b/src/postgrest/tests/test_types.py index 1826429c..28f508ec 100644 --- a/src/postgrest/tests/test_types.py +++ b/src/postgrest/tests/test_types.py @@ -1,10 +1,11 @@ +from pydantic import TypeAdapter +from pydantic.types import JsonValue + from postgrest.types import JSONAdapter -def test_json_adapter_uses_pydantic_json_value_schema(): - assert JSONAdapter.core_schema["schema"]["schema_ref"].startswith( - "pydantic.types.JsonValue:" - ) +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():