git.iko.soy/typeable/swagger2
on master help sign in
Merge pull request #141 from parsonsmatt/master
22e975eNickolay Kudasov
src/Data/Swagger/Internal/Schema.hs+7
@@ -44,6 +44,7 @@ import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap
44import Data.Int
44import Data.Int
45import Data.IntSet (IntSet)
45import Data.IntSet (IntSet)
46import Data.IntMap (IntMap)
46import Data.IntMap (IntMap)
47+import Data.List.NonEmpty (NonEmpty)
47import Data.Map (Map)
48import Data.Map (Map)
48import Data.Proxy
49import Data.Proxy
49import Data.Scientific (Scientific)
50import Data.Scientific (Scientific)
@@ -561,6 +562,12 @@ instance ToSchema a => ToSchema (Set a) where
561
562
562instance ToSchema a => ToSchema (HashSet a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Set a))
563instance ToSchema a => ToSchema (HashSet a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Set a))
563
564
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+
564instance ToSchema All where declareNamedSchema = plain . paramSchemaToSchema
571instance ToSchema All where declareNamedSchema = plain . paramSchemaToSchema
565instance ToSchema Any where declareNamedSchema = plain . paramSchemaToSchema
572instance ToSchema Any where declareNamedSchema = plain . paramSchemaToSchema
566
573
swagger2.cabal+6
@@ -71,6 +71,8 @@ library
71 , uuid-types >=1.0.2 && <1.1
71 , uuid-types >=1.0.2 && <1.1
72 if !impl(ghc >= 7.10)
72 if !impl(ghc >= 7.10)
73 build-depends: nats >=1.1.1 && <1.2
73 build-depends: nats >=1.1.1 && <1.2
74+ if !impl(ghc >= 8.0)
75+ build-depends: semigroups >= 0.18.3 && <0.19
74 default-language: Haskell2010
76 default-language: Haskell2010
75
77
76test-suite spec
78test-suite spec
@@ -90,12 +92,16 @@ test-suite spec
90 , HUnit
92 , HUnit
91 , mtl
93 , mtl
92 , QuickCheck >=2.8.2
94 , QuickCheck >=2.8.2
95+ , quickcheck-instances
93 , swagger2
96 , swagger2
94 , text
97 , text
95 , time
98 , time
96 , unordered-containers
99 , unordered-containers
97 , vector
100 , vector
98 , lens
101 , lens
102+ if !impl(ghc >= 8.0)
103+ build-depends: semigroups
104+
99 other-modules:
105 other-modules:
100 SpecCommon
106 SpecCommon
101 Data.SwaggerSpec
107 Data.SwaggerSpec
test/Data/Swagger/Schema/ValidationSpec.hs+3−30
@@ -16,6 +16,7 @@ import "unordered-containers" Data.HashSet (HashSet)
16import qualified "unordered-containers" Data.HashSet as HashSet
16import qualified "unordered-containers" Data.HashSet as HashSet
17import Data.HashMap.Strict (HashMap)
17import Data.HashMap.Strict (HashMap)
18import qualified Data.HashMap.Strict as HashMap
18import qualified Data.HashMap.Strict as HashMap
19+import Data.List.NonEmpty (NonEmpty(..), nonEmpty)
19import Data.Map (Map)
20import Data.Map (Map)
20import Data.Monoid (mempty)
21import Data.Monoid (mempty)
21import Data.Proxy
22import Data.Proxy
@@ -32,6 +33,7 @@ import Data.Swagger.Declare
32import Test.Hspec
33import Test.Hspec
33import Test.Hspec.QuickCheck
34import Test.Hspec.QuickCheck
34import Test.QuickCheck
35import Test.QuickCheck
36+import Test.QuickCheck.Instances ()
35
37
36shouldValidate :: (ToJSON a, ToSchema a) => Proxy a -> a -> Bool
38shouldValidate :: (ToJSON a, ToSchema a) => Proxy a -> a -> Bool
37shouldValidate _ x = validateToJSON x == []
39shouldValidate _ x = validateToJSON x == []
@@ -69,6 +71,7 @@ spec = do
69 -- prop "(Maybe [Int])" $ shouldValidate (Proxy :: Proxy (Maybe [Int]))
71 -- prop "(Maybe [Int])" $ shouldValidate (Proxy :: Proxy (Maybe [Int]))
70 prop "(IntMap String)" $ shouldValidate (Proxy :: Proxy (IntMap String))
72 prop "(IntMap String)" $ shouldValidate (Proxy :: Proxy (IntMap String))
71 prop "(Set Bool)" $ shouldValidate (Proxy :: Proxy (Set Bool))
73 prop "(Set Bool)" $ shouldValidate (Proxy :: Proxy (Set Bool))
74+ prop "(NonEmpty Bool)" $ shouldValidate (Proxy :: Proxy (NonEmpty Bool))
72 prop "(HashSet Bool)" $ shouldValidate (Proxy :: Proxy (HashSet Bool))
75 prop "(HashSet Bool)" $ shouldValidate (Proxy :: Proxy (HashSet Bool))
73 prop "(Either Int String)" $ shouldValidate (Proxy :: Proxy (Either Int String))
76 prop "(Either Int String)" $ shouldValidate (Proxy :: Proxy (Either Int String))
74 prop "(Int, String)" $ shouldValidate (Proxy :: Proxy (Int, String))
77 prop "(Int, String)" $ shouldValidate (Proxy :: Proxy (Int, String))
@@ -228,35 +231,5 @@ invalidButtonImagesToJSON = genericToJSON defaultOptions
228instance Arbitrary ButtonImages where
231instance Arbitrary ButtonImages where
229 arbitrary = ButtonImages <$> arbitrary
232 arbitrary = ButtonImages <$> arbitrary
230
233
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−
253instance Eq ZonedTime where
234instance Eq ZonedTime where
254 ZonedTime t (TimeZone x _ _) == ZonedTime t' (TimeZone y _ _) = t == t' && x == y
235 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))