on master help sign in
26 files changed+304−159
since splitsnapshots
unifiedsplit
.github/workflows/cabal.yml+64 .github/workflows/ci.yml+2−2 README.md+2−1 package.yaml+2−1 roboservant.cabal+10−4 src/Roboservant/Client.hs+20−10 src/Roboservant/Direct.hs+16−14 src/Roboservant/Server.hs+2−6 src/Roboservant/Types.hs+1 src/Roboservant/Types/Breakdown.hs+6−1 src/Roboservant/Types/BuildFrom.hs+10−4 src/Roboservant/Types/Internal.hs+2−2 src/Roboservant/Types/Orphans.hs+7 src/Roboservant/Types/ReifiedApi.hs+43−4 src/Roboservant/Types/ReifiedApi/Server.hs+4−5 stack.yaml+5−10 stack.yaml.lock+9−58 test/Breakdown.hs+1−1 test/Nested.hs+3−3 test/Post.hs+2−2 test/Product.hs+1−1 test/Put.hs+33 test/QueryParams.hs+15 test/Spec.hs+39−24 test/UnsafeIO.hs+3−3 test/Valid.hs+2−3
.github/workflows/cabal.ymlnew file+64
@@ -0,0 +1,64 @@
1+# modified from https://github.com/jgm/pandoc/blob/master/.github/workflows/ci.yml
2+name: Cabal CI
3+
4+on:
5+ push:
6+ branches:
7+ - '**'
8+ paths-ignore: []
9+ pull_request:
10+ paths-ignore: []
11+
12+jobs:
13+ linux:
14+
15+ runs-on: ubuntu-20.04
16+ strategy:
17+ fail-fast: false
18+ matrix:
19+ versions:
20+ - ghc: '8.8.4'
21+ cabal: '3.4'
22+ - ghc: '8.10.4'
23+ cabal: '3.4'
24+ steps:
25+ - uses: actions/checkout@v2
26+
27+ # need to install older cabal/ghc versions from ppa repository
28+
29+ - name: Install recent cabal/ghc
30+ uses: actions/setup-haskell@v1.1.3
31+ with:
32+ ghc-version: ${{ matrix.versions.ghc }}
33+ cabal-version: ${{ matrix.versions.cabal }}
34+
35+ # declare/restore cached things
36+ # caching doesn't work for scheduled runs yet
37+ # https://github.com/actions/cache/issues/63
38+
39+ - name: Cache cabal global package db
40+ id: cabal-global
41+ uses: actions/cache@v2
42+ with:
43+ path: |
44+ ~/.cabal
45+ key: ${{ runner.os }}-${{ matrix.versions.ghc }}-${{ matrix.versions.cabal }}-cabal-global-${{ hashFiles('cabal.project') }}
46+
47+ - name: Cache cabal work
48+ id: cabal-local
49+ uses: actions/cache@v2
50+ with:
51+ path: |
52+ dist-newstyle
53+ key: ${{ runner.os }}-${{ matrix.versions.ghc }}-${{ matrix.versions.cabal }}-cabal-local
54+
55+ - name: Install dependencies
56+ run: |
57+ cabal update
58+ cabal build all --dependencies-only --enable-tests --disable-optimization
59+ - name: Build
60+ run: |
61+ cabal build all --enable-tests --disable-optimization 2>&1 | tee build.log
62+ - name: Test
63+ run: |
64+ cabal test all --disable-optimization
.github/workflows/ci.yml+2−2
@@ -1,4 +1,4 @@
1−name: CI
1+name: Stack CI
22
33# Trigger the workflow on push or pull request, but only for the master branch
44on:
@@ -12,7 +12,7 @@ jobs:
1212 strategy:
1313 matrix:
1414 stack: ["2.3.3"]
15− ghc: ["8.8.3"]
15+ ghc: ["8.10.4"]
1616 os: [ubuntu-latest]
1717 steps:
1818 - uses: actions/checkout@v2
README.md+2−1
@@ -2,7 +2,8 @@
22
33Automatically fuzz your servant apis in a contextually-aware way.
44
5−![CI](https://github.com/mwotton/roboservant/workflows/CI/badge.svg)
5+[![Stack CI](https://github.com/mwotton/roboservant/actions/workflows/ci.yml/badge.svg)](https://github.com/mwotton/roboservant/actions/workflows/ci.yml)
6+[![Cabal CI](https://github.com/mwotton/roboservant/actions/workflows/cabal.yml/badge.svg)](https://github.com/mwotton/roboservant/actions/workflows/cabal.yml)
67
78## example
89
package.yaml+2−1
@@ -1,6 +1,6 @@
11
22name: roboservant
3−version: 0.1.0.2
3+version: 0.1.0.3
44github: "mwotton/roboservant"
55license: BSD3
66copyright: "2020 Mark Wotton, Samuel Schlesinger"
@@ -19,6 +19,7 @@ dependencies:
1919- containers
2020- random
2121- hashable
22+- http-types
2223- lifted-base
2324- monad-control
2425- mtl
roboservant.cabal+10−4
@@ -1,13 +1,13 @@
11cabal-version: 1.12
22
3−-- This file has been generated from package.yaml by hpack version 0.33.0.
3+-- This file has been generated from package.yaml by hpack version 0.35.0.
44--
55-- see: https://github.com/sol/hpack
66--
7−-- hash: 4c71b6b9460f4719791bb8e67f4a5398b807cd8d17c43dc69da806bb5ba9fb4b
7+-- hash: 0c0c35e1dca1d6c52e5bc61591cbd8c0da9d811b83341bbc676c5f8b93b0b20d
88
99name: roboservant
10−version: 0.1.0.2
10+version: 0.1.0.3
1111synopsis: Automatic session-aware servant testing
1212description: Please see the README on GitHub at <https://github.com/mwotton/roboservant#readme>
1313category: Web
@@ -36,6 +36,7 @@ library
3636 Roboservant.Types.BuildFrom
3737 Roboservant.Types.Config
3838 Roboservant.Types.Internal
39+ Roboservant.Types.Orphans
3940 Roboservant.Types.ReifiedApi
4041 Roboservant.Types.ReifiedApi.Server
4142 other-modules:
@@ -50,6 +51,7 @@ library
5051 , dependent-map
5152 , dependent-sum
5253 , hashable
54+ , http-types
5355 , lifted-base
5456 , monad-control
5557 , mtl
@@ -71,7 +73,7 @@ test-suite example
7173 other-modules:
7274 Paths_roboservant
7375 hs-source-dirs:
74− ./.
76+ ./
7577 ghc-options: -Wall -fwrite-ide-info -hiedir=.hie -pgmL markdown-unlit
7678 build-tool-depends:
7779 markdown-unlit:markdown-unlit
@@ -86,6 +88,7 @@ test-suite example
8688 , hspec
8789 , hspec-core
8890 , http-client
91+ , http-types
8992 , lifted-base
9093 , monad-control
9194 , mtl
@@ -113,6 +116,8 @@ test-suite roboservant-test
113116 Nested
114117 Post
115118 Product
119+ Put
120+ QueryParams
116121 Seeded
117122 UnsafeIO
118123 Valid
@@ -133,6 +138,7 @@ test-suite roboservant-test
133138 , hspec-wai
134139 , http-api-data
135140 , http-client
141+ , http-types
136142 , lifted-base
137143 , monad-control
138144 , mtl
src/Roboservant/Client.hs+20−10
@@ -1,18 +1,18 @@
11{-# LANGUAGE RankNTypes #-}
22{-# LANGUAGE TypeOperators #-}
3+{-# LANGUAGE NamedFieldPuns #-}
34{-# LANGUAGE DataKinds #-}
45{-# LANGUAGE AllowAmbiguousTypes #-}
56{-# LANGUAGE DataKinds #-}
6−{-# LANGUAGE NamedFieldPuns#-}
77{-# LANGUAGE GADTs #-}
88{-# LANGUAGE PolyKinds #-}
99{-# LANGUAGE ScopedTypeVariables #-}
1010{-# LANGUAGE TypeApplications #-}
11−{-# LANGUAGE TypeOperators #-}
11+
1212{-# LANGUAGE UndecidableInstances #-}
1313{-# LANGUAGE TypeFamilies #-}
1414{-# LANGUAGE FlexibleContexts #-}
15−-- should all the NormalizeFunction instances be in one place?
15+-- should all the NormalizeFunction instances be in one place?
1616{-# OPTIONS_GHC -fno-warn-orphans #-}
1717module Roboservant.Client where
1818
@@ -28,13 +28,14 @@ import qualified Data.Vinyl.Curry as V
2828import qualified Data.Text as T
2929import Control.Monad.Reader
3030import Data.Hashable
31+import Network.HTTP.Types.Status
3132
3233-- fuzz :: forall api.
3334-- (FlattenServer api, ToReifiedApi (Endpoints api)) =>
3435-- Server api ->
3536-- Config ->
3637-- IO (Maybe Report)
37−-- fuzz s = fuzz' (reifyServer s)
38+-- fuzz s = fuzz' (reifyServer s)
3839-- -- todo: how do we pull reifyServer out?
3940-- where reifyServer :: (FlattenServer api, ToReifiedApi (Endpoints api))
4041-- => Server api -> ReifiedApi
@@ -42,7 +43,10 @@ import Data.Hashable
4243
4344fuzz :: forall api . (ToReifiedClientApi (Endpoints api), FlattenClient api, HasClient ClientM api)
4445 => ClientEnv -> Config -> IO (Maybe Report)
45−fuzz clientEnv config = fuzz' (toReifiedClientApi (flattenClient @api apiClient) (Proxy @(Endpoints api)) clientEnv ) config
46+fuzz clientEnv
47+ = fuzz'
48+ (toReifiedClientApi
49+ (flattenClient @api apiClient) (Proxy @(Endpoints api)) clientEnv)
4650 where apiClient = client (Proxy @api)
4751
4852
@@ -78,7 +82,7 @@ instance
7882 foo :: V.Curried (EndpointArgs endpoint) (ReaderT ClientEnv IO ResultType)
7983 -> V.Curried (EndpointArgs endpoint) (IO ResultType)
8084 foo = mapCurried @(EndpointArgs endpoint) @(ReaderT ClientEnv IO ResultType) (`runReaderT` clientEnv)
81−
85+
8286mapCurried :: forall ts a b. V.RecordCurry' ts => (a -> b) -> V.Curried ts a -> V.Curried ts b
8387mapCurried f g = V.rcurry' @ts $ f . V.runcurry' g
8488
@@ -88,10 +92,15 @@ type ResultType = Either InteractionError (NonEmpty (Dynamic,Int))
8892
8993instance (Typeable x, Hashable x, Breakdown x) => NormalizeFunction (ClientM x) where
9094 type Normal (ClientM x) = ReaderT ClientEnv IO (Either InteractionError (NonEmpty (Dynamic,Int)))
91− normalize c = ReaderT $
92− fmap (bimap (InteractionError . T.pack . show) breakdown) . runClientM c
93−--
95+ normalize c = ReaderT $
96+ fmap (bimap renderClientError breakdown) . runClientM c
97+ where
98+ renderClientError :: ClientError -> InteractionError
99+ renderClientError err = case err of
100+ FailureResponse _ Response{responseStatusCode} -> InteractionError textual (responseStatusCode == status500)
101+ _ -> InteractionError textual True
94102
103+ where textual = T.pack $ show err
95104instance ToReifiedClientApi '[] where
96105 toReifiedClientApi NoClientEndpoints _ _ = []
97106
@@ -103,7 +112,7 @@ instance
103112 FlattenClient (endpoint :<|> api)
104113 where
105114 flattenClient (endpoint :<|> c) = endpoint `AClientEndpoint` flattenClient @api c
106−
115+
107116instance
108117 (
109118 Endpoints api ~ '[api]
@@ -112,6 +121,7 @@ instance
112121 where
113122 flattenClient c = c `AClientEndpoint` NoClientEndpoints
114123
124+
115125instance FlattenClient (Verb method statusCode contentTypes responseType)
116126 where
117127 flattenClient c = c `AClientEndpoint` NoClientEndpoints
src/Roboservant/Direct.hs+16−14
@@ -7,7 +7,7 @@
77{-# LANGUAGE RecordWildCards #-}
88{-# LANGUAGE ScopedTypeVariables #-}
99{-# LANGUAGE TupleSections #-}
10−{-# LANGUAGE TypeApplications #-}
10+
1111{-# LANGUAGE TypeFamilies #-}
1212{-# LANGUAGE UndecidableInstances #-}
1313
@@ -56,7 +56,7 @@ import GHC.Generics ((:*:) (..))
5656import Roboservant.Types
5757 ( ApiOffset (..),
5858 Argument (..),
59− InteractionError,
59+ InteractionError(..),
6060 Provenance (..),
6161 ReifiedApi,
6262 ReifiedEndpoint (..),
@@ -150,12 +150,12 @@ fuzz' reifiedApi Config {..} = handle (pure . Just . formatException) $ do
150150 Report
151151 (unlines [show failureType, show exception])
152152 r
153− displayDiagnostics FuzzState {..} = liftIO $ do
153+ displayDiagnostics FuzzState {..} = liftIO $
154154 logInfo $ unlines $
155− ["api endpoints covered"]
156− <> (map show . Set.toList . Set.fromList $ map apiOffset path)
157− <> ["", "types in stash"]
158− <> DM.foldrWithKey (\_ v r -> (show . NEL.length . getStashValue $ v) : r) [] (getStash stash)
155+ ["api endpoints covered"]
156+ <> (map show . Set.toList . Set.fromList $ map apiOffset path)
157+ <> ["", "types in stash"]
158+ <> DM.foldrWithKey (\_ v r -> (show . NEL.length . getStashValue $ v) : r) [] (getStash stash)
159159 -- <> (map (show . NEL.length . getStashValue ) $ DM.assocs (getStash stash))
160160 -- $ \_k v ->
161161 -- (show . NEL.length $ getStashValue v))
@@ -242,12 +242,15 @@ fuzz' reifiedApi Config {..} = handle (pure . Just . formatException) $ do
242242 execute fuzzop func args = do
243243 (liftIO . logInfo . show . (fuzzop,) . stash) =<< get
244244 liftIO (V.runcurry' func argVals) >>= \case
245− Left (e::InteractionError) -> throw e
246− Right (dyn :: NEL.NonEmpty (Dynamic, Int)) -> do
245+ Left (e::InteractionError) ->
246+ if fatalError e
247+ then throw e
248+ else pure ()
249+ Right (dyn :: NEL.NonEmpty (Dynamic, Int)) ->
247250 modify'
248− ( \fs@FuzzState {..} ->
249− fs {stash = addToStash (NEL.toList dyn) stash}
250− )
251+ ( \fs@FuzzState {..} ->
252+ fs {stash = addToStash (NEL.toList dyn) stash}
253+ )
251254 where
252255 argVals = V.rmap (\(_ :*: V.Identity x) -> V.Identity x) args
253256 -- argTypes = recordToList' (\(tr :*: _) -> R.SomeTypeRep tr) args
@@ -259,8 +262,7 @@ fuzz' reifiedApi Config {..} = handle (pure . Just . formatException) $ do
259262 (execute op func args)
260263 [ Handler (\(e :: SomeAsyncException) -> throw e),
261264 Handler
262− ( \(e :: SomeException) -> do
263− -- displayDiagnostics =<< get
265+ ( \(e :: SomeException) ->
264266 throw . RoboservantException ServerCrashed (Just e) =<< get
265267 )
266268 ]
src/Roboservant/Server.hs+2−6
@@ -4,12 +4,9 @@
44{-# LANGUAGE AllowAmbiguousTypes #-}
55{-# LANGUAGE FlexibleContexts #-}
66{-# LANGUAGE GADTs #-}
7−{-# LANGUAGE LambdaCase #-}
87{-# LANGUAGE PolyKinds #-}
9−{-# LANGUAGE RankNTypes #-}
10−{-# LANGUAGE RecordWildCards #-}
8+
119{-# LANGUAGE ScopedTypeVariables #-}
12−{-# LANGUAGE TupleSections #-}
1310{-# LANGUAGE TypeApplications #-}
1411{-# LANGUAGE TypeFamilies #-}
1512{-# LANGUAGE UndecidableInstances #-}
@@ -30,11 +27,10 @@ fuzz :: forall api.
3027 Server api ->
3128 Config ->
3229 IO (Maybe Report)
33−fuzz s = fuzz' (reifyServer s)
30+fuzz s = fuzz' (reifyServer s)
3431 -- todo: how do we pull reifyServer out?
3532 where reifyServer :: (FlattenServer api, ToReifiedApi (Endpoints api))
3633 => Server api -> ReifiedApi
3734 reifyServer server = toReifiedApi (flattenServer @api server) (Proxy @(Endpoints api))
3835-- reifyServer server = toReifiedApi server (Proxy @(Endpoints api))
3936
40−
src/Roboservant/Types.hs+1
@@ -23,3 +23,4 @@ import Roboservant.Types.Config
2323import Roboservant.Types.Internal
2424import Roboservant.Types.ReifiedApi
2525import Roboservant.Types.ReifiedApi.Server
26+import Roboservant.Types.Orphans()
src/Roboservant/Types/Breakdown.hs+6−1
@@ -1,5 +1,6 @@
11{-# LANGUAGE AllowAmbiguousTypes #-}
22{-# LANGUAGE DataKinds #-}
3+{-# LANGUAGE FlexibleContexts #-}
34{-# LANGUAGE DerivingVia #-}
45{-# LANGUAGE FlexibleInstances #-}
56{-# LANGUAGE GADTs #-}
@@ -22,6 +23,8 @@ import qualified Data.List.NonEmpty as NEL
2223import Data.Typeable (Typeable)
2324import GHC.Generics
2425import Roboservant.Types.Internal
26+import Servant
27+import Roboservant.Types.Orphans()
2528
2629breakdown ::
2730 (Hashable x, Typeable x, Breakdown x) =>
@@ -46,7 +49,7 @@ instance (Hashable x, Typeable x, Breakdown x) => Breakdown [x] where
4649 breakdownExtras stash = concatMap (NEL.toList . breakdown) stash
4750
4851
49−class GBreakdown (f :: k -> *) where
52+class GBreakdown (f :: k -> Type) where
5053 gBreakdownExtras :: f a -> [(Dynamic, Int)]
5154
5255instance (Hashable x, Typeable x, Generic x, GBreakdown (Rep x)) => Breakdown (Compound (x :: Type)) where
@@ -74,3 +77,5 @@ instance (Hashable a, Typeable a, Breakdown a) => GBreakdown (K1 R a) where
7477
7578instance GBreakdown U1 where
7679 gBreakdownExtras U1 = []
80+
81+deriving via (Atom NoContent) instance Breakdown NoContent
src/Roboservant/Types/BuildFrom.hs+10−4
@@ -12,6 +12,7 @@
1212
1313module Roboservant.Types.BuildFrom where
1414
15+import Data.List(nub)
1516import qualified Data.Dependent.Map as DM
1617import Data.Hashable
1718import qualified Data.IntSet as IntSet
@@ -22,6 +23,8 @@ import Data.Typeable (Typeable)
2223import GHC.Generics
2324import Roboservant.Types.Internal
2425import qualified Type.Reflection as R
26+import Servant(NoContent)
27+import Roboservant.Types.Orphans()
2528
2629buildFrom :: forall x. (Hashable x, BuildFrom x, Typeable x) => Stash -> Maybe (StashValue x)
2730buildFrom = buildStash . buildFrom'
@@ -59,11 +62,12 @@ deriving via (Compound (Maybe x)) instance (Typeable x, Hashable x, BuildFrom x)
5962
6063-- this isn't wonderful, but we need a hand-rolled instance for recursive datatypes right now.
6164-- with an arbitrary-ish interface, we could use a size parameter, rng access etc.
62−instance (BuildFrom x) => BuildFrom [x] where
63− extras stash = map (\xs -> (concatMap fst xs, map snd xs)) $ notpowerset $ extras @x stash
65+instance (Eq x, BuildFrom x) => BuildFrom [x] where
66+ extras stash =
67+ nub $ map (\xs -> (concatMap fst xs, map snd xs)) $ notpowerset $ buildFrom' @x stash
6468 where
6569 -- powerset creates way too much stuff. something better here eventually.
66− notpowerset xs = xs:map pure xs
70+ notpowerset xs = []:xs:map pure xs
6771
6872
6973instance (Hashable x, Typeable x, Generic x, GBuildFrom (Rep x)) => BuildFrom (Compound (x :: Type)) where
@@ -73,7 +77,7 @@ deriving via (Atom Int) instance BuildFrom Int
7377
7478deriving via (Atom Char) instance BuildFrom Char
7579
76−class GBuildFrom (f :: k -> *) where
80+class GBuildFrom (f :: k -> Type) where
7781 gExtras :: Stash -> [([Provenance], f a)]
7882
7983instance GBuildFrom b => GBuildFrom (M1 D a b) where
@@ -99,3 +103,5 @@ instance BuildFrom a => GBuildFrom (K1 i a) where
99103
100104instance GBuildFrom U1 where
101105 gExtras _ = [([], U1)]
106+
107+deriving via (Atom NoContent) instance BuildFrom NoContent
src/Roboservant/Types/Internal.hs+2−2
@@ -43,11 +43,11 @@ instance Show Stash where
4343
4444-- | Can't be built up from parts, can't be broken down further.
4545newtype Atom x = Atom {unAtom :: x}
46− deriving newtype (Hashable, Typeable)
46+ deriving newtype (Hashable, Typeable,Eq)
4747
4848-- | can be broken down and built up from generic pieces
4949newtype Compound x = Compound {unCompound :: x}
50− deriving newtype (Hashable, Typeable)
50+ deriving newtype (Hashable, Typeable, Eq)
5151
5252hashedDyn :: (Hashable a, Typeable a) => a -> (Dynamic, Int)
5353hashedDyn a = (toDyn a, hash a)
src/Roboservant/Types/Orphans.hsnew file+7
@@ -0,0 +1,7 @@
1+{-# OPTIONS_GHC -fno-warn-orphans #-}
2+module Roboservant.Types.Orphans where
3+
4+import Servant
5+import Data.Hashable
6+
7+instance Hashable NoContent
src/Roboservant/Types/ReifiedApi.hs+43−4
@@ -28,7 +28,7 @@ import GHC.Generics ((:*:)(..))
2828import Roboservant.Types.Internal
2929import Roboservant.Types.Breakdown
3030import Roboservant.Types.BuildFrom
31−import Data.Kind
31+import Data.Kind(Type)
3232import Servant
3333import Servant.API.Modifiers(FoldRequired,FoldLenient)
3434import GHC.TypeLits (Symbol)
@@ -52,9 +52,12 @@ data ReifiedEndpoint = forall as. (V.RecordToList as, V.RMap as) => ReifiedEndpo
5252 , reEndpointFunc :: V.Curried as (IO (Either InteractionError (NonEmpty (Dynamic,Int))))
5353 }
5454
55+instance Show ReifiedEndpoint where
56+ show _ = "lol"
57+
5558class ( V.RecordToList (EndpointArgs endpoint)
5659 , V.RMap (EndpointArgs endpoint)
57− ) => ToReifiedEndpoint (endpoint :: *) where
60+ ) => ToReifiedEndpoint (endpoint :: Type) where
5861 type EndpointArgs endpoint :: [Type]
5962 type EndpointRes endpoint :: Type
6063
@@ -64,13 +67,15 @@ class ( V.RecordToList (EndpointArgs endpoint)
6467tagType :: Typeable a => f a -> TypedF f a
6568tagType = (R.typeRep :*:)
6669
67−newtype InteractionError = InteractionError T.Text
70+data InteractionError = InteractionError
71+ { errorMessage :: T.Text
72+ , fatalError :: Bool
73+ }
6874 deriving Show
6975instance Exception InteractionError
7076
7177
7278
73−
7479instance
7580 (Typeable responseType, Breakdown responseType) =>
7681 ToReifiedEndpoint (Verb method statusCode contentTypes responseType)
@@ -79,6 +84,12 @@ instance
7984 type EndpointRes (Verb method statusCode contentTypes responseType) = responseType
8085 reifiedEndpointArguments = V.RNil
8186
87+instance ToReifiedEndpoint (NoContentVerb method)
88+ where
89+ type EndpointArgs (NoContentVerb method) = '[]
90+ type EndpointRes (NoContentVerb method) = NoContent
91+ reifiedEndpointArguments = V.RNil
92+
8293instance
8394 (ToReifiedEndpoint endpoint) =>
8495 ToReifiedEndpoint ((x :: Symbol) :> endpoint)
@@ -87,6 +98,16 @@ instance
8798 type EndpointRes ((x :: Symbol) :> endpoint) = EndpointRes endpoint
8899 reifiedEndpointArguments = reifiedEndpointArguments @endpoint
89100
101+instance
102+ (ToReifiedEndpoint endpoint) =>
103+ ToReifiedEndpoint (RemoteHost :> endpoint)
104+ where
105+ type EndpointArgs (RemoteHost :> endpoint) = EndpointArgs endpoint
106+ type EndpointRes (RemoteHost :> endpoint) = EndpointRes endpoint
107+ reifiedEndpointArguments = reifiedEndpointArguments @endpoint
108+
109+
110+
90111instance
91112 (ToReifiedEndpoint endpoint) =>
92113 ToReifiedEndpoint (Description s :> endpoint)
@@ -129,6 +150,24 @@ instance
129150 tagType (Argument (buildFrom @(IfRequiredLenient T.Text mods paramType)))
130151 V.:& reifiedEndpointArguments @endpoint
131152
153+
154+instance
155+ ( BuildFrom paramType
156+ , ToReifiedEndpoint endpoint
157+ , Show paramType
158+ , Eq paramType
159+ ) =>
160+ ToReifiedEndpoint (QueryParams name paramType :> endpoint)
161+ where
162+ type EndpointArgs (QueryParams name paramType :> endpoint) = [paramType] ': EndpointArgs endpoint
163+ type EndpointRes (QueryParams name paramType :> endpoint) = EndpointRes endpoint
164+ reifiedEndpointArguments =
165+ tagType (Argument (buildFrom @[paramType]))
166+ V.:& reifiedEndpointArguments @endpoint
167+
168+
169+
170+
132171instance
133172 ( BuildFrom (IfRequiredLenient T.Text mods headerType)
134173 , ToReifiedEndpoint endpoint
src/Roboservant/Types/ReifiedApi/Server.hs+4−5
@@ -6,10 +6,8 @@
66{-# LANGUAGE GADTs #-}
77{-# LANGUAGE LambdaCase #-}
88{-# LANGUAGE PolyKinds #-}
9−{-# LANGUAGE RankNTypes #-}
10−{-# LANGUAGE RecordWildCards #-}
9+
1110{-# LANGUAGE ScopedTypeVariables #-}
12−{-# LANGUAGE TupleSections #-}
1311{-# LANGUAGE TypeApplications #-}
1412{-# LANGUAGE TypeFamilies #-}
1513{-# LANGUAGE UndecidableInstances #-}
@@ -68,7 +66,8 @@ instance (Typeable x, Hashable x, Breakdown x) => NormalizeFunction (Handler x)
6866 where
6967 -- | TODO improve this
7068 renderServerError :: ServerError -> InteractionError
71− renderServerError = InteractionError . T.pack . show
69+ renderServerError s = InteractionError (T.pack $ show s) (errHTTPCode serverError == 500)
70+
7271 Right x -> pure $ Right $ breakdown x
7372
7473
@@ -93,7 +92,7 @@ instance
9392 FlattenServer (endpoint :<|> api)
9493 where
9594 flattenServer (endpoint :<|> server) = endpoint `AnEndpoint` flattenServer @api server
96−
95+
9796instance
9897 (
9998 Endpoints api ~ '[api]
stack.yaml+5−10
@@ -1,15 +1,10 @@
1−resolver: lts-16.27
1+resolver: lts-20.4
22packages:
33- .
44
55extra-deps:
6+# - dependent-map-0.4.0.0@sha256:ca2b131046f4340a1c35d138c5a003fe4a5be96b14efc26291ed35fd08c62221,1657
7+# - dependent-sum-0.7.1.0@sha256:5599aa89637db434431b1dd3fa7c34bc3d565ee44f0519bfbc877be1927c2531,2068
68- servant-flatten-0.2@sha256:276896f7c5cdec5b8f8493f6205fded0cc602d050b58fdb09a6d7c85c3bb0837,1234
7−- vinyl-0.13.0
8−- dependent-sum-0.7.1.0
9−- constraints-extras-0.3.0.2
10−- dependent-map-0.4.0.0
11−
12−- servant-0.18.2
13−- servant-client-0.18.2
14−- servant-client-core-0.18.2
15−- servant-server-0.18.2
9+# - constraints-extras-0.3.0.2@sha256:013b8d0392582c6ca068e226718a4fe8be8e22321cc0634f6115505bf377ad26,1853
10+- some-1.0.3
stack.yaml.lock+9−58
@@ -7,69 +7,20 @@ packages:
77- completed:
88 hackage: servant-flatten-0.2@sha256:276896f7c5cdec5b8f8493f6205fded0cc602d050b58fdb09a6d7c85c3bb0837,1234
99 pantry-tree:
10− size: 325
1110 sha256: 04f12c7bef2c3f9a25d94eb9489752ed498db8e243069fe95838dbb51df1dcb3
11+ size: 325
1212 original:
1313 hackage: servant-flatten-0.2@sha256:276896f7c5cdec5b8f8493f6205fded0cc602d050b58fdb09a6d7c85c3bb0837,1234
1414- completed:
15− hackage: vinyl-0.13.0@sha256:0f247cd3f8682b30881a07de18e6fec52d540646fbcb328420049cc8d63cd407,3724
16− pantry-tree:
17− size: 1857
18− sha256: 860fb95820b595161cdbdec5f376100ebae2d14e5ef0dbe311546202f7525d01
19− original:
20− hackage: vinyl-0.13.0
21−- completed:
22− hackage: dependent-sum-0.7.1.0@sha256:5599aa89637db434431b1dd3fa7c34bc3d565ee44f0519bfbc877be1927c2531,2068
23− pantry-tree:
24− size: 290
25− sha256: 9cbfb32b5a8a782b7a1c941803fd517633cb699159b851c1d82267a9e9391b50
26− original:
27− hackage: dependent-sum-0.7.1.0
28−- completed:
29− hackage: constraints-extras-0.3.0.2@sha256:013b8d0392582c6ca068e226718a4fe8be8e22321cc0634f6115505bf377ad26,1853
30− pantry-tree:
31− size: 594
32− sha256: 3ce1012bfb02e4d7def9df19ce80b8cd2b472c691b25b181d9960638673fecd1
33− original:
34− hackage: constraints-extras-0.3.0.2
35−- completed:
36− hackage: dependent-map-0.4.0.0@sha256:ca2b131046f4340a1c35d138c5a003fe4a5be96b14efc26291ed35fd08c62221,1657
37− pantry-tree:
38− size: 551
39− sha256: 5defa30010904d2ad05a036f3eaf83793506717c93cbeb599f40db1a3632cfc5
40− original:
41− hackage: dependent-map-0.4.0.0
42−- completed:
43− hackage: servant-0.18.2@sha256:f8c9f0e9891a3ada1337a3c0b369333a3b5a2d0909dd3cd09d79bc26adeaca44,5298
44− pantry-tree:
45− size: 2662
46− sha256: e930e814de1aa4d24274bdf18341a50b7ed38604ae4734f730e09238ac5bf7e2
47− original:
48− hackage: servant-0.18.2
49−- completed:
50− hackage: servant-client-0.18.2@sha256:82578ade7468873259bb2fdc9d62290a0f998550900683e1410a237ed4b05410,4591
51− pantry-tree:
52− size: 1300
53− sha256: 6324892c77bedbce32f0d6f1612fc2cb0d82c163d3be39efb951d3cc3792ce4a
54− original:
55− hackage: servant-client-0.18.2
56−- completed:
57− hackage: servant-client-core-0.18.2@sha256:ad63ae0f227373fea7e547d4c2a7b0b69e112ff409a83cbadffc9f6ee049926f,3763
58− pantry-tree:
59− size: 1444
60− sha256: 9e37bc5f8cbb70cf1accb20bd6f83fca4c2ca42472cd0fc4b22183c2c57cbe3b
61− original:
62− hackage: servant-client-core-0.18.2
63−- completed:
64− hackage: servant-server-0.18.2@sha256:56679af62ab8820a2108da6153d9ae9dde37199e62172365bdaea1458c3f7c2d,5482
15+ hackage: some-1.0.3@sha256:938cec5fc0d3a08dde34cfc0acd5a0aa2e9fd489c9dbb64417f16588dfb47af0,2399
6516 pantry-tree:
66− size: 2614
67− sha256: 3ac7430134439e4b67f0f5333f63b89d0cb7de5e2e07f0af7801c8e223942b9c
17+ sha256: b88dddc6b991cd570c6750f8d309a9feb76938b29a690abdabc4ababb8c7b2f8
18+ size: 709
6819 original:
69− hackage: servant-server-0.18.2
20+ hackage: some-1.0.3
7021snapshots:
7122- completed:
72− size: 533252
73− url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/16/27.yaml
74− sha256: c2aaae52beeacf6a5727c1010f50e89d03869abfab6d2c2658ade9da8ed50c73
75− original: lts-16.27
23+ sha256: 3770dfd79f5aed67acdcc65c4e7730adddffe6dba79ea723cfb0918356fc0f94
24+ size: 648660
25+ url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/20/4.yaml
26+ original: lts-20.4
test/Breakdown.hs+1−1
@@ -1,7 +1,7 @@
11{-# LANGUAGE DataKinds #-}
22{-# LANGUAGE DeriveGeneric #-}
33{-# LANGUAGE DerivingStrategies #-}
4−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4+
55{-# LANGUAGE OverloadedStrings #-}
66{-# LANGUAGE TypeOperators #-}
77
test/Nested.hs+3−3
@@ -1,7 +1,7 @@
11{-# LANGUAGE DataKinds #-}
2−{-# LANGUAGE DeriveGeneric #-}
2+
33{-# LANGUAGE DerivingStrategies #-}
4−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4+
55{-# LANGUAGE OverloadedStrings #-}
66{-# LANGUAGE TypeOperators #-}
77
@@ -11,7 +11,7 @@ import Servant
1111import Servant.API.Flatten
1212
1313type Api =
14− ( "one" :> Post '[JSON] Int
14+ ( "one" :> Summary "foo" :> Post '[JSON] Int
1515 :<|> "two" :> Post '[JSON] Int
1616 )
1717 :<|> ( "three" :> Post '[JSON] Int
test/Post.hs+2−2
@@ -1,8 +1,8 @@
11{-# LANGUAGE DataKinds #-}
22{-# LANGUAGE DeriveGeneric #-}
33{-# LANGUAGE DerivingStrategies #-}
4−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5−{-# LANGUAGE OverloadedStrings #-}
4+
5+
66{-# LANGUAGE TypeOperators #-}
77
88module Post where
test/Product.hs+1−1
@@ -1,7 +1,7 @@
11{-# LANGUAGE DataKinds #-}
22{-# LANGUAGE DeriveGeneric #-}
33{-# LANGUAGE DerivingStrategies #-}
4−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4+
55{-# LANGUAGE OverloadedStrings #-}
66{-# LANGUAGE TypeOperators #-}
77
test/Put.hsnew file+33
@@ -0,0 +1,33 @@
1+{-# LANGUAGE DataKinds #-}
2+{-# LANGUAGE DeriveGeneric #-}
3+{-# LANGUAGE DerivingStrategies #-}
4+
5+
6+{-# LANGUAGE TypeOperators #-}
7+
8+module Put where
9+
10+import Data.Aeson
11+import Data.Hashable
12+import GHC.Generics (Generic)
13+import Servant
14+
15+type Api =
16+ Get '[JSON] Foo
17+ :<|> ReqBody '[JSON] Foo :> Put '[JSON] ()
18+ :<|> ReqBody '[JSON] Foo :> Put '[JSON] NoContent
19+
20+data Foo = Foo
21+ deriving (Eq, Show, Generic)
22+
23+instance Hashable Foo
24+
25+instance ToJSON Foo
26+
27+instance FromJSON Foo
28+
29+server :: Server Api
30+server =
31+ pure Foo
32+ :<|> const (pure ())
33+ :<|> const (pure NoContent)
test/QueryParams.hsnew file+15
@@ -0,0 +1,15 @@
1+{-# LANGUAGE DataKinds #-}
2+
3+{-# LANGUAGE DerivingStrategies #-}
4+
5+
6+{-# LANGUAGE TypeOperators #-}
7+
8+module QueryParams where
9+
10+import Servant
11+
12+type Api = QueryParams "ints" Int :> Get '[JSON] [Int]
13+
14+server :: Server Api
15+server = pure
test/Spec.hs+39−24
@@ -21,7 +21,9 @@ import qualified Foo
2121import qualified Headers
2222import qualified Nested
2323import qualified Post
24+import qualified Put
2425import qualified Product
26+import qualified QueryParams
2527import qualified Roboservant as R
2628import qualified Roboservant.Server as RS
2729import qualified Roboservant.Client as RC
@@ -29,31 +31,32 @@ import qualified Seeded
2931import Test.Hspec
3032import Test.Hspec.Core.Spec (FailureReason (Reason), ResultStatus (Failure, Success), itemExample, mapSpecItem_, resultStatus)
3133import qualified Valid
32−import Servant(Server,Proxy(..), serve)
34+import Servant ( Server, Proxy(..), serve, Endpoints, HasServer )
35+
3336import Servant.Client(ClientEnv, mkClientEnv, baseUrlPort, parseBaseUrl,HasClient,ClientM)
3437import Network.Wai(Application)
3538import qualified Network.Wai.Handler.Warp as Warp
3639import Network.HTTP.Client (newManager, defaultManagerSettings)
37−import Servant(Endpoints,HasServer)
40+import Control.Monad((>=>))
3841
3942main :: IO ()
4043main = hspec spec
4144
4245fuzzBoth
43− :: forall a .
46+ :: forall a .
4447 (R.ToReifiedApi (Endpoints a), HasServer a '[], RS.FlattenServer a, RC.ToReifiedClientApi (Endpoints a), RC.FlattenClient a,
4548 HasClient ClientM a)
4649 => String -> Server a -> R.Config -> (Maybe R.Report -> IO ()) -> Spec
4750fuzzBoth name server config condition = do
48− it (name <> " via server") $ do
51+ it (name <> " via server") $
4952 RS.fuzz @a server config >>= condition
5053
51− around (withServer (serve (Proxy :: Proxy a) server)) $ do
54+ around (withServer (serve (Proxy :: Proxy a) server)) $
5255 it (name <> " via client") $ \(clientEnv::ClientEnv) -> do
53− RC.fuzz @a clientEnv config >>= condition
56+ RC.fuzz @a clientEnv config >>= condition
5457
5558withServer :: Application -> ActionWith ClientEnv -> IO ()
56−withServer app action = Warp.testWithApplication (pure app) (\p -> genClientEnv p >>= action)
59+withServer app action = Warp.testWithApplication (pure app) (genClientEnv >=> action)
5760 where genClientEnv port = do
5861 baseUrl <- parseBaseUrl "http://localhost"
5962 manager <- newManager defaultManagerSettings
@@ -72,37 +75,45 @@ spec = do
7275 Just (R.InsufficientCoverage _) -> True
7376 _ -> False
7477 ))
75− describe "posted body" $ do
78+ describe "posted body" $
7679 fuzzBoth @Post.Api "passes a coverage check using a posted body" Post.server R.defaultConfig {R.coverageThreshold = 0.99}
77− (`shouldSatisfy` isNothing)
78−
80+ (`shouldSatisfy` isNothing)
81+
82+
83+ describe "PUTted body" $
84+ fuzzBoth @Put.Api "passes a coverage check using a posted body" Put.server R.defaultConfig {R.coverageThreshold = 0.99}
85+ (`shouldSatisfy` isNothing)
86+
87+
7988 describe "seeded" $ do
8089 let res = Seeded.Seed 1
8190 shouldFail $ fuzzBoth @Seeded.Api "finds an error using information passed in" Seeded.server
8291 (R.defaultConfig {R.seed = [(toDyn res, hash res)]})
8392 (`shouldSatisfy` isNothing)
84−
85− describe "Foo" $ do
93+
94+ describe "Foo" $
8695 fuzzBoth @Foo.Api "finds an error in a basic app" Foo.server R.defaultConfig (`shouldSatisfy` serverFailure)
8796
97+ describe "QueryParams" $
98+ fuzzBoth @QueryParams.Api "can handle query params" QueryParams.server R.defaultConfig { R.seed = [R.hashedDyn (12::Int)] }
99+ (`shouldSatisfy` isNothing)
100+
88101 describe "BuildFrom" $ do
89− describe "headers (and sum types)" $ do
102+ describe "headers (and sum types)" $
90103 fuzzBoth @Headers.Api "should find a failure that's dependent on using header info" Headers.server R.defaultConfig
91− (`shouldSatisfy` serverFailure)
92− describe "product types" $ do
93− fuzzBoth @Product.Api "should find a failure that's dependent on creating a product" Product.server
94− R.defaultConfig {R.seed = [R.hashedDyn 'a', R.hashedDyn (1 :: Int)]}
95− (`shouldSatisfy` serverFailure)
104+ (`shouldSatisfy` serverFailure)
105+ describe "product types" $
106+ fuzzBoth @Product.Api "should find a failure that's dependent on creating a product" Product.server
107+ R.defaultConfig {R.seed = [R.hashedDyn 'a', R.hashedDyn (1 :: Int)]}
108+ (`shouldSatisfy` serverFailure)
96109 describe "Breakdown" $ do
97110 fuzzBoth @Breakdown.ProductApi "handles products" Breakdown.productServer R.defaultConfig
98111 (`shouldSatisfy` serverFailure)
99112 fuzzBoth @Breakdown.SumApi "handles sums" Breakdown.sumServer R.defaultConfig
100113 (`shouldSatisfy` serverFailure)
101− describe "flattening" $ do
102− -- we don't actually do much here, this is just here to document the appropriate response
103− -- if you get a type error with a nested api.
114+ describe "flattening" $
104115 fuzzBoth @Nested.FlatApi "can handle nested apis" Nested.server R.defaultConfig {R.coverageThreshold = 0.99}
105− (`shouldSatisfy` isNothing)
116+ (`shouldSatisfy` isNothing)
106117
107118serverFailure :: Maybe R.Report -> Bool
108119serverFailure = \case
@@ -126,9 +137,13 @@ deriving via (R.Atom Seeded.Seed) instance R.BuildFrom Seeded.Seed
126137deriving via (R.Atom Void) instance R.BuildFrom Void
127138
128139deriving via (R.Atom Post.FooPost) instance R.Breakdown Post.FooPost
129−
130140deriving via (R.Atom Post.FooPost) instance R.BuildFrom Post.FooPost
131141
142+deriving via (R.Atom Put.Foo) instance R.Breakdown Put.Foo
143+deriving via (R.Atom Put.Foo) instance R.BuildFrom Put.Foo
144+
145+
146+
132147deriving via (R.Compound Breakdown.Foo) instance R.Breakdown Breakdown.Foo
133148
134149deriving via (R.Compound Product.Foo) instance R.BuildFrom Product.Foo
@@ -143,7 +158,7 @@ shouldFail =
143158 ( \i ->
144159 i
145160 { itemExample = \p a cb -> do
146− r <- (itemExample i) p a cb
161+ r <- itemExample i p a cb
147162 pure
148163 r
149164 { resultStatus = case resultStatus r of
test/UnsafeIO.hs+3−3
@@ -1,7 +1,7 @@
11{-# LANGUAGE DataKinds #-}
2−{-# LANGUAGE DeriveGeneric #-}
2+
33{-# LANGUAGE DerivingStrategies #-}
4−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4+
55{-# LANGUAGE OverloadedStrings #-}
66{-# LANGUAGE TypeOperators #-}
77
@@ -22,7 +22,7 @@ healthcheck ref = do
2222 t <- liftIO $ readIORef ref
2323 case t of
2424 0 -> pure ()
25− n -> throwError $ err500 {errBody = "observed inconsistency: " <> (BL8.pack $ show n)}
25+ n -> throwError $ err500 {errBody = "observed inconsistency: " <> BL8.pack (show n)}
2626
2727makeServer :: IO (Server UnsafeApi)
2828makeServer = do
test/Valid.hs+2−3
@@ -1,7 +1,7 @@
11{-# LANGUAGE DataKinds #-}
22{-# LANGUAGE DeriveGeneric #-}
33{-# LANGUAGE DerivingStrategies #-}
4−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4+
55{-# LANGUAGE OverloadedStrings #-}
66{-# LANGUAGE TypeOperators #-}
77
@@ -10,7 +10,6 @@ module Valid where
1010import Data.Void
1111import GHC.Generics
1212import Servant
13−import Servant.API.Generic
1413import Servant.Server.Generic
1514
1615type Api =
@@ -21,7 +20,7 @@ data Routes route
2120 = Routes
2221 { getInt ::
2322 route
24− :- Get '[JSON] Int,
23+ :- Summary "i'm a summary!" :> Get '[JSON] Int,
2524 captureIt ::
2625 route
2726 :- Capture "void" Void :> Get '[JSON] ()