Fix POST/PATCH error on a [{}] json

This commit is contained in:
steve-chavez
2019-02-26 11:38:21 -05:00
committed by Steve Chávez
parent 16059ad470
commit 50509b52b8
2 changed files with 25 additions and 15 deletions
+5 -6
View File
@@ -268,7 +268,7 @@ requestToQuery schema isParent (DbRead (Node (Select colSelects tbl tblAlias imp
--getQueryParts is not total but requestToQuery is called only after addJoinConditions which ensures the only --getQueryParts is not total but requestToQuery is called only after addJoinConditions which ensures the only
--posible relations are Child Parent Many --posible relations are Child Parent Many
getQueryParts _ _ = witness getQueryParts _ _ = witness
requestToQuery schema _ (DbMutate (Insert mainTbl p@(PayloadJSON _ _ pKeys) onConflct putConditions returnings)) = requestToQuery schema _ (DbMutate (Insert mainTbl (PayloadJSON _ _ pKeys) onConflct putConditions returnings)) =
unwords [ unwords [
"WITH payload AS (SELECT $1::json AS json_data),", "WITH payload AS (SELECT $1::json AS json_data),",
"vals AS (", "vals AS (",
@@ -276,7 +276,7 @@ requestToQuery schema _ (DbMutate (Insert mainTbl p@(PayloadJSON _ _ pKeys) onCo
"SELECT json_data AS val FROM payload WHERE json_typeof(json_data) = 'array'", "SELECT json_data AS val FROM payload WHERE json_typeof(json_data) = 'array'",
"UNION ALL", "UNION ALL",
"SELECT json_build_array(json_data) AS val FROM payload WHERE json_typeof(json_data) = 'object')"], "SELECT json_build_array(json_data) AS val FROM payload WHERE json_typeof(json_data) = 'object')"],
"INSERT INTO ", fromQi qi, if payloadIsEmpty then " " else "(" <> cols <> ")", "INSERT INTO ", fromQi qi, if S.null pKeys then " " else "(" <> cols <> ")",
unwords [ unwords [
"SELECT " <> cols <> " FROM", "SELECT " <> cols <> " FROM",
"json_populate_recordset", "(null::", fromQi qi, ", (select val from vals)) _", "json_populate_recordset", "(null::", fromQi qi, ", (select val from vals)) _",
@@ -293,10 +293,9 @@ requestToQuery schema _ (DbMutate (Insert mainTbl p@(PayloadJSON _ _ pKeys) onCo
where where
qi = QualifiedIdentifier schema mainTbl qi = QualifiedIdentifier schema mainTbl
cols = intercalate ", " $ pgFmtIdent <$> S.toList pKeys cols = intercalate ", " $ pgFmtIdent <$> S.toList pKeys
payloadIsEmpty = pjIsEmpty p requestToQuery schema _ (DbMutate (Update mainTbl (PayloadJSON _ _ keys) logicForest returnings)) =
requestToQuery schema _ (DbMutate (Update mainTbl p@(PayloadJSON _ _ keys) logicForest returnings)) = if S.null keys
if pjIsEmpty p then "WITH " <> ignoredBody <> "SELECT null WHERE false" -- if there are no columns we cannot do UPDATE table SET {empty}, it'd be invalid syntax
then "WITH " <> ignoredBody <> "SELECT ''"
else else
unwords [ unwords [
"WITH payload AS (SELECT $1::json AS json_data),", "WITH payload AS (SELECT $1::json AS json_data),",
+20 -9
View File
@@ -222,12 +222,23 @@ spec = do
, matchHeaders = ["Location" <:> location] , matchHeaders = ["Location" <:> location]
} }
context "empty object" $ context "empty objects" $ do
it "successfully populates table with all-default columns" $ it "successfully inserts a row with all-default columns" $ do
post "/items" "{}" `shouldRespondWith` "" post "/items" "{}" `shouldRespondWith` ""
{ matchStatus = 201 { matchStatus = 201
, matchHeaders = [] , matchHeaders = []
} }
post "/items" "[{}]" `shouldRespondWith` ""
{ matchStatus = 201
, matchHeaders = []
}
it "successfully inserts two rows with all-default columns" $
post "/items" "[{}, {}]" `shouldRespondWith` ""
{ matchStatus = 201
, matchHeaders = []
}
context "table with limited privileges" $ do context "table with limited privileges" $ do
it "succeeds if correct select is applied" $ it "succeeds if correct select is applied" $
request methodPost "/limited_article_stars?select=article_id,user_id" [("Prefer", "return=representation")] request methodPost "/limited_article_stars?select=article_id,user_id" [("Prefer", "return=representation")]
@@ -427,20 +438,20 @@ spec = do
matchHeaders = ["Content-Range" <:> "*/*"] matchHeaders = ["Content-Range" <:> "*/*"]
} }
get "/items" `shouldRespondWith` request methodPatch "/items" [] [json| [{}] |]
[json|[{"id":3},{"id":4},{"id":5},{"id":6},{"id":7},{"id":8},{"id":9},{"id":10},{"id":11},{"id":12},{"id":13},{"id":14},{"id":15},{id:16},{"id":2},{"id":1}]|] `shouldRespondWith` ""
{ matchHeaders = [matchContentTypeJson] } {
matchStatus = 204,
matchHeaders = ["Content-Range" <:> "*/*"]
}
it "makes no updates and and returns 200, when patching with an empty json object and return=rep" $ do it "makes no updates and and returns 200, when patching with an empty json object and return=rep" $
request methodPatch "/items" [("Prefer", "return=representation")] [json| {} |] request methodPatch "/items" [("Prefer", "return=representation")] [json| {} |]
`shouldRespondWith` "[]" `shouldRespondWith` "[]"
{ {
matchStatus = 200, matchStatus = 200,
matchHeaders = ["Content-Range" <:> "*/*"] matchHeaders = ["Content-Range" <:> "*/*"]
} }
get "/items" `shouldRespondWith`
[json| [{"id":3},{"id":4},{"id":5},{"id":6},{"id":7},{"id":8},{"id":9},{"id":10},{"id":11},{"id":12},{"id":13},{"id":14},{"id":15},{id:16},{"id":2},{"id":1}] |]
{ matchHeaders = [matchContentTypeJson] }
context "with unicode values" $ context "with unicode values" $
it "succeeds and returns values intact" $ do it "succeeds and returns values intact" $ do