From 3d14093ceadba3deb510a2d13f44225f31625405 Mon Sep 17 00:00:00 2001 From: Lukas Klingsbo Date: Mon, 2 Nov 2026 14:26:32 +0100 Subject: [PATCH] fix(postgrest): json-encode dictionary rpc arguments in get and head requests Co-authored-by: Kayvan Zahiri <123409429+Kayvan-Zahiri@users.noreply.github.com> --- src/postgrest/src/postgrest/utils.py | 6 ++- src/postgrest/tests/_async/test_client.py | 48 +++++++++++++++++++++++ src/postgrest/tests/_sync/test_client.py | 48 +++++++++++++++++++++++ 3 files changed, 101 insertions(+), 1 deletion(-) diff --git a/src/postgrest/src/postgrest/utils.py b/src/postgrest/src/postgrest/utils.py index 8bd644f06..1bddf0780 100644 --- a/src/postgrest/src/postgrest/utils.py +++ b/src/postgrest/src/postgrest/utils.py @@ -1,5 +1,6 @@ from __future__ import annotations +import json from collections.abc import Iterable from typing import Any, TypeVar @@ -78,10 +79,13 @@ def rpc_query_param(value: Any) -> Any: """Format an RPC argument sent in the query string, as in GET or HEAD calls. Lists and tuples become PostgreSQL array literals, since PostgREST keeps only - one value when a non-variadic argument is repeated. + one value when a non-variadic argument is repeated. Dictionaries become JSON + for named json/jsonb arguments. """ if isinstance(value, (list, tuple)): return sanitize_array_param(value) + if isinstance(value, dict): + return json.dumps(value, separators=(",", ":")) return value diff --git a/src/postgrest/tests/_async/test_client.py b/src/postgrest/tests/_async/test_client.py index 1a3ecbf0f..5c60e522e 100644 --- a/src/postgrest/tests/_async/test_client.py +++ b/src/postgrest/tests/_async/test_client.py @@ -1,3 +1,4 @@ +import json import re from collections.abc import AsyncIterator from typing import Any @@ -10,6 +11,7 @@ BasicAuth, Headers, Limits, + MockTransport, Request, Response, Timeout, @@ -257,3 +259,49 @@ def test_rpc_query_params_quote_array_elements( assert builder.request.params.get_list("tags") == [ '{"a,b","c d","","say \\"hi\\"","x}","null",NULL}' ] + + +@pytest.mark.parametrize("method", ["GET", "HEAD", "POST"]) +@pytest.mark.parametrize( + ("options", "encoded"), + [ + ({"enabled": True}, '{"enabled":true}'), + ( + {"nested": {"items": [1, None, "a&b"]}}, + '{"nested":{"items":[1,null,"a&b"]}}', + ), + ({}, "{}"), + ], +) +async def test_rpc_serializes_dictionary_arguments( + method: str, options: dict, encoded: str +) -> None: + requests: list[Request] = [] + + def handler(request: Request) -> Response: + requests.append(request) + return Response(200, json=[]) + + params: Any = { + "options": options, + "raw": '{"preserved": true}', + "ids": [1, 2], + "text": "keep", + } + async with AsyncClient(transport=MockTransport(handler)) as http: + client = AsyncPostgrestClient("https://example.com", http_client=http) + await client.rpc( + "example", params, get=method == "GET", head=method == "HEAD" + ).execute() + + assert len(requests) == 1 + request = requests[0] + assert request.method == method + if method == "POST": + assert json.loads(request.content) == params + assert "options" not in request.url.params + else: + assert request.url.params.get_list("options") == [encoded] + assert request.url.params["raw"] == params["raw"] + assert request.url.params["ids"] == "{1,2}" + assert request.url.params["text"] == "keep" diff --git a/src/postgrest/tests/_sync/test_client.py b/src/postgrest/tests/_sync/test_client.py index 38f865647..59e22e5b1 100644 --- a/src/postgrest/tests/_sync/test_client.py +++ b/src/postgrest/tests/_sync/test_client.py @@ -1,3 +1,4 @@ +import json import re from collections.abc import Iterator from typing import Any @@ -10,6 +11,7 @@ Headers, HTTPTransport, Limits, + MockTransport, Request, Response, Timeout, @@ -253,3 +255,49 @@ def test_rpc_query_params_quote_array_elements( assert builder.request.params.get_list("tags") == [ '{"a,b","c d","","say \\"hi\\"","x}","null",NULL}' ] + + +@pytest.mark.parametrize("method", ["GET", "HEAD", "POST"]) +@pytest.mark.parametrize( + ("options", "encoded"), + [ + ({"enabled": True}, '{"enabled":true}'), + ( + {"nested": {"items": [1, None, "a&b"]}}, + '{"nested":{"items":[1,null,"a&b"]}}', + ), + ({}, "{}"), + ], +) +def test_rpc_serializes_dictionary_arguments( + method: str, options: dict, encoded: str +) -> None: + requests: list[Request] = [] + + def handler(request: Request) -> Response: + requests.append(request) + return Response(200, json=[]) + + params: Any = { + "options": options, + "raw": '{"preserved": true}', + "ids": [1, 2], + "text": "keep", + } + with Client(transport=MockTransport(handler)) as http: + client = SyncPostgrestClient("https://example.com", http_client=http) + client.rpc( + "example", params, get=method == "GET", head=method == "HEAD" + ).execute() + + assert len(requests) == 1 + request = requests[0] + assert request.method == method + if method == "POST": + assert json.loads(request.content) == params + assert "options" not in request.url.params + else: + assert request.url.params.get_list("options") == [encoded] + assert request.url.params["raw"] == params["raw"] + assert request.url.params["ids"] == "{1,2}" + assert request.url.params["text"] == "keep"