@@ -26,6 +26,7 @@ import Data.Aeson.TH as A
26import Data.Aeson.Types
26import Data.Aeson.Types
27import Data.List
27import Data.List
28import Data.Maybe
28import Data.Maybe
29+-- import Data.Traversable
29import Data.Typeable
30import Data.Typeable
30import Language.Haskell.TH
31import Language.Haskell.TH
31import Language.Haskell.TH.Datatype
32import Language.Haskell.TH.Datatype
@@ -48,7 +49,7 @@ deriveApiInstances opts tname =
48 deriveJSON (derivingOptionsToJsonOptions opts) tname
49 deriveJSON (derivingOptionsToJsonOptions opts) tname
51−-- | derive JSON and openapi3 (ToSchema) instances
52+-- | derive JSON and openapi3 (ToSchema) and Arbitrary instances
52deriveApiAndArbitraryInstances :: DerivingOptions -> Name -> Q [Dec]
53deriveApiAndArbitraryInstances :: DerivingOptions -> Name -> Q [Dec]
53#ifndef ghcjs_HOST_OS
54#ifndef ghcjs_HOST_OS
54deriveApiAndArbitraryInstances opts tname = liftM2 (<>)
55deriveApiAndArbitraryInstances opts tname = liftM2 (<>)
@@ -139,8 +140,14 @@ stripPrefix' :: String -> String -> String
139stripPrefix' pfx = fromMaybe <*> stripPrefix pfx
140stripPrefix' pfx = fromMaybe <*> stripPrefix pfx
141deriveArbitrary :: Name -> Q [Dec]
142deriveArbitrary :: Name -> Q [Dec]
142−deriveArbitrary t = [d|
143+deriveArbitrary t = do
143− instance Arbitrary $(conT t) where
144+ tinfo <- reifyDatatype t
146+#if MIN_VERSION_th_abstraction(0,3,0)
147+ tvars = VarT . tvName <$> datatypeVars tinfo
149+ tvars = datatypeVars tinfo
151+ ctx = AppT (ConT ''Arbitrary) <$> tvars
144− arbitrary = genericArbitrary
152+ decs <- [d| arbitrary = genericArbitrary; shrink = genericShrink |]
145− shrink = genericShrink
153+ pure [InstanceD Nothing ctx (ConT ''Arbitrary `AppT` datatypeType tinfo) decs]