diff --git a/Web/Scotty/Body.hs b/Web/Scotty/Body.hs index 97614ea..dfcdc4e 100644 --- a/Web/Scotty/Body.hs +++ b/Web/Scotty/Body.hs @@ -23,7 +23,7 @@ import qualified Data.ByteString.Lazy.Char8 as BL import qualified GHC.Exception as E (throw) import Network.Wai (Request(..), getRequestBodyChunk) import qualified Network.Wai.Handler.Warp as Warp (InvalidRequest(..)) -import qualified Network.Wai.Parse as W (File, Param, getRequestBodyType, tempFileBackEnd, RequestBodyType(..), sinkRequestBodyEx, RequestParseException(..), ParseRequestBodyOptions) +import qualified Network.Wai.Parse as W (File, Param, getRequestBodyType, tempFileBackEnd, RequestBodyType(..), sinkRequestBodyEx, RequestParseException(..), ParseRequestBodyOptions,FileInfo(..)) -- import UnliftIO (MonadUnliftIO(..)) import UnliftIO.Exception (Handler(..), catches, throwIO) @@ -64,7 +64,10 @@ getFormParamsAndFilesAction istate prbo req bodyInfo opts = do let wholeBody = BL.toChunks bs (formparams, fs) <- parseRequestBodyExBS istate prbo wholeBody (W.getRequestBodyType req) `catches` handleWaiParseSafeExceptions - return (convertBoth <$> formparams, convertKey <$> fs) + let + fs' = filter emptyFile fs + return (convertBoth <$> formparams, convertKey <$> fs') + where emptyFile (_,fInfo) = ("\"\"" /= (W.fileName fInfo)) -- | Wrap exceptions from upstream libraries into 'ScottyException' handleWaiParseSafeExceptions :: MonadIO m => [Handler m a] diff --git a/changelog.md b/changelog.md index 80575ef..aa3ff43 100644 --- a/changelog.md +++ b/changelog.md @@ -7,7 +7,7 @@ ### Breaking changes * Remove dependency on data-default class (#386). We have been exporting constants for default config values since 0.20, and this dependency was simply unnecessary. * Remove re-export of `Network.Wai.Parse.ParseRequestBodyOptions` from `Web.Scotty` and `Web.Scotty.Trans`. This is a type from `wai-extra` so exporting it is unnecessary. - +* (#396) Fixed unwated behaviour of `files` and `filesOpts` while not uploading any file. ## 0.22 [2024.03.09] diff --git a/test/Web/ScottySpec.hs b/test/Web/ScottySpec.hs index 0279adf..9f358bb 100644 --- a/test/Web/ScottySpec.hs +++ b/test/Web/ScottySpec.hs @@ -380,7 +380,9 @@ spec = do (FMFile "file1.txt", "text/plain;charset=UTF-8", "file", "xxx"), (FMFile "file1.txt", "text/plain;charset=UTF-8", "file", "xxx") ] `shouldRespondWith` 413 - + context "Not uploading file should return empty list (#396)" $ do + it "Length of list should be 0" $ do + postMultipartForm "/files" "ABC123" [(FMFile "", "", "", "")] `shouldRespondWith` "0" describe "filesOpts" $ do let @@ -401,6 +403,10 @@ spec = do Scotty.post "/files" processForm) $ do it "loads uploaded files in memory" $ do postMpForm `shouldRespondWith` 200 { matchBody = "2"} + context "Not uploading file should return empty list (#396)" $ do + withApp (Scotty.post "/files" processForm) $ do + it "Length of list should be 0" $ do + postMultipartForm "/files" "ABC123" [(FMFile "", "", "", "")] `shouldRespondWith` "0" describe "text" $ do