git.iko.soy/typeable/swagger2
on master help sign in
Merge pull request #141 from parsonsmatt/master

NonEmpty ToSchema instance * parsonsmatt-master: Remove unused instance code use quickcheck-instances Fix tests Add semigroups build dep (squash-me) support GHC 7.x Add ToSchema NonEmpty instance for GHC 8.

commit22e975e678fe037636502fdb115f34078fdce168 authorNickolay Kudasov <nickolay.kudasov@gmail.com> dateSat 10 Mar 2018, 00:21 (9 years ago) parent5275f627765211
3 files changed+16−30
src/Data/Swagger/Internal/Schema.hs+7 swagger2.cabal+6 test/Data/Swagger/Schema/ValidationSpec.hs+3−30
src/Data/Swagger/Internal/Schema.hs+7
@@ -44,6 +44,7 @@ import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap
4444import Data.Int
4545import Data.IntSet (IntSet)
4646import Data.IntMap (IntMap)
47+import Data.List.NonEmpty (NonEmpty)
4748import Data.Map (Map)
4849import Data.Proxy
4950import Data.Scientific (Scientific)
@@ -561,6 +562,12 @@ instance ToSchema a => ToSchema (Set a) where
561562
562563instance ToSchema a => ToSchema (HashSet a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Set a))
563564
565+instance ToSchema a => ToSchema (NonEmpty a) where
566+ declareNamedSchema _ = do
567+ schema <- declareSchema (Proxy :: Proxy [a])
568+ return $ unnamed $ schema
569+ & minItems .~ Just 1
570+
564571instance ToSchema All where declareNamedSchema = plain . paramSchemaToSchema
565572instance ToSchema Any where declareNamedSchema = plain . paramSchemaToSchema
566573
swagger2.cabal+6
@@ -71,6 +71,8 @@ library
7171 , uuid-types >=1.0.2 && <1.1
7272 if !impl(ghc >= 7.10)
7373 build-depends: nats >=1.1.1 && <1.2
74+ if !impl(ghc >= 8.0)
75+ build-depends: semigroups >= 0.18.3 && <0.19
7476 default-language: Haskell2010
7577
7678test-suite spec
@@ -90,12 +92,16 @@ test-suite spec
9092 , HUnit
9193 , mtl
9294 , QuickCheck >=2.8.2
95+ , quickcheck-instances
9396 , swagger2
9497 , text
9598 , time
9699 , unordered-containers
97100 , vector
98101 , lens
102+ if !impl(ghc >= 8.0)
103+ build-depends: semigroups
104+
99105 other-modules:
100106 SpecCommon
101107 Data.SwaggerSpec
test/Data/Swagger/Schema/ValidationSpec.hs+3−30
@@ -16,6 +16,7 @@ import "unordered-containers" Data.HashSet (HashSet)
1616import qualified "unordered-containers" Data.HashSet as HashSet
1717import Data.HashMap.Strict (HashMap)
1818import qualified Data.HashMap.Strict as HashMap
19+import Data.List.NonEmpty (NonEmpty(..), nonEmpty)
1920import Data.Map (Map)
2021import Data.Monoid (mempty)
2122import Data.Proxy
@@ -32,6 +33,7 @@ import Data.Swagger.Declare
3233import Test.Hspec
3334import Test.Hspec.QuickCheck
3435import Test.QuickCheck
36+import Test.QuickCheck.Instances ()
3537
3638shouldValidate :: (ToJSON a, ToSchema a) => Proxy a -> a -> Bool
3739shouldValidate _ x = validateToJSON x == []
@@ -69,6 +71,7 @@ spec = do
6971 -- prop "(Maybe [Int])" $ shouldValidate (Proxy :: Proxy (Maybe [Int]))
7072 prop "(IntMap String)" $ shouldValidate (Proxy :: Proxy (IntMap String))
7173 prop "(Set Bool)" $ shouldValidate (Proxy :: Proxy (Set Bool))
74+ prop "(NonEmpty Bool)" $ shouldValidate (Proxy :: Proxy (NonEmpty Bool))
7275 prop "(HashSet Bool)" $ shouldValidate (Proxy :: Proxy (HashSet Bool))
7376 prop "(Either Int String)" $ shouldValidate (Proxy :: Proxy (Either Int String))
7477 prop "(Int, String)" $ shouldValidate (Proxy :: Proxy (Int, String))
@@ -228,35 +231,5 @@ invalidButtonImagesToJSON = genericToJSON defaultOptions
228231instance Arbitrary ButtonImages where
229232 arbitrary = ButtonImages <$> arbitrary
230233
231−-- Arbitrary instances for common types
232−
233−instance (Eq k, Hashable k, Arbitrary k, Arbitrary v) => Arbitrary (HashMap k v) where
234− arbitrary = HashMap.fromList <$> arbitrary
235−
236−instance (Eq a, Hashable a, Arbitrary a) => Arbitrary (HashSet a) where
237− arbitrary = HashSet.fromList <$> arbitrary
238−
239−instance Arbitrary T.Text where
240− arbitrary = T.pack <$> arbitrary
241−
242−instance Arbitrary TL.Text where
243− arbitrary = TL.pack <$> arbitrary
244−
245−instance Arbitrary Day where
246− arbitrary = liftA3 fromGregorian (fmap ((+ 1) . abs) arbitrary) arbitrary arbitrary
247−
248−instance Arbitrary LocalTime where
249− arbitrary = LocalTime
250− <$> arbitrary
251− <*> liftA3 TimeOfDay (choose (0, 23)) (choose (0, 59)) (fromInteger <$> choose (0, 60))
252−
253234instance Eq ZonedTime where
254235 ZonedTime t (TimeZone x _ _) == ZonedTime t' (TimeZone y _ _) = t == t' && x == y
255−
256−instance Arbitrary ZonedTime where
257− arbitrary = ZonedTime
258− <$> arbitrary
259− <*> liftA3 TimeZone arbitrary arbitrary (vectorOf 3 (elements ['A'..'Z']))
260−
261−instance Arbitrary UTCTime where
262− arbitrary = UTCTime <$> arbitrary <*> fmap fromInteger (choose (0, 86400))