diff --git a/CHANGELOG.md b/CHANGELOG.md index 93e63d576..2637b6a36 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - #945, Fix slow start-up time on big schemas - @steve-chavez - #1129, Fix view embedding when table is capitalized - @steve-chavez - #1149, OpenAPI: Change `GET` response type to array - @laughedelic +- #1152, Fix RPC failing when having arguments with reserved or uppercase keywords - @mdr1384 ### Changed diff --git a/src/PostgREST/QueryBuilder.hs b/src/PostgREST/QueryBuilder.hs index 8045573bc..302251c6b 100644 --- a/src/PostgREST/QueryBuilder.hs +++ b/src/PostgREST/QueryBuilder.hs @@ -173,9 +173,9 @@ callProc qi pgArgs returnsScalar selectQuery countQuery countTotal isSingle para unwords [ "_args_record AS (", "SELECT * FROM " <> (if isObject then "json_to_record" else "json_to_recordset") <> "($1)", - "AS _(" <> intercalate ", " ((\a -> pgaName a <> " " <> pgaType a) <$> pgArgs) <> ")", + "AS _(" <> intercalate ", " ((\a -> pgFmtIdent (pgaName a) <> " " <> pgaType a) <$> pgArgs) <> ")", ")"] - , intercalate ", " ((\a -> pgaName a <> " := (SELECT " <> pgaName a <> " FROM _args_record)") <$> pgArgs)) + , intercalate ", " ((\a -> pgFmtIdent (pgaName a) <> " := (SELECT " <> pgFmtIdent (pgaName a) <> " FROM _args_record)") <$> pgArgs)) countResultF = if countTotal then "( "<> countQuery <> ")" else "null::bigint" :: Text _procName = qiName qi responseHeaders = diff --git a/test/Feature/RpcSpec.hs b/test/Feature/RpcSpec.hs index d808115ee..3e81f220f 100644 --- a/test/Feature/RpcSpec.hs +++ b/test/Feature/RpcSpec.hs @@ -78,6 +78,16 @@ spec = get "/rpc/sayhello" `shouldRespondWith` 404 get "/rpc/sayhello?any_arg=value" `shouldRespondWith` 404 + it "works when having uppercase identifiers" $ do + get "/rpc/quotedFunction?user=mscott&fullName=Michael Scott&SSN=401-32-XXXX" `shouldRespondWith` + [json|{"user": "mscott", "fullName": "Michael Scott", "SSN": "401-32-XXXX"}|] + { matchHeaders = [matchContentTypeJson] } + post "/rpc/quotedFunction" + [json|{"user": "dschrute", "fullName": "Dwight Schrute", "SSN": "030-18-XXXX"}|] + `shouldRespondWith` + [json|{"user": "dschrute", "fullName": "Dwight Schrute", "SSN": "030-18-XXXX"}|] + { matchHeaders = [matchContentTypeJson] } + context "shaping the response returned by a proc" $ do it "returns a project" $ do post "/rpc/getproject" [json| { "id": 1} |] `shouldRespondWith` diff --git a/test/fixtures/schema.sql b/test/fixtures/schema.sql index e4bb32762..ff7fd2456 100755 --- a/test/fixtures/schema.sql +++ b/test/fixtures/schema.sql @@ -1570,3 +1570,8 @@ comment on materialized view materialized_projects is $$A materialized view for projects Just a test for materialized views$$; + +create or replace function test."quotedFunction"("user" text, "fullName" text, "SSN" text) +returns jsonb AS $$ + select format('{"user": "%s", "fullName": "%s", "SSN": "%s"}', "user", "fullName", "SSN")::jsonb; +$$ language sql;