From 6c9f9d35f81a3847165fd012f5e6cf965711a0db Mon Sep 17 00:00:00 2001 From: Bruno Gale Date: Mon, 19 Oct 2026 00:35:26 +0200 Subject: [PATCH] feat(postgrest): add strip_nulls() --- sdk-compliance.yaml | 5 ++++- .../src/postgrest/base_request_builder.py | 19 ++++++++++++++++ src/postgrest/tests/test_strip_nulls.py | 22 +++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 src/postgrest/tests/test_strip_nulls.py diff --git a/sdk-compliance.yaml b/sdk-compliance.yaml index 9345a3944..ed5c3236e 100644 --- a/sdk-compliance.yaml +++ b/sdk-compliance.yaml @@ -418,7 +418,10 @@ features: symbols: - AsyncSelectRequestBuilder.maybe_single - SyncSelectRequestBuilder.maybe_single - database.using_modifiers.strip_nulls: not_implemented + database.using_modifiers.strip_nulls: + status: implemented + symbols: + - BaseSelectRequestBuilder.strip_nulls database.using_modifiers.format_csv: status: implemented symbols: diff --git a/src/postgrest/src/postgrest/base_request_builder.py b/src/postgrest/src/postgrest/base_request_builder.py index 02c18c474..3d429d1dc 100644 --- a/src/postgrest/src/postgrest/base_request_builder.py +++ b/src/postgrest/src/postgrest/base_request_builder.py @@ -83,6 +83,7 @@ def __init__( self.json = None if http_method in {"GET", "HEAD"} else json self.auth = auth self.retry_enabled = retry_enabled + self.strip_nulls = False @overload def send( @@ -95,6 +96,14 @@ def send( def send(self: RequestConfig[C], additional_headers: Headers): additional_headers.update(self.headers) + if self.strip_nulls: + accept = additional_headers.get("Accept") + if accept == "application/vnd.pgrst.object+json": + additional_headers["Accept"] = f"{accept};nulls=stripped" + elif accept in (None, "application/json"): + additional_headers["Accept"] = ( + "application/vnd.pgrst.array+json;nulls=stripped" + ) return self.session.request( self.http_method, str(self.path), @@ -674,6 +683,16 @@ def range( ) return self + def strip_nulls(self: Self) -> Self: + """Strip null values from the response data. + + Only available in PostgREST v11.2+. + """ + if self.request.headers.get("Accept") == "text/csv": + raise ValueError("strip_nulls() cannot be used with csv()") + self.request.strip_nulls = True + return self + class BaseRPCRequestBuilder(BaseSelectRequestBuilder): def select( diff --git a/src/postgrest/tests/test_strip_nulls.py b/src/postgrest/tests/test_strip_nulls.py new file mode 100644 index 000000000..378c3dbdb --- /dev/null +++ b/src/postgrest/tests/test_strip_nulls.py @@ -0,0 +1,22 @@ +import pytest +from httpx import Client, MockTransport, Request, Response + +from postgrest import SyncPostgrestClient + + +@pytest.mark.parametrize( + "single, expected", + [ + (False, "application/vnd.pgrst.array+json;nulls=stripped"), + (True, "application/vnd.pgrst.object+json;nulls=stripped"), + ], +) +def test_strip_nulls_sets_accept_header(single: bool, expected: str) -> None: + def handler(request: Request) -> Response: + assert request.headers["Accept"] == expected + return Response(200, json=[]) + + with Client(transport=MockTransport(handler)) as http: + client = SyncPostgrestClient("https://example.com", http_client=http) + query = client.from_("users").select("*").strip_nulls() + (query.single() if single else query).execute()