@@ -1,14 +1,34 @@
1+{-# LANGUAGE OverloadedLists #-}
1{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE QuasiQuotes #-}
3{-# LANGUAGE QuasiQuotes #-}
3module Data.SwaggerSpec where
4module Data.SwaggerSpec where
5import Data.Aeson
6import Data.Aeson
6import Data.Aeson.QQ
7import Data.Aeson.QQ
8+import qualified Data.HashMap.Strict as HashMap
10+import qualified Data.Vector as Vector
7import Data.Swagger
12import Data.Swagger
9import Test.Hspec
14import Test.Hspec
10import Test.Hspec.HUnit
15import Test.Hspec.HUnit
17+isSubJSON :: Value -> Value -> Bool
18+isSubJSON Null _ = True
19+isSubJSON (Object x) (Object y) = HashMap.keys x == HashMap.keys i && and i
21+ i = HashMap.intersectionWith isSubJSON x y
22+isSubJSON (Array xs) (Array ys) = Vector.length xs == Vector.length ys && and (Vector.zipWith isSubJSON xs ys)
23+isSubJSON x y = x == y
25+(<~>) :: (Eq a, Show a, ToJSON a, FromJSON a) => a -> Value -> Spec
27+ it "encodes correctly (probably with extra properties)" $ do
28+ toJSON x `shouldSatisfy` (json `isSubJSON`)
29+ it "decodes correctly" $ do
30+ fromJSON json `shouldBe` Success x
12(<=>) :: (Eq a, Show a, ToJSON a, FromJSON a) => a -> Value -> Spec
32(<=>) :: (Eq a, Show a, ToJSON a, FromJSON a) => a -> Value -> Spec
13x <=> json = do
33x <=> json = do
14 it "encodes correctly" $ do
34 it "encodes correctly" $ do
@@ -18,6 +38,7 @@ x <=> json = do
19spec :: Spec
39spec :: Spec
41+ describe "Operation Object" $ operationExample <~> operationExampleJSON
21 describe "License Object" $ licenseExample <=> licenseExampleJSON
42 describe "License Object" $ licenseExample <=> licenseExampleJSON
22 describe "Contact Object" $ contactExample <=> contactExampleJSON
43 describe "Contact Object" $ contactExample <=> contactExampleJSON
23 describe "Info Object" $ infoExample <=> infoExampleJSON
44 describe "Info Object" $ infoExample <=> infoExampleJSON
@@ -96,3 +117,141 @@ licenseExampleJSON = [aesonQQ|
96 "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
117 "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
122+-- =======================================================================
124+-- =======================================================================
126+operationExample :: SwaggerOperation
127+operationExample = SwaggerOperation
128+ { swaggerOperationTags = ["pet"]
129+ , swaggerOperationSummary = Just "Updates a pet in the store with form data"
130+ , swaggerOperationDescription = Just ""
131+ , swaggerOperationOperationId = Just "updatePetWithForm"
132+ , swaggerOperationConsumes = Just (SwaggerMimeList ["application/x-www-form-urlencoded"])
133+ , swaggerOperationProduces = Just (SwaggerMimeList ["application/json", "application/xml"])
134+ , swaggerOperationParameters = params
135+ , swaggerOperationResponses = responses
136+ , swaggerOperationSecurity = security
137+ , swaggerOperationExternalDocs = Nothing
138+ , swaggerOperationSchemes = Nothing
139+ , swaggerOperationDeprecated = False
142+ security = [SwaggerSecurityRequirement [("petstore_auth", ["write:pets", "read:pets"])]]
144+ responses = SwaggerResponses
145+ { swaggerResponsesDefault = Nothing
146+ , swaggerResponsesResponses =
147+ [ (200, SwaggerResponse
148+ { swaggerResponseDescription = "Pet updated."
149+ , swaggerResponseSchema = Nothing
150+ , swaggerResponseHeaders = mempty
151+ , swaggerResponseExamples = Nothing } )
152+ , (405, SwaggerResponse
153+ { swaggerResponseDescription = "Invalid input"
154+ , swaggerResponseSchema = Nothing
155+ , swaggerResponseHeaders = mempty
156+ , swaggerResponseExamples = Nothing } ) ] }
160+ { swaggerParameterName = "petId"
161+ , swaggerParameterDescription = Just "ID of pet that needs to be updated"
162+ , swaggerParameterRequired = True
163+ , swaggerParameterSchema = SwaggerParameterOther (stringSchema SwaggerParameterPath) }
165+ { swaggerParameterName = "name"
166+ , swaggerParameterDescription = Just "Updated name of the pet"
167+ , swaggerParameterRequired = False
168+ , swaggerParameterSchema = SwaggerParameterOther (stringSchema SwaggerParameterFormData) }
170+ { swaggerParameterName = "status"
171+ , swaggerParameterDescription = Just "Updated status of the pet"
172+ , swaggerParameterRequired = False
173+ , swaggerParameterSchema = SwaggerParameterOther (stringSchema SwaggerParameterFormData) }
176+ stringSchema i = SwaggerParameterOtherSchema
177+ { swaggerParameterOtherSchemaIn = i
178+ , swaggerParameterOtherSchemaType = SwaggerParamString
179+ , swaggerParameterOtherSchemaFormat = Nothing
180+ , swaggerParameterOtherSchemaAllowEmptyValue = False
181+ , swaggerParameterOtherSchemaItems = Nothing
182+ , swaggerParameterOtherSchemaCollectionFormat = Nothing
183+ , swaggerParameterOtherSchemaCommon = emptySchemaCommon
186+ emptySchemaCommon = SwaggerSchemaCommon
187+ { swaggerSchemaDefault = Nothing
188+ , swaggerSchemaMaximum = Nothing
189+ , swaggerSchemaExclusiveMaximum = Nothing
190+ , swaggerSchemaMinimum = Nothing
191+ , swaggerSchemaExclusiveMinimum = Nothing
192+ , swaggerSchemaMaxLength = Nothing
193+ , swaggerSchemaMinLength = Nothing
194+ , swaggerSchemaPattern = Nothing
195+ , swaggerSchemaMaxItems = Nothing
196+ , swaggerSchemaMinItems = Nothing
197+ , swaggerSchemaUniqueItems = Nothing
198+ , swaggerSchemaEnum = Nothing
199+ , swaggerSchemaMultipleOf = Nothing }
201+operationExampleJSON :: Value
202+operationExampleJSON = [aesonQQ|
207+ "summary": "Updates a pet in the store with form data",
209+ "operationId": "updatePetWithForm",
211+ "application/x-www-form-urlencoded"
221+ "description": "ID of pet that needs to be updated",
228+ "description": "Updated name of the pet",
235+ "description": "Updated status of the pet",
242+ "description": "Pet updated."
245+ "description": "Invalid input"