Skip to content

Commit

Permalink
fix: nested empty embeds no longer return empty values and are correc…
Browse files Browse the repository at this point in the history
…tly omitted
  • Loading branch information
laurenceisla committed Jul 4, 2024
1 parent e0baf7e commit c3070bb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- #3558, Add the `admin-server-host` config to set the host for the admin server - @develop7
- #3607, Log to stderr when the JWT secret is less than 32 characters long - @laurenceisla

### Fixed

- #3093, Nested empty embeds no longer show empty values and are correctly omitted - @laurenceisla

### Changed

- #2052, Dropped support for PostgreSQL 9.6 - @wolfgangwalther
Expand Down
6 changes: 5 additions & 1 deletion src/PostgREST/Plan.hs
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,11 @@ generateRelSelectField (Node ReadPlan{relToParent=Just rel, select, relName, rel
where
rsSelName = fromMaybe relName relAlias
rsEmbedMode = if relIsToOne rel then JsonObject else JsonArray
rsEmptyEmbed = null select && null forest
rsEmptyEmbed = hasOnlyNullEmbed (null select) forest
hasOnlyNullEmbed = foldr checkIfNullEmbed
checkIfNullEmbed :: ReadPlanTree -> Bool -> Bool
checkIfNullEmbed (Node ReadPlan{select=s} f) isNullEmbed =
isNullEmbed && hasOnlyNullEmbed (null s) f
generateRelSelectField _ = Nothing

generateSpreadSelectFields :: ReadPlan -> [SpreadSelectField]
Expand Down
15 changes: 14 additions & 1 deletion test/spec/Feature/Query/QuerySpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,20 @@ spec = do
[json|[{"id":1,"name":"Angela Martin"}]|]
{ matchHeaders = [matchContentTypeJson] }

it "works on nested relationships" $ do
get "/users?select=*,users_tasks(tasks(projects()))" `shouldRespondWith`
[json| [{"id":1,"name":"Angela Martin"}, {"id":2,"name":"Michael Scott"}, {"id":3,"name":"Dwight Schrute"}]|]
{ matchHeaders = [matchContentTypeJson] }
get "/users?select=*,users_tasks!inner(tasks!inner(projects()))&users_tasks.tasks.id=eq.3" `shouldRespondWith`
[json| [{"id":1,"name":"Angela Martin"}]|]
{ matchHeaders = [matchContentTypeJson] }
get "/users?select=*,tasks(projects(clients()),users_tasks())" `shouldRespondWith`
[json| [{"id":1,"name":"Angela Martin"}, {"id":2,"name":"Michael Scott"}, {"id":3,"name":"Dwight Schrute"}]|]
{ matchHeaders = [matchContentTypeJson] }
get "/users?select=*,tasks!inner(projects(clients()),users_tasks(),name)&tasks.id=eq.3" `shouldRespondWith`
[json| [{"id":1,"name":"Angela Martin","tasks":[{"name": "Design w10"}]}]|]
{ matchHeaders = [matchContentTypeJson] }

context "empty root select" $
it "gives all columns" $ do
get "/projects?select=" `shouldRespondWith`
Expand Down Expand Up @@ -1394,4 +1408,3 @@ spec = do
get "/infinite_recursion?select=*" `shouldRespondWith`
[json|{"code":"42P17","message":"infinite recursion detected in rules for relation \"infinite_recursion\"","details":null,"hint":null}|]
{ matchStatus = 500 }

0 comments on commit c3070bb

Please sign in to comment.