1{-# LANGUAGE OverloadedLists #-}
2{-# LANGUAGE OverloadedStrings #-}
3{-# LANGUAGE ScopedTypeVariables #-}
4{-# LANGUAGE QuasiQuotes #-}
5module Data.SwaggerSpec where
14import Data.HashMap.Strict (HashMap)
15import Data.Text (Text)
19import Test.Hspec hiding (example)
23 describe "License Object" $ licenseExample <=> licenseExampleJSON
24 describe "Contact Object" $ contactExample <=> contactExampleJSON
25 describe "Info Object" $ infoExample <=> infoExampleJSON
26 describe "Operation Object" $ operationExample <=> operationExampleJSON
27 describe "Schema Object" $ do
28 context "Primitive Sample" $ schemaPrimitiveExample <=> schemaPrimitiveExampleJSON
29 context "Simple Model" $ schemaSimpleModelExample <=> schemaSimpleModelExampleJSON
30 context "Model with Map/Dictionary Properties" $ schemaModelDictExample <=> schemaModelDictExampleJSON
31 context "Model with Example" $ schemaWithExampleExample <=> schemaWithExampleExampleJSON
32 describe "Definitions Object" $ definitionsExample <=> definitionsExampleJSON
33 describe "Parameters Definition Object" $ paramsDefinitionExample <=> paramsDefinitionExampleJSON
34 describe "Responses Definition Object" $ responsesDefinitionExample <=> responsesDefinitionExampleJSON
35 describe "Security Definitions Object" $ securityDefinitionsExample <=> securityDefinitionsExampleJSON
36 describe "Swagger Object" $ do
37 context "Todo Example" $ swaggerExample <=> swaggerExampleJSON
38 context "PetStore Example" $
39 it "decodes successfully" $ do
40 fromJSON petstoreExampleJSON `shouldSatisfy` (\x -> case x of Success (_ :: Swagger) -> True; _ -> False)
45-- =======================================================================
47-- =======================================================================
51 & title .~ "Swagger Sample App"
52 & description ?~ "This is a sample server Petstore server."
53 & termsOfService ?~ "http://swagger.io/terms/"
54 & contact ?~ contactExample
55 & license ?~ licenseExample
58infoExampleJSON :: Value
59infoExampleJSON = [aesonQQ|
61 "title": "Swagger Sample App",
62 "description": "This is a sample server Petstore server.",
63 "termsOfService": "http://swagger.io/terms/",
65 "name": "API Support",
66 "url": "http://www.swagger.io/support",
67 "email": "support@swagger.io"
71 "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
77-- =======================================================================
79-- =======================================================================
81contactExample :: Contact
82contactExample = mempty
83 & name ?~ "API Support"
84 & url ?~ URL "http://www.swagger.io/support"
85 & email ?~ "support@swagger.io"
87contactExampleJSON :: Value
88contactExampleJSON = [aesonQQ|
90 "name": "API Support",
91 "url": "http://www.swagger.io/support",
92 "email": "support@swagger.io"
96-- =======================================================================
98-- =======================================================================
100licenseExample :: License
101licenseExample = License "Apache 2.0" (Just (URL "http://www.apache.org/licenses/LICENSE-2.0.html"))
103licenseExampleJSON :: Value
104licenseExampleJSON = [aesonQQ|
106 "name": "Apache 2.0",
107 "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
112-- =======================================================================
114-- =======================================================================
116operationExample :: Operation
117operationExample = mempty
119 & summary ?~ "Updates a pet in the store with form data"
121 & operationId ?~ "updatePetWithForm"
122 & consumes ?~ MimeList ["application/x-www-form-urlencoded"]
123 & produces ?~ MimeList ["application/json", "application/xml"]
124 & parameters .~ map Inline
127 & description ?~ "ID of pet that needs to be updated"
129 & schema .~ ParamOther (stringSchema ParamPath)
132 & description ?~ "Updated name of the pet"
134 & schema .~ ParamOther (stringSchema ParamFormData)
137 & description ?~ "Updated status of the pet"
139 & schema .~ ParamOther (stringSchema ParamFormData)
142 & responses .~ (mempty & responses .~
143 [ (200, Inline (mempty & description .~ "Pet updated."))
144 , (405, Inline (mempty & description .~ "Invalid input")) ])
145 & security .~ [SecurityRequirement [("petstore_auth", ["write:pets", "read:pets"])]]
147 stringSchema :: ParamLocation -> ParamOtherSchema
148 stringSchema loc = mempty
150 & type_ .~ SwaggerString
152operationExampleJSON :: Value
153operationExampleJSON = [aesonQQ|
158 "summary": "Updates a pet in the store with form data",
160 "operationId": "updatePetWithForm",
162 "application/x-www-form-urlencoded"
172 "description": "ID of pet that needs to be updated",
179 "description": "Updated name of the pet",
186 "description": "Updated status of the pet",
193 "description": "Pet updated."
196 "description": "Invalid input"
210-- =======================================================================
212-- =======================================================================
214schemaPrimitiveExample :: Schema
215schemaPrimitiveExample = mempty
216 & type_ .~ SwaggerString
219schemaPrimitiveExampleJSON :: Value
220schemaPrimitiveExampleJSON = [aesonQQ|
227schemaSimpleModelExample :: Schema
228schemaSimpleModelExample = mempty
229 & type_ .~ SwaggerObject
230 & required .~ [ "name" ]
232 [ ("name", Inline (mempty & type_ .~ SwaggerString))
233 , ("address", Ref (Reference "Address"))
234 , ("age", Inline $ mempty
236 & type_ .~ SwaggerInteger
237 & format ?~ "int32" ) ]
239schemaSimpleModelExampleJSON :: Value
240schemaSimpleModelExampleJSON = [aesonQQ|
251 "$ref": "#/definitions/Address"
262schemaModelDictExample :: Schema
263schemaModelDictExample = mempty
264 & type_ .~ SwaggerObject
265 & additionalProperties ?~ (mempty & type_ .~ SwaggerString)
267schemaModelDictExampleJSON :: Value
268schemaModelDictExampleJSON = [aesonQQ|
271 "additionalProperties": {
277schemaWithExampleExample :: Schema
278schemaWithExampleExample = mempty
279 & type_ .~ SwaggerObject
281 [ ("id", Inline $ mempty
282 & type_ .~ SwaggerInteger
283 & format ?~ "int64" )
284 , ("name", Inline $ mempty
285 & type_ .~ SwaggerString) ]
286 & required .~ [ "name" ]
287 & example ?~ [aesonQQ|
294schemaWithExampleExampleJSON :: Value
295schemaWithExampleExampleJSON = [aesonQQ|
317-- =======================================================================
319-- =======================================================================
321definitionsExample :: HashMap Text Schema
323 [ ("Category", mempty
324 & type_ .~ SwaggerObject
326 [ ("id", Inline $ mempty
327 & type_ .~ SwaggerInteger
329 , ("name", Inline (mempty & type_ .~ SwaggerString)) ] )
331 & type_ .~ SwaggerObject
333 [ ("id", Inline $ mempty
334 & type_ .~ SwaggerInteger
336 , ("name", Inline (mempty & type_ .~ SwaggerString)) ] ) ]
338definitionsExampleJSON :: Value
339definitionsExampleJSON = [aesonQQ|
368-- =======================================================================
369-- Parameters Definition object
370-- =======================================================================
372paramsDefinitionExample :: HashMap Text Param
373paramsDefinitionExample =
374 [ ("skipParam", mempty
376 & description ?~ "number of items to skip"
378 & schema .~ ParamOther (mempty
380 & type_ .~ SwaggerInteger
381 & format ?~ "int32" ))
382 , ("limitParam", mempty
384 & description ?~ "max records to return"
386 & schema .~ ParamOther (mempty
388 & type_ .~ SwaggerInteger
389 & format ?~ "int32" )) ]
391paramsDefinitionExampleJSON :: Value
392paramsDefinitionExampleJSON = [aesonQQ|
397 "description": "number of items to skip",
405 "description": "max records to return",
413-- =======================================================================
414-- Responses Definition object
415-- =======================================================================
417responsesDefinitionExample :: HashMap Text Response
418responsesDefinitionExample =
419 [ ("NotFound", mempty & description .~ "Entity not found.")
420 , ("IllegalInput", mempty & description .~ "Illegal input for operation.") ]
422responsesDefinitionExampleJSON :: Value
423responsesDefinitionExampleJSON = [aesonQQ|
426 "description": "Entity not found."
429 "description": "Illegal input for operation."
434-- =======================================================================
435-- Responses Definition object
436-- =======================================================================
438securityDefinitionsExample :: HashMap Text SecurityScheme
439securityDefinitionsExample =
440 [ ("api_key", SecurityScheme
441 { _securitySchemeType = SecuritySchemeApiKey (ApiKeyParams "api_key" ApiKeyHeader)
442 , _securitySchemeDescription = Nothing })
443 , ("petstore_auth", SecurityScheme
444 { _securitySchemeType = SecuritySchemeOAuth2 (OAuth2Params
445 { _oauth2Flow = OAuth2Implicit "http://swagger.io/api/oauth/dialog"
447 [ ("write:pets", "modify pets in your account")
448 , ("read:pets", "read your pets") ] } )
449 , _securitySchemeDescription = Nothing }) ]
451securityDefinitionsExampleJSON :: Value
452securityDefinitionsExampleJSON = [aesonQQ|
461 "authorizationUrl": "http://swagger.io/api/oauth/dialog",
464 "write:pets": "modify pets in your account",
465 "read:pets": "read your pets"
471-- =======================================================================
473-- =======================================================================
475swaggerExample :: Swagger
476swaggerExample = mempty
481 & title .~ "Todo API"
482 & license ?~ License "MIT" (Just (URL "http://mit.com"))
483 & description ?~ "This is a an API that tests servant-swagger support for a Todo API")
484 & paths.at "/todo/{id}" ?~ (mempty & get ?~ ((mempty :: Operation)
485 & at 200 ?~ Inline (mempty
486 & description .~ "OK"
487 & schema ?~ Inline (mempty
488 & type_ .~ SwaggerObject
489 & example ?~ [aesonQQ|
492 "description": "get milk"
494 & description ?~ "This is some real Todo right here"
496 [ ("created", Inline $ mempty
497 & type_ .~ SwaggerInteger
499 , ("description", Inline (mempty & type_ .~ SwaggerString))]))
500 & produces ?~ MimeList [ "application/json" ]
505 & description ?~ "TodoId param"
506 & schema .~ ParamOther (mempty
508 & type_ .~ SwaggerString ) ]
509 & tags .~ [ "todo" ] ))
511swaggerExampleJSON :: Value
512swaggerExampleJSON = [aesonQQ|
523 "url": "http://mit.com",
526 "description": "This is a an API that tests servant-swagger support for a Todo API"
536 "description": "get milk"
539 "description": "This is some real Todo right here",
562 "description": "TodoId param"
574petstoreExampleJSON :: Value
575petstoreExampleJSON = [aesonQQ|
579 "description":"This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters.",
581 "title":"Swagger Petstore",
582 "termsOfService":"http://swagger.io/terms/",
584 "email":"apiteam@swagger.io"
588 "url":"http://www.apache.org/licenses/LICENSE-2.0.html"
591 "host":"petstore.swagger.io",
596 "description":"Everything about your Pets",
598 "description":"Find out more",
599 "url":"http://swagger.io"
604 "description":"Access to Petstore orders"
608 "description":"Operations about user",
610 "description":"Find out more about our store",
611 "url":"http://swagger.io"
624 "summary":"Add a new pet to the store",
626 "operationId":"addPet",
639 "description":"Pet object that needs to be added to the store",
642 "$ref":"#/definitions/Pet"
648 "description":"Invalid input"
664 "summary":"Update an existing pet",
666 "operationId":"updatePet",
679 "description":"Pet object that needs to be added to the store",
682 "$ref":"#/definitions/Pet"
688 "description":"Invalid ID supplied"
691 "description":"Pet not found"
694 "description":"Validation exception"
707 "/pet/findByStatus":{
712 "summary":"Finds Pets by status",
713 "description":"Multiple status values can be provided with comma seperated strings",
714 "operationId":"findPetsByStatus",
723 "description":"Status values that need to be considered for filter",
733 "default":"available"
735 "collectionFormat":"csv"
740 "description":"successful operation",
744 "$ref":"#/definitions/Pet"
749 "description":"Invalid status value"
767 "summary":"Finds Pets by tags",
768 "description":"Muliple tags can be provided with comma seperated strings. Use tag1, tag2, tag3 for testing.",
769 "operationId":"findPetsByTags",
778 "description":"Tags to filter by",
784 "collectionFormat":"csv"
789 "description":"successful operation",
793 "$ref":"#/definitions/Pet"
798 "description":"Invalid tag value"
816 "summary":"Find pet by ID",
817 "description":"Returns a single pet",
818 "operationId":"getPetById",
827 "description":"ID of pet to return",
835 "description":"successful operation",
837 "$ref":"#/definitions/Pet"
841 "description":"Invalid ID supplied"
844 "description":"Pet not found"
857 "summary":"Updates a pet in the store with form data",
859 "operationId":"updatePetWithForm",
861 "application/x-www-form-urlencoded"
871 "description":"ID of pet that needs to be updated",
879 "description":"Updated name of the pet",
886 "description":"Updated status of the pet",
893 "description":"Invalid input"
909 "summary":"Deletes a pet",
911 "operationId":"deletePet",
926 "description":"Pet id to delete",
934 "description":"Invalid pet value"
947 "/pet/{petId}/uploadImage":{
952 "summary":"uploads an image",
954 "operationId":"uploadFile",
956 "multipart/form-data"
965 "description":"ID of pet to update",
971 "name":"additionalMetadata",
973 "description":"Additional data to pass to server",
980 "description":"file to upload",
987 "description":"successful operation",
989 "$ref":"#/definitions/ApiResponse"
1003 "/store/inventory":{
1008 "summary":"Returns pet inventories by status",
1009 "description":"Returns a map of status codes to quantities",
1010 "operationId":"getInventory",
1017 "description":"successful operation",
1020 "additionalProperties":{
1039 "summary":"Place an order for a pet",
1041 "operationId":"placeOrder",
1050 "description":"order placed for purchasing the pet",
1053 "$ref":"#/definitions/Order"
1059 "description":"successful operation",
1061 "$ref":"#/definitions/Order"
1065 "description":"Invalid Order"
1070 "/store/order/{orderId}":{
1075 "summary":"Find purchase order by ID",
1076 "description":"For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions",
1077 "operationId":"getOrderById",
1086 "description":"ID of pet that needs to be fetched",
1096 "description":"successful operation",
1098 "$ref":"#/definitions/Order"
1102 "description":"Invalid ID supplied"
1105 "description":"Order not found"
1113 "summary":"Delete purchase order by ID",
1114 "description":"For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors",
1115 "operationId":"deleteOrder",
1124 "description":"ID of the order that needs to be deleted",
1132 "description":"Invalid ID supplied"
1135 "description":"Order not found"
1145 "summary":"Create user",
1146 "description":"This can only be done by the logged in user.",
1147 "operationId":"createUser",
1156 "description":"Created user object",
1159 "$ref":"#/definitions/User"
1165 "description":"successful operation"
1170 "/user/createWithArray":{
1175 "summary":"Creates list of users with given input array",
1177 "operationId":"createUsersWithArrayInput",
1186 "description":"List of user object",
1191 "$ref":"#/definitions/User"
1198 "description":"successful operation"
1203 "/user/createWithList":{
1208 "summary":"Creates list of users with given input array",
1210 "operationId":"createUsersWithListInput",
1219 "description":"List of user object",
1224 "$ref":"#/definitions/User"
1231 "description":"successful operation"
1241 "summary":"Logs user into the system",
1243 "operationId":"loginUser",
1252 "description":"The user name for login",
1259 "description":"The password for login in clear text",
1266 "description":"successful operation",
1274 "description":"calls per hour allowed by the user"
1278 "format":"date-time",
1279 "description":"date in UTC when toekn expires"
1284 "description":"Invalid username/password supplied"
1294 "summary":"Logs out current logged in user session",
1296 "operationId":"logoutUser",
1304 "description":"successful operation"
1309 "/user/{username}":{
1314 "summary":"Get user by user name",
1316 "operationId":"getUserByName",
1325 "description":"The name that needs to be fetched. Use user1 for testing. ",
1332 "description":"successful operation",
1334 "$ref":"#/definitions/User"
1338 "description":"Invalid username supplied"
1341 "description":"User not found"
1349 "summary":"Updated user",
1350 "description":"This can only be done by the logged in user.",
1351 "operationId":"updateUser",
1360 "description":"name that need to be deleted",
1367 "description":"Updated user object",
1370 "$ref":"#/definitions/User"
1376 "description":"Invalid user supplied"
1379 "description":"User not found"
1387 "summary":"Delete user",
1388 "description":"This can only be done by the logged in user.",
1389 "operationId":"deleteUser",
1398 "description":"The name that needs to be deleted",
1405 "description":"Invalid username supplied"
1408 "description":"User not found"
1414 "securityDefinitions":{
1417 "authorizationUrl":"http://petstore.swagger.io/api/oauth/dialog",
1420 "write:pets":"modify pets in your account",
1421 "read:pets":"read your pets"
1448 "format":"date-time"
1452 "description":"Order Status",
1511 "description":"User Status"
1545 "$ref":"#/definitions/Category"
1568 "$ref":"#/definitions/Tag"
1573 "description":"pet status in the store",
1602 "description":"Find out more about Swagger",
1603 "url":"http://swagger.io"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list