From e08bb3a197cbc03e5c623b9847651880955e45b8 Mon Sep 17 00:00:00 2001 From: Wolfgang Walther Date: Thu, 8 Oct 2020 22:59:10 +0200 Subject: [PATCH] Fix overloading of functions with unnamed arguments (specifically for prefer params=single-object) --- CHANGELOG.md | 1 + src/PostgREST/DbStructure.hs | 5 ++++- test/Feature/RpcSpec.hs | 13 +++++++++++++ test/fixtures/schema.sql | 8 ++++++++ 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ae3c23d17..33da83cc5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - #1585, Fix error messages on connection failure for localized postgres on Windows - @wolfgangwalther - #1636, Fix `application/octet-stream` appending `charset=utf-8` - @steve-chavez - #1615, Fix RPC return type handling and embedding for domains with composite base type - @wolfgangwalther + - #1469, Fix overloading of functions with unnamed arguments - @wolfgangwalther ### Changed diff --git a/src/PostgREST/DbStructure.hs b/src/PostgREST/DbStructure.hs index 8a7605c0f..ceafd6225 100644 --- a/src/PostgREST/DbStructure.hs +++ b/src/PostgREST/DbStructure.hs @@ -153,6 +153,7 @@ decodeProcs = parseArgs :: Text -> [PgArg] parseArgs = mapMaybe parseArg . filter (not . isPrefixOf "OUT" . toS) . map strip . split (==',') + -- TODO: does parseArg properly handle unnamed "character varying" arguments or arguments with spaces in their names? parseArg :: Text -> Maybe PgArg parseArg arg = let isVariadic = isPrefixOf "VARIADIC " $ toS arg @@ -161,7 +162,9 @@ decodeProcs = (body, def) = breakOn " DEFAULT " argNoMode (name, typ) = breakOn " " body in if T.null typ - then Nothing + -- Handle unnamed args. TODO: refactor to types + then Just $ + PgArg mempty (strip name) (T.null def) isVariadic else Just $ PgArg (dropAround (== '"') name) (strip typ) (T.null def) isVariadic diff --git a/test/Feature/RpcSpec.hs b/test/Feature/RpcSpec.hs index 7a1c4fd28..8fd581ef4 100644 --- a/test/Feature/RpcSpec.hs +++ b/test/Feature/RpcSpec.hs @@ -284,6 +284,19 @@ spec actualPgVersion = [json|null|] { matchHeaders = [matchContentTypeJson] } + context "different types when overloaded" $ do + it "returns composite type" $ + post "/rpc/ret_point_overloaded" [json|{"x": 1, "y": 2}|] `shouldRespondWith` + [json|[{"x": 1, "y": 2}]|] + { matchHeaders = [matchContentTypeJson] } + + it "returns json scalar with prefer single object" $ + request methodPost "/rpc/ret_point_overloaded" [("Prefer","params=single-object")] + [json|{"x": 1, "y": 2}|] + `shouldRespondWith` + [json|{"x": 1, "y": 2}|] + { matchHeaders = [matchContentTypeJson] } + context "proc argument types" $ do -- different syntax for array needed for pg<10 when (actualPgVersion < pgVersion100) $ diff --git a/test/fixtures/schema.sql b/test/fixtures/schema.sql index 4e2882ef4..0fe2c6ee8 100755 --- a/test/fixtures/schema.sql +++ b/test/fixtures/schema.sql @@ -983,6 +983,14 @@ create function test.ret_point_2d() returns test.point_2d as $$ select row(10, 5)::test.point_2d; $$ language sql; +create function test.ret_point_overloaded(x int, y int) returns test.point_2d as $$ + select row(x, y)::test.point_2d; +$$ language sql; + +create function test.ret_point_overloaded(json) returns json as $$ + select $1; +$$ language sql; + -- domains on composite types are only supported from pg 11 on do $do$begin if (SELECT current_setting('server_version_num')::int >= 110000) then