@@ -7,17 +7,18 @@ module Data.Swagger.SchemaSpec where
77import Prelude ()
88import Prelude.Compat
99
10+import Control.Lens ((^.))
1011import Data.Aeson
1112import Data.Aeson.QQ
1213import Data.Char
1314import qualified Data.HashMap.Strict as HashMap
15+import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap
1416import Data.Proxy
1517import Data.Set (Set)
1618import qualified Data.Set as Set
1719import qualified Data.Text as Text
1820import GHC.Generics
1921
20−import qualified Data.Swagger.OrdHashMap as OrdHashMap
2122import Data.Swagger
2223import Data.Swagger.Declare
2324
@@ -35,10 +36,17 @@ checkSchemaName sname proxy =
3536checkDefs :: ToSchema a => Proxy a -> [String] -> Spec
3637checkDefs proxy names =
3738 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
3940 where
4041 defs = execDeclare (declareNamedSchema proxy) mempty
4142
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
47+ where
48+ fields = toSchema proxy ^. properties
49+
4250checkInlinedSchema :: ToSchema a => Proxy a -> Value -> Spec
4351checkInlinedSchema proxy js = toInlinedSchema proxy <=> js
4452
@@ -59,6 +67,9 @@ spec = do
5967 context "Person" $ checkToSchema (Proxy :: Proxy Person) personSchemaJSON
6068 context "ISPair" $ checkToSchema (Proxy :: Proxy ISPair) ispairSchemaJSON
6169 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
6273 context "Color (bounded enum)" $ checkToSchema (Proxy :: Proxy Color) colorSchemaJSON
6374 context "Shade (paramSchemaToNamedSchema)" $ checkToSchema (Proxy :: Proxy Shade) shadeSchemaJSON
6475 context "Paint (record with bounded enum field)" $ checkToSchema (Proxy :: Proxy Paint) paintSchemaJSON
@@ -144,6 +155,7 @@ ispairSchemaJSON = [aesonQQ|
144155-- ========================================================================
145156-- Point (record data type with custom fieldLabelModifier)
146157-- ========================================================================
158+
147159data Point = Point
148160 { pointX :: Double
149161 , pointY :: Double
@@ -166,6 +178,40 @@ pointSchemaJSON = [aesonQQ|
166178}
167179|]
168180
181+-- ========================================================================
182+-- Point (record data type with multiple fields)
183+-- ========================================================================
184+
185+data Point5 = Point5
186+ { point5X :: Double
187+ , point5Y :: Double
188+ , point5Z :: Double
189+ , point5U :: Double
190+ , point5V :: Double -- 5 dimensional!
191+ } deriving (Generic)
192+
193+instance ToSchema Point5 where
194+ declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
195+ { fieldLabelModifier = map toLower . drop (length "point5") }
196+
197+point5SchemaJSON :: Value
198+point5SchemaJSON = [aesonQQ|
199+{
200+ "type": "object",
201+ "properties":
202+ {
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" }
208+ },
209+ "required": ["x", "y", "z", "u", "v"]
210+}
211+|]
212+
213+point5Properties :: [String]
214+point5Properties = ["x", "y", "z", "u", "v"]
169215
170216-- ========================================================================
171217-- Color (enum)