From 43d06204036123981b24167e6245cb08ae5e2499 Mon Sep 17 00:00:00 2001 From: Christian Marie Date: Sat, 16 May 2015 11:11:38 +1000 Subject: [PATCH] servant: allow whitespace after parsing JSON This includes some de-duplication in the test suite. --- servant/src/Servant/API/ContentTypes.hs | 25 ++++++++++++---- servant/test/Servant/API/ContentTypesSpec.hs | 31 ++++++++++---------- 2 files changed, 35 insertions(+), 21 deletions(-) diff --git a/servant/src/Servant/API/ContentTypes.hs b/servant/src/Servant/API/ContentTypes.hs index 384b3fe8..2cad8579 100644 --- a/servant/src/Servant/API/ContentTypes.hs +++ b/servant/src/Servant/API/ContentTypes.hs @@ -70,11 +70,12 @@ import Control.Applicative ((<*)) #endif import Control.Arrow (left) import Control.Monad -import Data.Aeson (FromJSON, ToJSON, Value, - encode, parseJSON) +import Data.Aeson (FromJSON, ToJSON, encode, + parseJSON) import Data.Aeson.Parser (value) import Data.Aeson.Types (parseEither) -import Data.Attoparsec.ByteString (endOfInput, parseOnly) +import Data.Attoparsec.ByteString.Char8 (endOfInput, parseOnly, + skipSpace, ()) import qualified Data.ByteString as BS import Data.ByteString.Lazy (ByteString, fromStrict, toStrict) import qualified Data.ByteString.Lazy as B @@ -291,10 +292,22 @@ instance MimeRender OctetStream BS.ByteString where -- | Like 'Data.Aeson.eitherDecode' but allows all JSON values instead of just -- objects and arrays. +-- +-- Will handle trailing whitespace, but not trailing junk. ie. +-- +-- >>> eitherDecodeLenient "1 " :: Either String Int +-- Right 1 +-- +-- >>> eitherDecodeLenient "1 junk" :: Either String Int +-- Left "trailing junk after valid JSON: endOfInput" eitherDecodeLenient :: FromJSON a => ByteString -> Either String a -eitherDecodeLenient input = do - v :: Value <- parseOnly (Data.Aeson.Parser.value <* endOfInput) (cs input) - parseEither parseJSON v +eitherDecodeLenient input = + parseOnly parser (cs input) >>= parseEither parseJSON + where + parser = + Data.Aeson.Parser.value + <* skipSpace + <* (endOfInput "trailing junk after valid JSON") -- | `eitherDecode` instance FromJSON a => MimeUnrender JSON a where diff --git a/servant/test/Servant/API/ContentTypesSpec.hs b/servant/test/Servant/API/ContentTypesSpec.hs index 272bde85..aaf935ed 100644 --- a/servant/test/Servant/API/ContentTypesSpec.hs +++ b/servant/test/Servant/API/ContentTypesSpec.hs @@ -10,6 +10,7 @@ module Servant.API.ContentTypesSpec where import Control.Applicative #endif import Control.Arrow +import Data.Either import Data.Aeson import Data.Aeson.Parser (jstring) import Data.Attoparsec.ByteString (parseOnly) @@ -25,6 +26,7 @@ import Data.String.Conversions (cs) import qualified Data.Text as TextS import qualified Data.Text.Lazy as TextL import GHC.Generics +import Data.Monoid import Network.URL (exportParams, importParams) import Test.Hspec import Test.QuickCheck @@ -36,54 +38,53 @@ spec :: Spec spec = describe "Servant.API.ContentTypes" $ do describe "The JSON Content-Type type" $ do + let p = Proxy :: Proxy JSON + + it "handles whitespace at end of input" $ do + mimeUnrender p "[1] " `shouldBe` Right [1 :: Int] + + it "does not like junk at end of input" $ do + mimeUnrender p "[1] this probably shouldn't work" + `shouldSatisfy` (isLeft :: Either a [Int] -> Bool) it "has mimeUnrender reverse mimeRender for valid top-level json ([Int]) " $ do - let p = Proxy :: Proxy JSON property $ \x -> mimeUnrender p (mimeRender p x) == Right (x::[Int]) it "has mimeUnrender reverse mimeRender for valid top-level json " $ do - let p = Proxy :: Proxy JSON property $ \x -> mimeUnrender p (mimeRender p x) == Right (x::SomeData) describe "The FormUrlEncoded Content-Type type" $ do - - let isNonNull ("", "") = False - isNonNull _ = True + let p = Proxy :: Proxy FormUrlEncoded it "has mimeUnrender reverse mimeRender" $ do - let p = Proxy :: Proxy FormUrlEncoded - property $ \x -> all isNonNull x + property $ \x -> all (/= mempty) x ==> mimeUnrender p (mimeRender p x) == Right (x::[(TextS.Text,TextS.Text)]) it "has mimeUnrender reverse exportParams (Network.URL)" $ do - let p = Proxy :: Proxy FormUrlEncoded - property $ \x -> all isNonNull x + property $ \x -> all (/= mempty) x ==> (mimeUnrender p . cs . exportParams . map (cs *** cs) $ x) == Right (x::[(TextS.Text,TextS.Text)]) it "has importParams (Network.URL) reverse mimeRender" $ do - let p = Proxy :: Proxy FormUrlEncoded - property $ \x -> all isNonNull x + property $ \x -> all (/= mempty) x ==> (fmap (map (cs *** cs)) . importParams . cs . mimeRender p $ x) == Just (x::[(TextS.Text,TextS.Text)]) describe "The PlainText Content-Type type" $ do + let p = Proxy :: Proxy PlainText it "has mimeUnrender reverse mimeRender (lazy Text)" $ do - let p = Proxy :: Proxy PlainText property $ \x -> mimeUnrender p (mimeRender p x) == Right (x::TextL.Text) it "has mimeUnrender reverse mimeRender (strict Text)" $ do - let p = Proxy :: Proxy PlainText property $ \x -> mimeUnrender p (mimeRender p x) == Right (x::TextS.Text) describe "The OctetStream Content-Type type" $ do + let p = Proxy :: Proxy OctetStream it "is id (Lazy ByteString)" $ do - let p = Proxy :: Proxy OctetStream property $ \x -> mimeRender p x == (x :: BSL.ByteString) && mimeUnrender p x == Right x it "is fromStrict/toStrict (Strict ByteString)" $ do - let p = Proxy :: Proxy OctetStream property $ \x -> mimeRender p x == BSL.fromStrict (x :: ByteString) && mimeUnrender p (BSL.fromStrict x) == Right x