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

Support JSONPath in error messages #1807

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions yesod-core/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# ChangeLog for yesod-core

## 1.6.25

* Support `JSONPath` in error messages [#1807](https://github.com/yesodweb/yesod/pull/1807)

## 1.6.24.2

* No star is type [#1797](https://github.com/yesodweb/yesod/pull/1797)
Expand Down
37 changes: 29 additions & 8 deletions yesod-core/src/Yesod/Core/Json.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ module Yesod.Core.Json

-- * Convert to a JSON value
, parseCheckJsonBody
, iparseCheckJsonBody
, parseInsecureJsonBody
, iparseInsecureJsonBody
, requireCheckJsonBody
, requireInsecureJsonBody
-- ** Deprecated JSON conversion
Expand Down Expand Up @@ -47,6 +49,7 @@ import Yesod.Core.Class.Handler
import Yesod.Core.Widget (WidgetFor)
import Yesod.Routes.Class
import qualified Data.Aeson as J
import qualified Data.Aeson.Types as JT
import qualified Data.Aeson.Parser as JP
import Data.Aeson ((.=), object)
import Data.Conduit.Attoparsec (sinkParser)
Expand Down Expand Up @@ -99,8 +102,6 @@ provideJson :: (Monad m, J.ToJSON a) => a -> Writer (Endo [ProvidedRep m]) ()
provideJson = provideRep . return . J.toEncoding

-- | Same as 'parseInsecureJsonBody'
--
-- @since 0.3.0
parseJsonBody :: (MonadHandler m, J.FromJSON a) => m (J.Result a)
parseJsonBody = parseInsecureJsonBody
{-# DEPRECATED parseJsonBody "Use parseCheckJsonBody or parseInsecureJsonBody instead" #-}
Expand All @@ -118,6 +119,16 @@ parseInsecureJsonBody = do
Left e -> J.Error $ show e
Right value -> J.fromJSON value

-- | Same as 'parseInsecureJsonBody', but returns an `IResult`
--
-- @since 1.6.25
iparseInsecureJsonBody :: (MonadHandler m, J.FromJSON a) => m (JT.IResult a)
iparseInsecureJsonBody = do
eValue <- runConduit $ rawRequestBody .| runCatchC (sinkParser JP.value')
return $ case eValue of
Left e -> JT.IError [] $ show e
Right value -> JT.ifromJSON value
Comment on lines +127 to +130
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the symmetry with the parseInsecureJsonBody, but I must admit I'm curious about this implementation.

It seems like we have a two step process here - first piping the ConduitT _ ByteString _ () into a JSON parser to get an intermediate Value, and then calling ifromJSON on that value.

But eitherDecodeStrict works on lazy ByteString, and calls ifromJSON too:

-- | Like 'decodeStrict' but returns an error message when decoding fails.
eitherDecodeStrict :: (FromJSON a) => B.ByteString -> Either String a
eitherDecodeStrict =
  eitherFormatError . eitherDecodeStrictWith jsonEOF ifromJSON

If we did rawRequestBody .| sinkLazy, then we'd get a Lazy.ByteString, which we could provide to eitherDecodeStrict and that would give us the JSON path error, albeit pre-formatted in the String error message. But that's what we end up doing anyway, so it may not be too bad.

But I'm also hesitant to change the implementation of a core bit of this without understanding why it is the way it is.

@snoyberg do you have any thouughts on this? tl;dr: this implementation requires an aeson >= 2.1 bound, and the eitherDecodeStrict implementation would work for our current bounds.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think adding a strict lower requiring aeson 2 of any kind may be a problem, I know of at least a few companies out there that haven't been able to migrate over yet. Would it be possible to keep backwards compat with CPP?


-- | Parse the request body to a data type as a JSON value. The
-- data type must support conversion from JSON via 'J.FromJSON'.
-- If you want the raw JSON value, just ask for a @'J.Result'
Expand All @@ -141,6 +152,16 @@ parseCheckJsonBody = do
Just True -> parseInsecureJsonBody
_ -> return $ J.Error $ "Non-JSON content type: " ++ show mct

-- | Same as 'parseCheckJsonBody', but returns an `IResult`
--
-- @since 1.6.25
iparseCheckJsonBody :: (MonadHandler m, J.FromJSON a) => m (JT.IResult a)
iparseCheckJsonBody = do
mct <- lookupHeader "content-type"
case fmap contentTypeHeaderIsJson mct of
Just True -> iparseInsecureJsonBody
_ -> return $ JT.IError [] $ "Non-JSON content type: " ++ show mct

-- | Same as 'parseInsecureJsonBody', but return an invalid args response on a parse
-- error.
parseJsonBody_ :: (MonadHandler m, J.FromJSON a) => m a
Expand All @@ -159,19 +180,19 @@ requireJsonBody = requireInsecureJsonBody
-- @since 1.6.11
requireInsecureJsonBody :: (MonadHandler m, J.FromJSON a) => m a
requireInsecureJsonBody = do
ra <- parseInsecureJsonBody
ra <- iparseInsecureJsonBody
case ra of
J.Error s -> invalidArgs [pack s]
J.Success a -> return a
JT.IError path s -> invalidArgs [pack (JT.formatError path s)]
JT.ISuccess a -> return a

-- | Same as 'parseCheckJsonBody', but return an invalid args response on a parse
-- error.
requireCheckJsonBody :: (MonadHandler m, J.FromJSON a) => m a
requireCheckJsonBody = do
ra <- parseCheckJsonBody
ra <- iparseCheckJsonBody
case ra of
J.Error s -> invalidArgs [pack s]
J.Success a -> return a
JT.IError path s -> invalidArgs [pack (JT.formatError path s)]
JT.ISuccess a -> return a

-- | Convert a list of values to an 'J.Array'.
array :: J.ToJSON a => [a] -> J.Value
Expand Down
4 changes: 2 additions & 2 deletions yesod-core/yesod-core.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: yesod-core
version: 1.6.24.2
version: 1.6.25
license: MIT
license-file: LICENSE
author: Michael Snoyman <[email protected]>
Expand All @@ -26,7 +26,7 @@ library
hs-source-dirs: src

build-depends: base >= 4.10 && < 5
, aeson >= 1.0
, aeson >= 2.1.0.0
, auto-update
, blaze-html >= 0.5
, blaze-markup >= 0.7.1
Expand Down
Loading