add: config to emit warning for legacy target names
Adds the `url_use_legacy_target_names` config. Enabled (default): * It allows using the resource name in filters, orders or limits when it has an alias, e.g. `table?select=alias:target(*)&target.id=eq.1` * Logs a WARNING with a hint to use the alias * Returns a Warning header in the response Disabled: * It returns an error, only the alias is allowed * No warnings returned This feature is deprecated
This commit is contained in:
committed by
Laurence Isla
parent
2fa8de4e52
commit
490d1dc5d3
@@ -7,6 +7,7 @@ import Test.Hspec hiding (pendingWith)
|
||||
import Test.Hspec.Wai
|
||||
import Test.Hspec.Wai.JSON
|
||||
|
||||
import PostgREST.Config (AppConfig (..))
|
||||
import PostgREST.Config.PgVersion (PgVersion, pgVersion190)
|
||||
|
||||
import Protolude hiding (get)
|
||||
@@ -1071,16 +1072,6 @@ spec actualPgVersion withConfig = withConfig baseCfg $ do
|
||||
{ "id":4,"children":[]}
|
||||
]|] { matchHeaders = [matchContentTypeJson] }
|
||||
|
||||
it "works when embedding the same table more than once" $
|
||||
get "/places?select=name,visits(id,start_time,visit_type),work_visits:visits(id,start_time,visit_type)&id=eq.1&visits.visit_type=neq.work&visits.start_time=gt.20250101+00:00&work_visits.visit_type=eq.work&work_visits.start_time=gt.20250101+00:00" `shouldRespondWith`
|
||||
[json|[
|
||||
{
|
||||
"name":"Lake",
|
||||
"visits":[{"id": 1, "start_time": "2025-01-01T10:00:00", "visit_type": "vacation"}, {"id": 2, "start_time": "2025-01-01T15:00:00", "visit_type": "vacation"}],
|
||||
"work_visits":[{"id": 3, "start_time": "2025-01-01T20:00:00", "visit_type": "work"}]
|
||||
}
|
||||
]|] { matchHeaders = [matchContentTypeJson] }
|
||||
|
||||
describe "ordering response" $ do
|
||||
it "by a column asc" $
|
||||
get "/items?id=lte.2&order=id.asc"
|
||||
@@ -1186,6 +1177,12 @@ spec actualPgVersion withConfig = withConfig baseCfg $ do
|
||||
]
|
||||
}
|
||||
|
||||
it "filters, orders and limits aliased embeded entities using the target name" $
|
||||
get "/projects?id=eq.1&select=id, name, the_tasks:tasks(id, name)&tasks.name=like.Code*&tasks.order=name.asc&tasks.limit=1" `shouldRespondWith`
|
||||
[json|[{"id":1,"name":"Windows 7","the_tasks":[{"id":2,"name":"Code w7"}]}]|]
|
||||
{ matchHeaders = ["Warning" <:> "299 PostgRESTv15(pre-release) \"Embedded resource was referenced by relation name even though it has an alias. This is deprecated and will stop working in a future release. Update `tasks` to `the_tasks` in query string filters, orders or limits.\""] }
|
||||
|
||||
|
||||
describe "Accept headers" $ do
|
||||
it "should respond an unknown accept type with 406" $
|
||||
request methodGet "/simple_pk"
|
||||
@@ -1690,3 +1687,30 @@ spec actualPgVersion withConfig = withConfig baseCfg $ do
|
||||
[json| {"code":"PGRST125","details":null,"hint":null,"message":"Invalid path specified in request URL"} |]
|
||||
{ matchStatus = 404
|
||||
, matchHeaders = ["Content-Length" <:> "96"]}
|
||||
|
||||
specLegacyTargetNames :: SpecWithConfig
|
||||
specLegacyTargetNames withConfig = withConfig (baseCfg { configUrlUseLegacyTargetNames = False }) $
|
||||
context "disable legacy target names" $ do
|
||||
describe "Shaping response with select parameter" $ do
|
||||
it "works when embedding the same table more than once" $
|
||||
get "/places?select=name,visits(id,start_time,visit_type),work_visits:visits(id,start_time,visit_type)&id=eq.1&visits.visit_type=neq.work&visits.start_time=gt.20250101+00:00&work_visits.visit_type=eq.work&work_visits.start_time=gt.20250101+00:00" `shouldRespondWith`
|
||||
[json|[
|
||||
{
|
||||
"name":"Lake",
|
||||
"visits":[{"id": 1, "start_time": "2025-01-01T10:00:00", "visit_type": "vacation"}, {"id": 2, "start_time": "2025-01-01T15:00:00", "visit_type": "vacation"}],
|
||||
"work_visits":[{"id": 3, "start_time": "2025-01-01T20:00:00", "visit_type": "work"}]
|
||||
}
|
||||
]|] { matchHeaders = [matchContentTypeJson] }
|
||||
describe "ordering response" $
|
||||
it "filters, orders or limits do not work with aliased embeded entities using the target name" $
|
||||
get "/projects?id=eq.1&select=id, name, the_tasks:tasks(id, name)&tasks.name=like.Code*&tasks.order=name.asc&tasks.limit=1" `shouldRespondWith`
|
||||
[json|
|
||||
{
|
||||
"code":"PGRST108",
|
||||
"details":"Target names are not allowed in filters if they have an alias",
|
||||
"hint":"Change 'tasks' to 'the_tasks' in filters, orders or limits.",
|
||||
"message":"'tasks' is not an embedded resource in this request"
|
||||
}
|
||||
|]
|
||||
{ matchStatus = 400,
|
||||
matchHeaders = [matchContentTypeJson] }
|
||||
|
||||
Reference in New Issue
Block a user