diff --git a/Web/Scotty/Route.hs b/Web/Scotty/Route.hs index f1402cc..9c4bd95 100644 --- a/Web/Scotty/Route.hs +++ b/Web/Scotty/Route.hs @@ -140,10 +140,10 @@ matchRoute (Capture pat) req = go (T.split (=='/') pat) (compress $ "":pathInfo | otherwise = Nothing -- request string is longer than pattern go p [] prs | T.null (mconcat p) = Just prs -- in case pattern has trailing slashes | otherwise = Nothing -- request string is not long enough - go (p:ps) (r:rs) prs | p == r = go ps rs prs -- equal literals, keeping checking - | T.null p = Nothing -- p is null, but r is not, fail - | T.head p == ':' = go ps rs $ (T.tail p, r) : prs -- p is a capture, add to params - | otherwise = Nothing -- both literals, but unequal, fail + go (p:ps) (r:rs) prs = case T.uncons p of + Just (':', name) -> go ps rs $ (name, r) : prs -- p is a capture, add to params + _ | p == r -> go ps rs prs -- equal literals, keeping checking + | otherwise -> Nothing -- both literals, but unequal, fail compress ("":rest@("":_)) = compress rest compress (x:xs) = x : compress xs compress [] = [] diff --git a/changelog.md b/changelog.md index 267a3a1..c2c52a4 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,8 @@ ## next [????.??.??] +### Fixes +* Path parameters with value matching the parameter name prefixed by colon will properly populate `pathParams` with their literal value : `/:param` will match `/:param` and add a `Param` with value `("param", ":param")` (#301) + ## 0.21 [2023.12.17] ### New diff --git a/test/Web/ScottySpec.hs b/test/Web/ScottySpec.hs index c73b5f5..1fb3ee7 100644 --- a/test/Web/ScottySpec.hs +++ b/test/Web/ScottySpec.hs @@ -57,6 +57,8 @@ spec = do makeRequest "//scotty" `shouldRespondWith` 200 withApp (route "/:paramName" $ captureParam "paramName" >>= text) $ do + it ("captures route parameters for " ++ method ++ " requests when parameter matches its name") $ do + makeRequest "/:paramName" `shouldRespondWith` ":paramName" it ("captures route parameters for " ++ method ++ " requests with url encoded '/' in path") $ do makeRequest "/a%2Fb" `shouldRespondWith` "a/b"