@@ -26,6 +26,7 @@ import Data.Aeson.TH as A
2626import Data.Aeson.Types
2727import Data.List
2828import Data.Maybe
29+-- import Data.Traversable
2930import Data.Typeable
3031import Language.Haskell.TH
3132import Language.Haskell.TH.Datatype
@@ -48,7 +49,7 @@ deriveApiInstances opts tname =
4849 deriveJSON (derivingOptionsToJsonOptions opts) tname
4950#endif
5051
51−-- | derive JSON and openapi3 (ToSchema) instances
52+-- | derive JSON and openapi3 (ToSchema) and Arbitrary instances
5253deriveApiAndArbitraryInstances :: DerivingOptions -> Name -> Q [Dec]
5354#ifndef ghcjs_HOST_OS
5455deriveApiAndArbitraryInstances opts tname = liftM2 (<>)
@@ -139,8 +140,14 @@ stripPrefix' :: String -> String -> String
139140stripPrefix' pfx = fromMaybe <*> stripPrefix pfx
140141
141142deriveArbitrary :: Name -> Q [Dec]
142−deriveArbitrary t = [d|
143− instance Arbitrary $(conT t) where
144− arbitrary = genericArbitrary
145− shrink = genericShrink
146− |]
143+deriveArbitrary t = do
144+ tinfo <- reifyDatatype t
145+ let
146+#if MIN_VERSION_th_abstraction(0,3,0)
147+ tvars = VarT . tvName <$> datatypeVars tinfo
148+#else
149+ tvars = datatypeVars tinfo
150+#endif
151+ ctx = AppT (ConT ''Arbitrary) <$> tvars
152+ decs <- [d| arbitrary = genericArbitrary; shrink = genericShrink |]
153+ pure [InstanceD Nothing ctx (ConT ''Arbitrary `AppT` datatypeType tinfo) decs]