git.iko.soy/typeable/swagger2
on master help sign in
Validation test for additionalProperties = true
42f69fdMoss Prescott
test/Data/Swagger/Schema/ValidationSpec.hs+27−1
@@ -7,6 +7,7 @@
7module Data.Swagger.Schema.ValidationSpec where
7module Data.Swagger.Schema.ValidationSpec where
8
8
9import Control.Applicative
9import Control.Applicative
10+import Control.Lens ((&), (.~), (?~))
10import Data.Aeson
11import Data.Aeson
11import Data.Aeson.Types
12import Data.Aeson.Types
12import Data.Int
13import Data.Int
@@ -17,7 +18,7 @@ import qualified "unordered-containers" Data.HashSet as HashSet
17import Data.HashMap.Strict (HashMap)
18import Data.HashMap.Strict (HashMap)
18import qualified Data.HashMap.Strict as HashMap
19import qualified Data.HashMap.Strict as HashMap
19import Data.List.NonEmpty.Compat (NonEmpty(..), nonEmpty)
20import Data.List.NonEmpty.Compat (NonEmpty(..), nonEmpty)
20−import Data.Map (Map)
21+import Data.Map (Map, fromList)
21import Data.Monoid (mempty)
22import Data.Monoid (mempty)
22import Data.Proxy
23import Data.Proxy
23import Data.Time
24import Data.Time
@@ -92,6 +93,7 @@ spec = do
92 prop "Light" $ shouldValidate (Proxy :: Proxy Light)
93 prop "Light" $ shouldValidate (Proxy :: Proxy Light)
93 prop "ButtonImages" $ shouldValidate (Proxy :: Proxy ButtonImages)
94 prop "ButtonImages" $ shouldValidate (Proxy :: Proxy ButtonImages)
94 prop "Version" $ shouldValidate (Proxy :: Proxy Version)
95 prop "Version" $ shouldValidate (Proxy :: Proxy Version)
96+ prop "FreeForm" $ shouldValidate (Proxy :: Proxy FreeForm)
95
97
96 describe "invalid cases" $ do
98 describe "invalid cases" $ do
97 prop "invalidPersonToJSON" $ shouldNotValidate invalidPersonToJSON
99 prop "invalidPersonToJSON" $ shouldNotValidate invalidPersonToJSON
@@ -233,5 +235,29 @@ invalidButtonImagesToJSON = genericToJSON defaultOptions
233instance Arbitrary ButtonImages where
235instance Arbitrary ButtonImages where
234 arbitrary = ButtonImages <$> arbitrary
236 arbitrary = ButtonImages <$> arbitrary
235
237
238+-- ========================================================================
239+-- FreeForm (wraps a raw JSON Value)
240+-- ========================================================================
241+
242+data FreeForm = FreeForm { jsonContent :: Map T.Text Value }
243+ deriving (Show, Generic)
244+
245+instance ToJSON FreeForm where
246+ toJSON = toJSON . jsonContent
247+
248+instance ToSchema FreeForm where
249+ declareNamedSchema _ = pure $ NamedSchema (Just $ T.pack "FreeForm") $ mempty
250+ & type_ .~ SwaggerObject
251+ & additionalProperties ?~ AdditionalPropertiesAllowed True
252+
253+instance Arbitrary FreeForm where
254+ arbitrary = (FreeForm . fromList) <$> genObj
255+ where
256+ genObj = listOf $ do
257+ k <- arbitrary
258+ v <- oneof [ String <$> arbitrary, Number <$> arbitrary, Bool <$> arbitrary, pure Null ]
259+ pure (k, v)
260+
236instance Eq ZonedTime where
261instance Eq ZonedTime where
237 ZonedTime t (TimeZone x _ _) == ZonedTime t' (TimeZone y _ _) = t == t' && x == y
262 ZonedTime t (TimeZone x _ _) == ZonedTime t' (TimeZone y _ _) = t == t' && x == y
263+