Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: avoid matching capture pattern name ( Fixes #291) #301

Merged
merged 7 commits into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Web/Scotty/Route.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import qualified Text.Regex as Regex

import Web.Scotty.Action
import Web.Scotty.Internal.Types (RoutePattern(..), RouteOptions, ActionEnv(..), ActionT, ScottyState(..), ScottyT(..), ErrorHandler, Middleware, BodyInfo, handler, addRoute, defaultScottyResponse)

Check warning on line 23 in Web/Scotty/Route.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.6.3

The import of ‘ActionT’

Check warning on line 23 in Web/Scotty/Route.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 8.10.7

The import of ‘ActionT’

Check warning on line 23 in Web/Scotty/Route.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.0.2

The import of ‘ActionT’

Check warning on line 23 in Web/Scotty/Route.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.2.8

The import of ‘ActionT’

Check warning on line 23 in Web/Scotty/Route.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.4.6

The import of ‘ActionT’

Check warning on line 23 in Web/Scotty/Route.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.6.2

The import of ‘ActionT’
import Web.Scotty.Util (decodeUtf8Lenient)
import Web.Scotty.Body (cloneBodyInfo, getBodyAction, getBodyChunkAction, getFormParamsAndFilesAction)

Expand Down Expand Up @@ -140,10 +140,10 @@
| 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 [] = []
Expand Down
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions test/Web/ScottySpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
ocramz marked this conversation as resolved.
Show resolved Hide resolved
it ("captures route parameters for " ++ method ++ " requests with url encoded '/' in path") $ do
makeRequest "/a%2Fb" `shouldRespondWith` "a/b"

Expand Down
Loading