git.iko.soy/typeable/openapi3
on master help sign in
Add tests for declareSchemaBoundedEnumMapping
c09a11dNickolay Kudasov
test/Data/Swagger/Schema/ValidationSpec.hs+38−4
@@ -85,12 +85,14 @@ spec = do
85 prop "Paint" $ shouldValidate (Proxy :: Proxy Paint)
85 prop "Paint" $ shouldValidate (Proxy :: Proxy Paint)
86 prop "MyRoseTree" $ shouldValidate (Proxy :: Proxy MyRoseTree)
86 prop "MyRoseTree" $ shouldValidate (Proxy :: Proxy MyRoseTree)
87 prop "Light" $ shouldValidate (Proxy :: Proxy Light)
87 prop "Light" $ shouldValidate (Proxy :: Proxy Light)
88+ prop "ButtonImages" $ shouldValidate (Proxy :: Proxy ButtonImages)
88
89
89 describe "invalid cases" $ do
90 describe "invalid cases" $ do
90− prop "invalidPersonToJSON" $ shouldNotValidate invalidPersonToJSON
91+ prop "invalidPersonToJSON" $ shouldNotValidate invalidPersonToJSON
91− prop "invalidColorToJSON" $ shouldNotValidate invalidColorToJSON
92+ prop "invalidColorToJSON" $ shouldNotValidate invalidColorToJSON
92− prop "invalidPaintToJSON" $ shouldNotValidate invalidPaintToJSON
93+ prop "invalidPaintToJSON" $ shouldNotValidate invalidPaintToJSON
93− prop "invalidLightToJSON" $ shouldNotValidate invalidLightToJSON
94+ prop "invalidLightToJSON" $ shouldNotValidate invalidLightToJSON
95+ prop "invalidButtonImagesToJSON" $ shouldNotValidate invalidButtonImagesToJSON
94
96
95main :: IO ()
97main :: IO ()
96main = hspec spec
98main = hspec spec
@@ -193,6 +195,38 @@ instance Arbitrary Light where
193invalidLightToJSON :: Light -> Value
195invalidLightToJSON :: Light -> Value
194invalidLightToJSON = genericToJSON defaultOptions
196invalidLightToJSON = genericToJSON defaultOptions
195
197
198+-- ========================================================================
199+-- ButtonImages (bounded enum key mapping)
200+-- ========================================================================
201+
202+data ButtonState = Neutral | Focus | Active | Hover | Disabled
203+ deriving (Show, Eq, Ord, Bounded, Enum, Generic)
204+
205+instance ToJSON ButtonState
206+instance ToSchema ButtonState
207+instance ToJSONKey ButtonState where toJSONKey = toJSONKeyText (T.pack . show)
208+
209+instance Arbitrary ButtonState where
210+ arbitrary = arbitraryBoundedEnum
211+
212+type ImageUrl = T.Text
213+
214+newtype ButtonImages = ButtonImages { getButtonImages :: Map ButtonState ImageUrl }
215+ deriving (Show, Generic)
216+
217+instance ToJSON ButtonImages where
218+ toJSON = toJSON . getButtonImages
219+
220+instance ToSchema ButtonImages where
221+ declareNamedSchema = genericDeclareNamedSchemaNewtype defaultSchemaOptions
222+ declareSchemaBoundedEnumKeyMapping
223+
224+invalidButtonImagesToJSON :: ButtonImages -> Value
225+invalidButtonImagesToJSON = genericToJSON defaultOptions
226+
227+instance Arbitrary ButtonImages where
228+ arbitrary = ButtonImages <$> arbitrary
229+
196-- Arbitrary instances for common types
230-- Arbitrary instances for common types
197
231
198instance (Eq k, Hashable k, Arbitrary k, Arbitrary v) => Arbitrary (HashMap k v) where
232instance (Eq k, Hashable k, Arbitrary k, Arbitrary v) => Arbitrary (HashMap k v) where
test/Data/Swagger/SchemaSpec.hs+44−1
@@ -8,12 +8,14 @@ import Prelude ()
8import Prelude.Compat
8import Prelude.Compat
9
9
10import Control.Lens ((^.))
10import Control.Lens ((^.))
11−import Data.Aeson (Value)
11+import Data.Aeson (Value, ToJSON(..), ToJSONKey(..))
12+import Data.Aeson.Types (toJSONKeyText)
12import Data.Aeson.QQ
13import Data.Aeson.QQ
13import Data.Char
14import Data.Char
14import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap
15import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap
15import Data.Proxy
16import Data.Proxy
16import Data.Set (Set)
17import Data.Set (Set)
18+import Data.Map (Map)
17import qualified Data.Text as Text
19import qualified Data.Text as Text
18import GHC.Generics
20import GHC.Generics
19
21
@@ -101,6 +103,8 @@ spec = do
101 context "Character (inlining only Player)" $ checkInlinedSchemas ["Player"] (Proxy :: Proxy Character) characterInlinedPlayerSchemaJSON
103 context "Character (inlining only Player)" $ checkInlinedSchemas ["Player"] (Proxy :: Proxy Character) characterInlinedPlayerSchemaJSON
102 context "Light" $ checkInlinedSchema (Proxy :: Proxy Light) lightInlinedSchemaJSON
104 context "Light" $ checkInlinedSchema (Proxy :: Proxy Light) lightInlinedSchemaJSON
103 context "MyRoseTree (inlineNonRecursiveSchemas)" $ checkInlinedRecSchema (Proxy :: Proxy MyRoseTree) myRoseTreeSchemaJSON
105 context "MyRoseTree (inlineNonRecursiveSchemas)" $ checkInlinedRecSchema (Proxy :: Proxy MyRoseTree) myRoseTreeSchemaJSON
106+ describe "Bounded Enum key mapping" $ do
107+ context "ButtonImages" $ checkToSchema (Proxy :: Proxy ButtonImages) buttonImagesSchemaJSON
104
108
105main :: IO ()
109main :: IO ()
106main = hspec spec
110main = hspec spec
@@ -647,3 +651,42 @@ instance ToSchema Id
647
651
648newtype ResourceId = ResourceId Id deriving (Generic)
652newtype ResourceId = ResourceId Id deriving (Generic)
649instance ToSchema ResourceId
653instance ToSchema ResourceId
654+
655+-- ========================================================================
656+-- ButtonImages (bounded enum key mapping)
657+-- ========================================================================
658+
659+data ButtonState = Neutral | Focus | Active | Hover | Disabled
660+ deriving (Show, Bounded, Enum, Generic)
661+
662+instance ToJSON ButtonState
663+instance ToSchema ButtonState
664+instance ToJSONKey ButtonState where toJSONKey = toJSONKeyText (Text.pack . show)
665+
666+type ImageUrl = Text.Text
667+
668+newtype ButtonImages = ButtonImages { getButtonImages :: Map ButtonState ImageUrl }
669+ deriving (Generic)
670+
671+instance ToJSON ButtonImages where
672+ toJSON = toJSON . getButtonImages
673+
674+instance ToSchema ButtonImages where
675+ declareNamedSchema = genericDeclareNamedSchemaNewtype defaultSchemaOptions
676+ declareSchemaBoundedEnumKeyMapping
677+
678+buttonImagesSchemaJSON :: Value
679+buttonImagesSchemaJSON = [aesonQQ|
680+{
681+ "type": "object",
682+ "properties":
683+ {
684+ "Neutral": { "type": "string" },
685+ "Focus": { "type": "string" },
686+ "Active": { "type": "string" },
687+ "Hover": { "type": "string" },
688+ "Disabled": { "type": "string" }
689+ }
690+}
691+|]
692+