on master help sign in
Add support for Lenient|Strict Capture
commit966ebe016912c0d7bac8220aab45b8995ddeb54e authorjschaul <jschaul@users.noreply.github.com> dateMon 18 Mar 2019, 16:43 (8 years ago) parentad02280
1 file changed+8−6
servant-server/src/Servant/Server/Internal.hs+8−6
servant-server/src/Servant/Server/Internal.hs+8−6
@@ -87,7 +87,7 @@ import Servant.API.ResponseHeaders
8787import qualified Servant.Types.SourceT as S
8888import Web.HttpApiData
8989 (FromHttpApiData, parseHeader, parseQueryParam,
90− parseUrlPieceMaybe, parseUrlPieces)
90+ parseUrlPieceMaybe, parseUrlPieces, parseUrlPiece)
9191
9292import Servant.Server.Internal.BasicAuth
9393import Servant.Server.Internal.Context
@@ -166,11 +166,11 @@ 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
@@ -178,9 +178,11 @@ instance (KnownSymbol capture, FromHttpApiData a, HasServer api context)
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,