From cb821c5ca027a732b599c1ecda76e9007f60b587 Mon Sep 17 00:00:00 2001 From: Guy Gastineau Date: Tue, 25 Jan 2022 14:31:36 -0500 Subject: [PATCH] Fixed [""] for /route/ on `CaptureAll` apis. * Routes ending in a `CaptureAll` now get an empty list instead of [""] when they have a trailing slash. * WARNING: I think this will break the expectation that a rooted capture all will produce [""] for `//`. That is, previously `// => [""]`, but I think `// => []`. I will make some tests to check and see what is going on with this. --- servant-server/src/Servant/Server/Internal/Router.hs | 9 ++++++++- servant-server/test/Servant/ServerSpec.hs | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/servant-server/src/Servant/Server/Internal/Router.hs b/servant-server/src/Servant/Server/Internal/Router.hs index ecee5901..e5623ffb 100644 --- a/servant-server/src/Servant/Server/Internal/Router.hs +++ b/servant-server/src/Servant/Server/Internal/Router.hs @@ -178,7 +178,14 @@ runRouterEnv fmt router env request respond = -> let request' = request { pathInfo = rest } in runRouterEnv fmt router' (first, env) request' respond CaptureAllRouter router' -> - let segments = pathInfo request + let segments = case pathInfo request of + -- This case handles empty capture alls in a sub route like: + -- /legs/ => [] instead of [""] + -- But will this break a rooted capture all? like: + -- // => [] instead of [""] + -- Maybe we should fix it in Wai first. + [""] -> [] + xs -> xs request' = request { pathInfo = [] } in runRouterEnv fmt router' (segments, env) request' respond RawRouter app -> diff --git a/servant-server/test/Servant/ServerSpec.hs b/servant-server/test/Servant/ServerSpec.hs index 401abcf3..cc502e8c 100644 --- a/servant-server/test/Servant/ServerSpec.hs +++ b/servant-server/test/Servant/ServerSpec.hs @@ -273,7 +273,7 @@ type CaptureAllApi = "legs" :> CaptureAll "legs" Integer :> Get '[JSON] Animal captureAllApi :: Proxy CaptureAllApi captureAllApi = Proxy captureAllServer :: Server CaptureAllApi -captureAllServer = handleLegs :<|> (return :: [String] -> Handler [String]) +captureAllServer = handleLegs :<|> return where handleLegs [] = return beholder handleLegs legs = case sum legs of