Repository navigation
fix(postgrest)!: parse responses with json.loads and return scalar RPC results - #2049
Conversation
|
No actionable comments were generated in the recent review. Summary
Change: Bug fix Merge Risk:
|
adbd05e to
87c3da4
Compare
## What `csv()` results now have their own response type, `TextAPIResponse`, instead of being forced into `APIResponse.data` (typed as a list of rows) or returned as a bare `str`. - `TextAPIResponse` holds the CSV body as a `str` in `data` and the row count in `count` when the query asked for one. It is exported from `postgrest`. - `TextRequestBuilder.execute()` returns `TextAPIResponse`. Before, it returned a bare `str` and dropped the count from `Content-Range`. - `rpc(...).csv()` returns the same `TextRequestBuilder` as `csv()` on a table. Since #2049 it returned the text in `RPCResponse.data`, typed as `JSON`. That special case in `RPCFilterRequestBuilder.execute()` is removed, and the `RPCResponse` docstring and `MIGRATION.md` now point to `TextAPIResponse`. - `csv()` moved from `BaseRPCRequestBuilder` to `RPCFilterRequestBuilder`, and `sdk-compliance.yaml` now lists the new symbols. - A CSV body that is not valid UTF-8 raises `APIError` with the response, like an invalid JSON body does. - Two `rpc()` overloads now return the builder `rpc()` actually returns. `count=..., head=True` returns `RPCCountRequestBuilder` (it claimed `RPCFilterRequestBuilder`), and `count=..., get=False` returns `RPCFilterRequestBuilder` (it claimed a union with `RPCCountRequestBuilder`). Without this, `.csv()` after those calls would have been accepted by the type checker and failed at runtime, or rejected although it works. ## Breaking changes - `.csv().execute()` returns `TextAPIResponse` instead of `str`. Read the text from `response.data`. - Filters, modifiers and `strip_nulls()` can no longer follow `rpc(...).csv()`. They go before `csv()`, as they already did on a table. `strip_nulls()` after `csv()` already raised `ValueError`. `MIGRATION.md` has a new section with a type-checked example. ## Tests - New `tests/test_text_response.py` covers table `csv()` with a count, sync and async RPC `csv()`, a non-UTF-8 body, and the corrected `rpc()` overload types (with `assert_type`) against a mock transport. - Added `TextAPIResponse.from_http_request_response` unit tests to the sync and async request builder tests. - The CSV integration tests now assert on `res.data`. - Removed the RPC `csv()` then `strip_nulls()` test, since that chain no longer type-checks. The `PostgrestRequest` test still covers the `ValueError`. Linear: SDK-1706
What
Postgrest responses are now parsed with
json.loadsplus a shape check instead of a pydanticTypeAdapter, andrpc().execute()returns whatever JSON value the function produced.Why
TypeAdapter(list[dict[str, JSON]])parse was slow. On 10,000 rows it took 21.2 ms against 3.7 ms forjson.loadswith the shape check. v2 made the same switch in perf(postgrest): speed up JSON parsing by 22x using json.loads #1571.1or"text") or a single row (body{...}) failed validation and raisedAPIErrorwith code"200", even though the call succeeded.Changes
postgrest/_parsing.pywithparse_json,parse_rowsandparse_object. They decode withjson.loadsand raiseInvalidResponseBodyErrorwhen the body is not JSON (including bodies nested too deeply forjson.loads) or not in the expected shape.APIResponse,SingleAPIResponseand the table executor use these helpers. Callers catchInvalidResponseBodyErrorinstead of pydantic'sValidationErrorand turn it intoAPIErroras before. Other errors, such as a malformedContent-Range, are not swallowed.RPCResponsedataclass withdata: JSONandcount.RPCFilterRequestBuilder.execute()returns it instead ofAPIResponse. An empty body givesdata=None, andcsv()on an RPC gives the text, where it used to fail on JSON parsing.RPCResponseis exported frompostgrest, exported fromsupabaseasPostgrestRPCResponse, and registered insdk-compliance.yamlunderdatabase.query.rpc.MIGRATION.mddocuments theRPCResponsechange.count_stored_countries(bigint),country_iso(text) andstored_country(a single row).Behavior change
rpc(...).execute()now returnsRPCResponserather thanAPIResponse. At runtimedatais the same for set returning functions. For type checkers,dataisJSONinstead oflist[dict[str, JSON]]. A void function now givesdata=Noneinstead of[].Tests
RPCResponsewith sync and async clients: numbers, strings, booleans, null, objects, rows, arrays of scalars and empty bodies. A non JSON success body still raisesAPIError.csv()on an RPC returns the text.singlegetting an array, invalid JSON).SDK-2826
Fixes SDK-2975