Skip to content

Commit

Permalink
Allow to search for primary keys on objects with different keys in th…
Browse files Browse the repository at this point in the history
…e body
  • Loading branch information
laurenceisla committed Jul 8, 2022
1 parent 4dd5086 commit 1a3e8e2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/PostgREST/Request/ApiRequest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ apiRequest conf@AppConfig{..} dbStructure req reqBody queryparams@QueryParams{..
payload :: Either ByteString Payload
payload = case (contentMediaType, isTargetingProc) of
(MTApplicationJSON, _) ->
if isJust columns
if isJust columns || method == "DELETE" -- For DELETES, always searches for primary keys only
then Right $ RawJSON reqBody
else note "All object keys must match" . payloadAttributes reqBody
=<< if LBS.null reqBody && isTargetingProc
Expand Down
2 changes: 1 addition & 1 deletion src/PostgREST/Request/DbRequestBuilder.hs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ mutateRequest mutation schema tName ApiRequest{..} pkCols readReq = mapLeft ApiR
null qsRanges &&
body == mempty
then Left $ NoBodyFilterLimit "delete"
else
else
Right $ Delete qi body combinedLogic pkCols iTopLevelRange rootOrder returnings

where
Expand Down
36 changes: 30 additions & 6 deletions test/spec/Feature/Query/DeleteSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -627,21 +627,45 @@ spec =
, { "id": 3, "name": "item-3" }
]|]
`shouldRespondWith`
[json| {"message":"All object keys must match","code":"PGRST102","hint":null,"details":null} |]
[json|{
"code":"22023",
"hint":null,
"details":null,
"message":"argument of json_populate_recordset must be an array of objects"
}|]
{ matchStatus = 400
, matchHeaders = [matchContentTypeJson]
}

it "rejects a json array that has objects with different keys" $
it "works with a json array that has objects with different keys" $ do
get "/bulk_delete_items_cpk"
`shouldRespondWith`
[json|[
{ "id": 1, "name": "item-1", "observation": null }
, { "id": 2, "name": "item-2", "observation": null }
, { "id": 3, "name": "item-3", "observation": null }
]|]

request methodDelete "/bulk_delete_items_cpk"
[("Prefer", "tx=commit"), ("Prefer", "count=exact")]
[json|[
{ "id": 1, "name": "item-1" }
, { "id": 2 }
, { "id": 3, "name": "item-3" }
, { "other": "value" }
, { "id": 3, "name": "item-3", "observation": null }
]|]
`shouldRespondWith`
[json| {"message":"All object keys must match","code":"PGRST102","hint":null,"details":null} |]
{ matchStatus = 400
, matchHeaders = [matchContentTypeJson]
""
{ matchStatus = 204
, matchHeaders = ["Content-Range" <:> "*/2"]
}

get "/bulk_delete_items_cpk?order=id"
`shouldRespondWith`
[json|[ { "id": 2, "name": "item-2", "observation": null } ]|]

request methodPost "/rpc/reset_items_tables"
[("Prefer", "tx=commit")]
[json| {"tbl_name": "bulk_delete_items_cpk"} |]
`shouldRespondWith` ""
{ matchStatus = 204 }

0 comments on commit 1a3e8e2

Please sign in to comment.