Skip to content

Commit 966ebe0

Browse files
committed
Add support for Lenient|Strict Capture
1 parent ad02280 commit 966ebe0

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

servant-server/src/Servant/Server/Internal.hs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ import Servant.API.ResponseHeaders
8787
import qualified Servant.Types.SourceT as S
8888
import Web.HttpApiData
8989
(FromHttpApiData, parseHeader, parseQueryParam,
90-
parseUrlPieceMaybe, parseUrlPieces)
90+
parseUrlPieceMaybe, parseUrlPieces, parseUrlPiece)
9191

9292
import Servant.Server.Internal.BasicAuth
9393
import Servant.Server.Internal.Context
@@ -166,21 +166,23 @@ instance (HasServer a context, HasServer b context) => HasServer (a :<|> b) cont
166166
-- > server = getBook
167167
-- > where getBook :: Text -> Handler Book
168168
-- > getBook isbn = ...
169-
instance (KnownSymbol capture, FromHttpApiData a, HasServer api context)
169+
instance (KnownSymbol capture, FromHttpApiData a, HasServer api context, SBoolI (FoldLenient mods))
170170
=> HasServer (Capture' mods capture a :> api) context where
171171

172172
type ServerT (Capture' mods capture a :> api) m =
173-
a -> ServerT api m
173+
If (FoldLenient mods) (Either String a) a -> ServerT api m
174174

175175
hoistServerWithContext _ pc nt s = hoistServerWithContext (Proxy :: Proxy api) pc nt . s
176176

177177
route Proxy context d =
178178
CaptureRouter $
179179
route (Proxy :: Proxy api)
180180
context
181-
(addCapture d $ \ txt -> case parseUrlPieceMaybe txt of
182-
Nothing -> delayedFail err400
183-
Just v -> return v
181+
(addCapture d $ \ txt -> case ( sbool :: SBool (FoldLenient mods)
182+
, parseUrlPiece txt :: Either T.Text a) of
183+
(SFalse, Left e) -> delayedFail err400 { errBody = cs e }
184+
(SFalse, Right v) -> return v
185+
(STrue, piece) -> return $ (either (Left . cs) Right) piece
184186
)
185187

186188
-- | If you use 'CaptureAll' in one of the endpoints for your API,

0 commit comments

Comments
 (0)