From 3af63cb94ce68f49b7dcd2ee19df20318d7cff44 Mon Sep 17 00:00:00 2001 From: steve-chavez Date: Tue, 19 Dec 2023 16:46:56 -0500 Subject: [PATCH] fix: empty row on handler function Closes https://github.com/PostgREST/postgrest/issues/3126 --- CHANGELOG.md | 1 + src/PostgREST/Query/Statements.hs | 3 ++- test/spec/Feature/Query/CustomMediaSpec.hs | 7 +++++++ test/spec/fixtures/schema.sql | 5 +++++ 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 138f054ff0..aa1eb5bfd3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Fixed - #3124, Fix table's media type handlers not working for all schemas - @steve-chavez + - #3126, Fix empty row on media type handler function - @steve-chavez ## [12.0.1] - 2023-12-12 diff --git a/src/PostgREST/Query/Statements.hs b/src/PostgREST/Query/Statements.hs index 2c6d7d28e3..77947d04cd 100644 --- a/src/PostgREST/Query/Statements.hs +++ b/src/PostgREST/Query/Statements.hs @@ -157,7 +157,8 @@ preparePlanRows countQuery = standardRow :: Bool -> HD.Row ResultSet standardRow noLocation = RSStandard <$> nullableColumn HD.int8 <*> column HD.int8 - <*> (if noLocation then pure mempty else fmap splitKeyValue <$> arrayColumn HD.bytea) <*> column HD.bytea + <*> (if noLocation then pure mempty else fmap splitKeyValue <$> arrayColumn HD.bytea) + <*> (fromMaybe mempty <$> nullableColumn HD.bytea) <*> nullableColumn HD.bytea <*> nullableColumn HD.text <*> nullableColumn HD.int8 diff --git a/test/spec/Feature/Query/CustomMediaSpec.hs b/test/spec/Feature/Query/CustomMediaSpec.hs index 0c4de99cdd..c5c1786040 100644 --- a/test/spec/Feature/Query/CustomMediaSpec.hs +++ b/test/spec/Feature/Query/CustomMediaSpec.hs @@ -119,6 +119,13 @@ spec = describe "custom media types" $ do , matchHeaders = ["Content-Type" <:> "application/json; charset=utf-8"] } + it "should not fail when the function doesn't return a row" $ do + request methodGet "/rpc/get_line?id=777" (acceptHdrs "application/vnd.twkb") "" + `shouldRespondWith` "" + { matchStatus = 200 + , matchHeaders = ["Content-Type" <:> "application/vnd.twkb"] + } + context "Proc that returns scalar based on a table" $ do it "can get an image with Accept: image/png" $ do r <- request methodGet "/rpc/ret_image" (acceptHdrs "image/png") "" diff --git a/test/spec/fixtures/schema.sql b/test/spec/fixtures/schema.sql index 8d268dcc5a..33f2eefc5b 100644 --- a/test/spec/fixtures/schema.sql +++ b/test/spec/fixtures/schema.sql @@ -3517,6 +3517,11 @@ returns setof test.lines as $$ select * from lines; $$ language sql; +create or replace function test.get_line (id int) +returns "application/vnd.twkb" as $$ + select extensions.st_astwkb(geom)::"application/vnd.twkb" from lines where id = get_line.id; +$$ language sql; + create or replace function test.get_shop_bles () returns setof test.shop_bles as $$ select * from shop_bles;