From 7f11c1a9911bb04d5098b7de60eb2c15cc41f41d Mon Sep 17 00:00:00 2001 From: laurenceisla Date: Wed, 7 Apr 2021 18:45:13 -0500 Subject: [PATCH] fix: Void functions return null instead of empty body (#1795) * Change empty body reponse to null for scalar functions returning void or null values * Add test for functions returning an integer with null value Co-authored-by: Laurence Isla --- src/PostgREST/Private/QueryFragment.hs | 2 +- test/Feature/RpcSpec.hs | 13 ++++++++++--- test/fixtures/schema.sql | 4 ++++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/PostgREST/Private/QueryFragment.hs b/src/PostgREST/Private/QueryFragment.hs index 839cc6ec0..fbefcc893 100644 --- a/src/PostgREST/Private/QueryFragment.hs +++ b/src/PostgREST/Private/QueryFragment.hs @@ -98,7 +98,7 @@ asJsonF returnsScalar asJsonSingleF :: Bool -> SqlFragment --TODO! unsafe when the query actually returns multiple rows, used only on inserting and returning single element asJsonSingleF returnsScalar - | returnsScalar = "coalesce(string_agg(to_json(_postgrest_t.pgrst_scalar)::text, ','), '')::character varying" + | returnsScalar = "coalesce(string_agg(to_json(_postgrest_t.pgrst_scalar)::text, ','), 'null')::character varying" | otherwise = "coalesce(string_agg(to_json(_postgrest_t)::text, ','), '')::character varying" asBinaryF :: FieldName -> SqlFragment diff --git a/test/Feature/RpcSpec.hs b/test/Feature/RpcSpec.hs index f0f7ba032..052ca437a 100644 --- a/test/Feature/RpcSpec.hs +++ b/test/Feature/RpcSpec.hs @@ -282,11 +282,18 @@ spec actualPgVersion = `shouldRespondWith` [json|{"id": 2}|] - it "returns nothing for void" $ + it "returns null for void" $ post "/rpc/ret_void" [json|{}|] `shouldRespondWith` - "" + "null" + { matchHeaders = [matchContentTypeJson] } + + it "returns null for an integer with null value" $ + post "/rpc/ret_null" + [json|{}|] + `shouldRespondWith` + "null" { matchHeaders = [matchContentTypeJson] } context "different types when overloaded" $ do @@ -919,7 +926,7 @@ spec actualPgVersion = it "can set the same http header twice" $ get "/rpc/set_cookie_twice" `shouldRespondWith` - "" + "null" { matchHeaders = [ matchContentTypeJson , "Set-Cookie" <:> "sessionid=38afes7a8; HttpOnly; Path=/" , "Set-Cookie" <:> "id=a3fWa; Expires=Wed, 21 Oct 2015 07:28:00 GMT; Secure; HttpOnly" ]} diff --git a/test/fixtures/schema.sql b/test/fixtures/schema.sql index 637d0a5e7..4931a654f 100644 --- a/test/fixtures/schema.sql +++ b/test/fixtures/schema.sql @@ -1051,6 +1051,10 @@ $$ language sql; create function test.ret_void() returns void as '' language sql; +create or replace function test.ret_null() returns int as $$ + select null::int; +$$ language sql; + create function test.ret_base64_bin() returns text as $$ select i.img from test.images_base64 i where i.name = 'A.png'; $$ language sql;