{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeOperators #-}
module Servant.OpenApi.Internal.Test where
import Data.Aeson (ToJSON (..))
import Data.Aeson.Encode.Pretty (encodePretty)
import Data.OpenApi (Pattern, ToSchema, toSchema)
import Data.OpenApi.Schema.Validation
import Data.Text (Text)
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Encoding as TL
import Data.Typeable
import Test.Hspec
import Test.Hspec.QuickCheck
import Test.QuickCheck (Arbitrary, Property, counterexample, property)
import Servant.API
import Servant.OpenApi.Internal.TypeLevel
validateEveryToJSON
:: forall proxy api .
TMap (Every [Typeable, Show, Arbitrary, ToJSON, ToSchema])
(BodyTypes JSON api)
=> proxy api
-> Spec
validateEveryToJSON :: proxy api -> Spec
validateEveryToJSON proxy api
_ = Proxy '[ToJSON, ToSchema]
-> (forall x. EveryTF '[ToJSON, ToSchema] x => x -> Property)
-> Proxy (Nub (BodyTypes' JSON api))
-> Spec
forall (p :: [* -> Constraint] -> *) (p'' :: [*] -> *)
(cs :: [* -> Constraint]) (xs :: [*]).
TMap (Every (Typeable : Show : Arbitrary : cs)) xs =>
p cs -> (forall x. EveryTF cs x => x -> Property) -> p'' xs -> Spec
props
(Proxy '[ToJSON, ToSchema]
forall k (t :: k). Proxy t
Proxy :: Proxy [ToJSON, ToSchema])
(Maybe String -> Property
maybeCounterExample (Maybe String -> Property) -> (x -> Maybe String) -> x -> Property
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (x -> [String]) -> x -> Maybe String
forall a.
(ToJSON a, ToSchema a) =>
(a -> [String]) -> a -> Maybe String
prettyValidateWith x -> [String]
forall a. (ToJSON a, ToSchema a) => a -> [String]
validateToJSON)
(Proxy (Nub (BodyTypes' JSON api))
forall k (t :: k). Proxy t
Proxy :: Proxy (BodyTypes JSON api))
validateEveryToJSONWithPatternChecker :: forall proxy api. TMap (Every [Typeable, Show, Arbitrary, ToJSON, ToSchema]) (BodyTypes JSON api) =>
(Pattern -> Text -> Bool)
-> proxy api
-> Spec
validateEveryToJSONWithPatternChecker :: (Pattern -> Pattern -> Bool) -> proxy api -> Spec
validateEveryToJSONWithPatternChecker Pattern -> Pattern -> Bool
checker proxy api
_ = Proxy '[ToJSON, ToSchema]
-> (forall x. EveryTF '[ToJSON, ToSchema] x => x -> Property)
-> Proxy (Nub (BodyTypes' JSON api))
-> Spec
forall (p :: [* -> Constraint] -> *) (p'' :: [*] -> *)
(cs :: [* -> Constraint]) (xs :: [*]).
TMap (Every (Typeable : Show : Arbitrary : cs)) xs =>
p cs -> (forall x. EveryTF cs x => x -> Property) -> p'' xs -> Spec
props
(Proxy '[ToJSON, ToSchema]
forall k (t :: k). Proxy t
Proxy :: Proxy [ToJSON, ToSchema])
(Maybe String -> Property
maybeCounterExample (Maybe String -> Property) -> (x -> Maybe String) -> x -> Property
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (x -> [String]) -> x -> Maybe String
forall a.
(ToJSON a, ToSchema a) =>
(a -> [String]) -> a -> Maybe String
prettyValidateWith ((Pattern -> Pattern -> Bool) -> x -> [String]
forall a.
(ToJSON a, ToSchema a) =>
(Pattern -> Pattern -> Bool) -> a -> [String]
validateToJSONWithPatternChecker Pattern -> Pattern -> Bool
checker))
(Proxy (Nub (BodyTypes' JSON api))
forall k (t :: k). Proxy t
Proxy :: Proxy (BodyTypes JSON api))
props :: forall p p'' cs xs. TMap (Every (Typeable ': Show ': Arbitrary ': cs)) xs =>
p cs
-> (forall x. EveryTF cs x => x -> Property)
-> p'' xs
-> Spec
props :: p cs -> (forall x. EveryTF cs x => x -> Property) -> p'' xs -> Spec
props p cs
_ forall x. EveryTF cs x => x -> Property
f p'' xs
px = [Spec] -> Spec
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, Monad m) =>
t (m a) -> m ()
sequence_ [Spec]
specs
where
specs :: [Spec]
specs :: [Spec]
specs = Proxy (Typeable : Show : Arbitrary : cs)
-> (forall x (p' :: * -> *).
Every (Typeable : Show : Arbitrary : cs) x =>
p' x -> Spec)
-> p'' xs
-> [Spec]
forall a (cs :: [* -> Constraint]) (p :: [* -> Constraint] -> *)
(p'' :: [*] -> *) (xs :: [*]).
TMap (Every cs) xs =>
p cs
-> (forall x (p' :: * -> *). Every cs x => p' x -> a)
-> p'' xs
-> [a]
tmapEvery (Proxy (Typeable : Show : Arbitrary : cs)
forall k (t :: k). Proxy t
Proxy :: Proxy (Typeable ': Show ': Arbitrary ': cs)) forall x (p' :: * -> *).
Every (Typeable : Show : Arbitrary : cs) x =>
p' x -> Spec
forall (p' :: * -> *) a.
(EveryTF cs a, Typeable a, Show a, Arbitrary a) =>
p' a -> Spec
aprop p'' xs
px
aprop :: forall p' a. (EveryTF cs a, Typeable a, Show a, Arbitrary a) => p' a -> Spec
aprop :: p' a -> Spec
aprop p' a
_ = String -> (a -> Property) -> Spec
forall prop.
(HasCallStack, Testable prop) =>
String -> prop -> Spec
prop (TypeRep -> String
forall a. Show a => a -> String
show (a -> TypeRep
forall a. Typeable a => a -> TypeRep
typeOf (a
forall a. HasCallStack => a
undefined :: a))) (a -> Property
forall x. EveryTF cs x => x -> Property
f :: a -> Property)
prettyValidateWith
:: forall a. (ToJSON a, ToSchema a)
=> (a -> [ValidationError]) -> a -> Maybe String
prettyValidateWith :: (a -> [String]) -> a -> Maybe String
prettyValidateWith a -> [String]
f a
x =
case a -> [String]
f a
x of
[] -> Maybe String
forall a. Maybe a
Nothing
[String]
errors -> String -> Maybe String
forall a. a -> Maybe a
Just (String -> Maybe String) -> String -> Maybe String
forall a b. (a -> b) -> a -> b
$ [String] -> String
unlines
[ String
"Validation against the schema fails:"
, [String] -> String
unlines ((String -> String) -> [String] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (String
" * " String -> String -> String
forall a. [a] -> [a] -> [a]
++) [String]
errors)
, String
"JSON value:"
, Value -> String
ppJSONString Value
json
, String
""
, String
"OpenApi Schema:"
, Value -> String
ppJSONString (Schema -> Value
forall a. ToJSON a => a -> Value
toJSON Schema
schema)
]
where
ppJSONString :: Value -> String
ppJSONString = Text -> String
TL.unpack (Text -> String) -> (Value -> Text) -> Value -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Text
TL.decodeUtf8 (ByteString -> Text) -> (Value -> ByteString) -> Value -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Value -> ByteString
forall a. ToJSON a => a -> ByteString
encodePretty
json :: Value
json = a -> Value
forall a. ToJSON a => a -> Value
toJSON a
x
schema :: Schema
schema = Proxy a -> Schema
forall a. ToSchema a => Proxy a -> Schema
toSchema (Proxy a
forall k (t :: k). Proxy t
Proxy :: Proxy a)
maybeCounterExample :: Maybe String -> Property
maybeCounterExample :: Maybe String -> Property
maybeCounterExample Maybe String
Nothing = Bool -> Property
forall prop. Testable prop => prop -> Property
property Bool
True
maybeCounterExample (Just String
s) = String -> Property -> Property
forall prop. Testable prop => String -> prop -> Property
counterexample String
s (Bool -> Property
forall prop. Testable prop => prop -> Property
property Bool
False)