Skip to content

Commit 1682677

Browse files
committed
fix: filtering the returned representation whenn using or/and filters on mutations
1 parent 21b8c34 commit 1682677

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ All notable changes to this project will be documented in this file. From versio
1414
### Fixed
1515

1616
- Ensure Listener connections are released by @mkleczek in #4614
17+
- Fix incorrectly filtering the returned representation for PATCH requests when using `or/and` filters by @laurenceisla in #3707
1718

1819
## [14.3] - 2026-01-03
1920

src/PostgREST/Plan.hs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -963,10 +963,17 @@ addRanges ApiRequest{..} rReq =
963963

964964
addLogicTrees :: ResolverContext -> ApiRequest -> ReadPlanTree -> Either Error ReadPlanTree
965965
addLogicTrees ctx ApiRequest{..} rReq =
966-
foldr addLogicTreeToNode (Right rReq) qsLogic
966+
foldr addLogicTreeToNode (Right rReq) logic
967967
where
968968
QueryParams.QueryParams{..} = iQueryParams
969969

970+
logic =
971+
case iAction of
972+
ActDb (ActRelationRead _ _) -> qsLogic
973+
ActDb (ActRoutine _ _) -> qsLogic
974+
-- For mutations, take the non-root logic filters. These will only affect the embeddings and not the top level of the returned representation.
975+
_ -> filter (not . null . fst) qsLogic
976+
970977
addLogicTreeToNode :: (EmbedPath, LogicTree) -> Either Error ReadPlanTree -> Either Error ReadPlanTree
971978
addLogicTreeToNode = updateNode (\t (Node q@ReadPlan{from=fromTable, where_=lf} f) -> Node q{ReadPlan.where_=resolveLogicTree ctx{qi=fromTable} t:lf} f)
972979

test/spec/Feature/Query/AndOrParamsSpec.hs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,21 +252,39 @@ spec =
252252
[json|[{"id": 7, "entities":null}, {"id": 8, "entities": {"id": 2}}, {"id": 9, "entities": {"id": 3}}]|]
253253
{ matchStatus = 201 }
254254

255-
context "used with PATCH" $
255+
context "used with PATCH" $ do
256256
it "succeeds when using and/or params" $
257257
request methodPatch "/grandchild_entities?or=(id.eq.1,id.eq.2)&select=id,name"
258258
[("Prefer", "return=representation")]
259259
[json|{ name : "updated grandchild entity"}|] `shouldRespondWith`
260260
[json|[{ "id": 1, "name" : "updated grandchild entity"},{ "id": 2, "name" : "updated grandchild entity"}]|]
261261
{ matchHeaders = [matchContentTypeJson] }
262+
it "succeeds when the filtered column is modified" $
263+
request methodPatch "/entities?select=id,name&or=(name.is.null,name.like.*test*)"
264+
[("Prefer", "return=representation")]
265+
[json|{ "name" : "updated entity" }|] `shouldRespondWith`
266+
[json|[{ "id": 4, "name": "updated entity" }]|]
267+
{ matchHeaders = [matchContentTypeJson] }
268+
it "succeeds when the filtered column is not selected in the returned representation" $
269+
request methodPatch "/entities?select=id&or=(name.is.null,name.like.*test*)"
270+
[("Prefer", "return=representation")]
271+
[json|{ "name" : "updated entity" }|] `shouldRespondWith`
272+
[json|[{ "id": 4 }]|]
273+
{ matchHeaders = [matchContentTypeJson] }
262274

263-
context "used with DELETE" $
275+
context "used with DELETE" $ do
264276
it "succeeds when using and/or params" $
265277
request methodDelete "/grandchild_entities?or=(id.eq.1,id.eq.2)&select=id,name"
266278
[("Prefer", "return=representation")]
267279
""
268280
`shouldRespondWith`
269281
[json|[{ "id": 1, "name" : "grandchild entity 1" },{ "id": 2, "name" : "grandchild entity 2" }]|]
282+
it "succeeds when the filtered column is not selected in the returned representation" $
283+
request methodDelete "/entities?select=id&or=(name.is.null,name.like.*test*)"
284+
[("Prefer", "return=representation")]
285+
""
286+
`shouldRespondWith`
287+
[json|[{ "id": 4 }]|]
270288

271289
it "can query columns that begin with and/or reserved words" $
272290
get "/grandchild_entities?or=(and_starting_col.eq.smth, or_starting_col.eq.smth)" `shouldRespondWith` 200

0 commit comments

Comments
 (0)