@@ -7,17 +7,18 @@ module Data.Swagger.SchemaSpec where
7import Prelude ()
7import Prelude ()
8import Prelude.Compat
8import Prelude.Compat
10+import Control.Lens ((^.))
10import Data.Aeson
11import Data.Aeson
11import Data.Aeson.QQ
12import Data.Aeson.QQ
12import Data.Char
13import Data.Char
13import qualified Data.HashMap.Strict as HashMap
14import qualified Data.HashMap.Strict as HashMap
15+import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap
14import Data.Proxy
16import Data.Proxy
15import Data.Set (Set)
17import Data.Set (Set)
16import qualified Data.Set as Set
18import qualified Data.Set as Set
17import qualified Data.Text as Text
19import qualified Data.Text as Text
18import GHC.Generics
20import GHC.Generics
20−import qualified Data.Swagger.OrdHashMap as OrdHashMap
21import Data.Swagger
22import Data.Swagger
22import Data.Swagger.Declare
23import Data.Swagger.Declare
@@ -35,10 +36,17 @@ checkSchemaName sname proxy =
35checkDefs :: ToSchema a => Proxy a -> [String] -> Spec
36checkDefs :: ToSchema a => Proxy a -> [String] -> Spec
36checkDefs proxy names =
37checkDefs proxy names =
37 it ("uses these definitions " ++ show names) $
38 it ("uses these definitions " ++ show names) $
38− Set.fromList (OrdHashMap.keys defs) `shouldBe` Set.fromList (map Text.pack names)
39+ InsOrdHashMap.keys defs `shouldBe` map Text.pack names
40 defs = execDeclare (declareNamedSchema proxy) mempty
41 defs = execDeclare (declareNamedSchema proxy) mempty
43+checkProperties :: ToSchema a => Proxy a -> [String] -> Spec
44+checkProperties proxy names =
45+ it ("has these fields in order " ++ show names) $
46+ InsOrdHashMap.keys fields `shouldBe` map Text.pack names
48+ fields = toSchema proxy ^. properties
42checkInlinedSchema :: ToSchema a => Proxy a -> Value -> Spec
50checkInlinedSchema :: ToSchema a => Proxy a -> Value -> Spec
43checkInlinedSchema proxy js = toInlinedSchema proxy <=> js
51checkInlinedSchema proxy js = toInlinedSchema proxy <=> js
@@ -59,6 +67,9 @@ spec = do
59 context "Person" $ checkToSchema (Proxy :: Proxy Person) personSchemaJSON
67 context "Person" $ checkToSchema (Proxy :: Proxy Person) personSchemaJSON
60 context "ISPair" $ checkToSchema (Proxy :: Proxy ISPair) ispairSchemaJSON
68 context "ISPair" $ checkToSchema (Proxy :: Proxy ISPair) ispairSchemaJSON
61 context "Point (fieldLabelModifier)" $ checkToSchema (Proxy :: Proxy Point) pointSchemaJSON
69 context "Point (fieldLabelModifier)" $ checkToSchema (Proxy :: Proxy Point) pointSchemaJSON
70+ context "Point5 (many field record)" $ do
71+ checkToSchema (Proxy :: Proxy Point5) point5SchemaJSON
72+ checkProperties (Proxy :: Proxy Point5) point5Properties
62 context "Color (bounded enum)" $ checkToSchema (Proxy :: Proxy Color) colorSchemaJSON
73 context "Color (bounded enum)" $ checkToSchema (Proxy :: Proxy Color) colorSchemaJSON
63 context "Shade (paramSchemaToNamedSchema)" $ checkToSchema (Proxy :: Proxy Shade) shadeSchemaJSON
74 context "Shade (paramSchemaToNamedSchema)" $ checkToSchema (Proxy :: Proxy Shade) shadeSchemaJSON
64 context "Paint (record with bounded enum field)" $ checkToSchema (Proxy :: Proxy Paint) paintSchemaJSON
75 context "Paint (record with bounded enum field)" $ checkToSchema (Proxy :: Proxy Paint) paintSchemaJSON
@@ -144,6 +155,7 @@ ispairSchemaJSON = [aesonQQ|
144-- ========================================================================
155-- ========================================================================
145-- Point (record data type with custom fieldLabelModifier)
156-- Point (record data type with custom fieldLabelModifier)
146-- ========================================================================
157-- ========================================================================
147data Point = Point
159data Point = Point
148 { pointX :: Double
160 { pointX :: Double
149 , pointY :: Double
161 , pointY :: Double
@@ -166,6 +178,40 @@ pointSchemaJSON = [aesonQQ|
181+-- ========================================================================
182+-- Point (record data type with multiple fields)
183+-- ========================================================================
190+ , point5V :: Double -- 5 dimensional!
191+ } deriving (Generic)
193+instance ToSchema Point5 where
194+ declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
195+ { fieldLabelModifier = map toLower . drop (length "point5") }
197+point5SchemaJSON :: Value
198+point5SchemaJSON = [aesonQQ|
203+ "x": { "type": "number", "format": "double" },
204+ "y": { "type": "number", "format": "double" },
205+ "z": { "type": "number", "format": "double" },
206+ "u": { "type": "number", "format": "double" },
207+ "v": { "type": "number", "format": "double" }
209+ "required": ["x", "y", "z", "u", "v"]
213+point5Properties :: [String]
214+point5Properties = ["x", "y", "z", "u", "v"]
170-- ========================================================================
216-- ========================================================================
171-- Color (enum)
217-- Color (enum)