1{-# LANGUAGE OverloadedLists #-}
2{-# LANGUAGE OverloadedStrings #-}
3{-# LANGUAGE ScopedTypeVariables #-}
4{-# LANGUAGE QuasiQuotes #-}
5module Data.SwaggerSpec where
14import Data.HashMap.Strict (HashMap)
15import qualified Data.Set as Set
16import Data.Text (Text)
20import Test.Hspec hiding (example)
24 describe "License Object" $ licenseExample <=> licenseExampleJSON
25 describe "Contact Object" $ contactExample <=> contactExampleJSON
26 describe "Info Object" $ infoExample <=> infoExampleJSON
27 describe "Operation Object" $ operationExample <=> operationExampleJSON
28 describe "Schema Object" $ do
29 context "Primitive Sample" $ schemaPrimitiveExample <=> schemaPrimitiveExampleJSON
30 context "Simple Model" $ schemaSimpleModelExample <=> schemaSimpleModelExampleJSON
31 context "Model with Map/Dictionary Properties" $ schemaModelDictExample <=> schemaModelDictExampleJSON
32 context "Model with Example" $ schemaWithExampleExample <=> schemaWithExampleExampleJSON
33 describe "Definitions Object" $ definitionsExample <=> definitionsExampleJSON
34 describe "Parameters Definition Object" $ paramsDefinitionExample <=> paramsDefinitionExampleJSON
35 describe "Responses Definition Object" $ responsesDefinitionExample <=> responsesDefinitionExampleJSON
36 describe "Security Definitions Object" $ securityDefinitionsExample <=> securityDefinitionsExampleJSON
37 describe "Swagger Object" $ do
38 context "Todo Example" $ swaggerExample <=> swaggerExampleJSON
39 context "PetStore Example" $
40 it "decodes successfully" $ do
41 fromJSON petstoreExampleJSON `shouldSatisfy` (\x -> case x of Success (_ :: Swagger) -> True; _ -> False)
46-- =======================================================================
48-- =======================================================================
52 & title .~ "Swagger Sample App"
53 & description ?~ "This is a sample server Petstore server."
54 & termsOfService ?~ "http://swagger.io/terms/"
55 & contact ?~ contactExample
56 & license ?~ licenseExample
59infoExampleJSON :: Value
60infoExampleJSON = [aesonQQ|
62 "title": "Swagger Sample App",
63 "description": "This is a sample server Petstore server.",
64 "termsOfService": "http://swagger.io/terms/",
66 "name": "API Support",
67 "url": "http://www.swagger.io/support",
68 "email": "support@swagger.io"
72 "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
78-- =======================================================================
80-- =======================================================================
82contactExample :: Contact
83contactExample = mempty
84 & name ?~ "API Support"
85 & url ?~ URL "http://www.swagger.io/support"
86 & email ?~ "support@swagger.io"
88contactExampleJSON :: Value
89contactExampleJSON = [aesonQQ|
91 "name": "API Support",
92 "url": "http://www.swagger.io/support",
93 "email": "support@swagger.io"
97-- =======================================================================
99-- =======================================================================
101licenseExample :: License
102licenseExample = "Apache 2.0"
103 & url ?~ URL "http://www.apache.org/licenses/LICENSE-2.0.html"
105licenseExampleJSON :: Value
106licenseExampleJSON = [aesonQQ|
108 "name": "Apache 2.0",
109 "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
114-- =======================================================================
116-- =======================================================================
118operationExample :: Operation
119operationExample = mempty
120 & tags .~ Set.fromList ["pet"]
121 & summary ?~ "Updates a pet in the store with form data"
123 & operationId ?~ "updatePetWithForm"
124 & consumes ?~ MimeList ["application/x-www-form-urlencoded"]
125 & produces ?~ MimeList ["application/json", "application/xml"]
126 & parameters .~ map Inline
129 & description ?~ "ID of pet that needs to be updated"
131 & schema .~ ParamOther (stringSchema ParamPath)
134 & description ?~ "Updated name of the pet"
136 & schema .~ ParamOther (stringSchema ParamFormData)
139 & description ?~ "Updated status of the pet"
141 & schema .~ ParamOther (stringSchema ParamFormData)
144 & at 200 ?~ "Pet updated."
145 & at 405 ?~ "Invalid input"
146 & security .~ [SecurityRequirement [("petstore_auth", ["write:pets", "read:pets"])]]
148 stringSchema :: ParamLocation -> ParamOtherSchema
149 stringSchema loc = mempty
151 & type_ .~ SwaggerString
153operationExampleJSON :: Value
154operationExampleJSON = [aesonQQ|
159 "summary": "Updates a pet in the store with form data",
161 "operationId": "updatePetWithForm",
163 "application/x-www-form-urlencoded"
173 "description": "ID of pet that needs to be updated",
180 "description": "Updated name of the pet",
187 "description": "Updated status of the pet",
194 "description": "Pet updated."
197 "description": "Invalid input"
211-- =======================================================================
213-- =======================================================================
215schemaPrimitiveExample :: Schema
216schemaPrimitiveExample = mempty
217 & type_ .~ SwaggerString
220schemaPrimitiveExampleJSON :: Value
221schemaPrimitiveExampleJSON = [aesonQQ|
228schemaSimpleModelExample :: Schema
229schemaSimpleModelExample = mempty
230 & type_ .~ SwaggerObject
231 & required .~ [ "name" ]
233 [ ("name", Inline (mempty & type_ .~ SwaggerString))
234 , ("address", Ref (Reference "Address"))
235 , ("age", Inline $ mempty
237 & type_ .~ SwaggerInteger
238 & format ?~ "int32" ) ]
240schemaSimpleModelExampleJSON :: Value
241schemaSimpleModelExampleJSON = [aesonQQ|
252 "$ref": "#/definitions/Address"
263schemaModelDictExample :: Schema
264schemaModelDictExample = mempty
265 & type_ .~ SwaggerObject
266 & additionalProperties ?~ (mempty & type_ .~ SwaggerString)
268schemaModelDictExampleJSON :: Value
269schemaModelDictExampleJSON = [aesonQQ|
272 "additionalProperties": {
278schemaWithExampleExample :: Schema
279schemaWithExampleExample = mempty
280 & type_ .~ SwaggerObject
282 [ ("id", Inline $ mempty
283 & type_ .~ SwaggerInteger
284 & format ?~ "int64" )
285 , ("name", Inline $ mempty
286 & type_ .~ SwaggerString) ]
287 & required .~ [ "name" ]
288 & example ?~ [aesonQQ|
295schemaWithExampleExampleJSON :: Value
296schemaWithExampleExampleJSON = [aesonQQ|
318-- =======================================================================
320-- =======================================================================
322definitionsExample :: HashMap Text Schema
324 [ ("Category", mempty
325 & type_ .~ SwaggerObject
327 [ ("id", Inline $ mempty
328 & type_ .~ SwaggerInteger
330 , ("name", Inline (mempty & type_ .~ SwaggerString)) ] )
332 & type_ .~ SwaggerObject
334 [ ("id", Inline $ mempty
335 & type_ .~ SwaggerInteger
337 , ("name", Inline (mempty & type_ .~ SwaggerString)) ] ) ]
339definitionsExampleJSON :: Value
340definitionsExampleJSON = [aesonQQ|
369-- =======================================================================
370-- Parameters Definition object
371-- =======================================================================
373paramsDefinitionExample :: HashMap Text Param
374paramsDefinitionExample =
375 [ ("skipParam", mempty
377 & description ?~ "number of items to skip"
379 & schema .~ ParamOther (mempty
381 & type_ .~ SwaggerInteger
382 & format ?~ "int32" ))
383 , ("limitParam", mempty
385 & description ?~ "max records to return"
387 & schema .~ ParamOther (mempty
389 & type_ .~ SwaggerInteger
390 & format ?~ "int32" )) ]
392paramsDefinitionExampleJSON :: Value
393paramsDefinitionExampleJSON = [aesonQQ|
398 "description": "number of items to skip",
406 "description": "max records to return",
414-- =======================================================================
415-- Responses Definition object
416-- =======================================================================
418responsesDefinitionExample :: HashMap Text Response
419responsesDefinitionExample =
420 [ ("NotFound", mempty & description .~ "Entity not found.")
421 , ("IllegalInput", mempty & description .~ "Illegal input for operation.") ]
423responsesDefinitionExampleJSON :: Value
424responsesDefinitionExampleJSON = [aesonQQ|
427 "description": "Entity not found."
430 "description": "Illegal input for operation."
435-- =======================================================================
436-- Responses Definition object
437-- =======================================================================
439securityDefinitionsExample :: HashMap Text SecurityScheme
440securityDefinitionsExample =
441 [ ("api_key", SecurityScheme
442 { _securitySchemeType = SecuritySchemeApiKey (ApiKeyParams "api_key" ApiKeyHeader)
443 , _securitySchemeDescription = Nothing })
444 , ("petstore_auth", SecurityScheme
445 { _securitySchemeType = SecuritySchemeOAuth2 (OAuth2Params
446 { _oauth2Flow = OAuth2Implicit "http://swagger.io/api/oauth/dialog"
448 [ ("write:pets", "modify pets in your account")
449 , ("read:pets", "read your pets") ] } )
450 , _securitySchemeDescription = Nothing }) ]
452securityDefinitionsExampleJSON :: Value
453securityDefinitionsExampleJSON = [aesonQQ|
462 "authorizationUrl": "http://swagger.io/api/oauth/dialog",
465 "write:pets": "modify pets in your account",
466 "read:pets": "read your pets"
472-- =======================================================================
474-- =======================================================================
476swaggerExample :: Swagger
477swaggerExample = mempty
482 & title .~ "Todo API"
484 & license._Just.url ?~ URL "http://mit.com"
485 & description ?~ "This is a an API that tests servant-swagger support for a Todo API")
486 & paths.at "/todo/{id}" ?~ (mempty & get ?~ ((mempty :: Operation)
487 & at 200 ?~ Inline (mempty
488 & description .~ "OK"
489 & schema ?~ Inline (mempty
490 & type_ .~ SwaggerObject
491 & example ?~ [aesonQQ|
494 "description": "get milk"
496 & description ?~ "This is some real Todo right here"
498 [ ("created", Inline $ mempty
499 & type_ .~ SwaggerInteger
501 , ("description", Inline (mempty & type_ .~ SwaggerString))]))
502 & produces ?~ MimeList [ "application/json" ]
507 & description ?~ "TodoId param"
508 & schema .~ ParamOther (mempty
510 & type_ .~ SwaggerString ) ]
511 & tags .~ Set.fromList [ "todo" ] ))
513swaggerExampleJSON :: Value
514swaggerExampleJSON = [aesonQQ|
525 "url": "http://mit.com",
528 "description": "This is a an API that tests servant-swagger support for a Todo API"
538 "description": "get milk"
541 "description": "This is some real Todo right here",
564 "description": "TodoId param"
576petstoreExampleJSON :: Value
577petstoreExampleJSON = [aesonQQ|
581 "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.",
583 "title":"Swagger Petstore",
584 "termsOfService":"http://swagger.io/terms/",
586 "email":"apiteam@swagger.io"
590 "url":"http://www.apache.org/licenses/LICENSE-2.0.html"
593 "host":"petstore.swagger.io",
598 "description":"Everything about your Pets",
600 "description":"Find out more",
601 "url":"http://swagger.io"
606 "description":"Access to Petstore orders"
610 "description":"Operations about user",
612 "description":"Find out more about our store",
613 "url":"http://swagger.io"
626 "summary":"Add a new pet to the store",
628 "operationId":"addPet",
641 "description":"Pet object that needs to be added to the store",
644 "$ref":"#/definitions/Pet"
650 "description":"Invalid input"
666 "summary":"Update an existing pet",
668 "operationId":"updatePet",
681 "description":"Pet object that needs to be added to the store",
684 "$ref":"#/definitions/Pet"
690 "description":"Invalid ID supplied"
693 "description":"Pet not found"
696 "description":"Validation exception"
709 "/pet/findByStatus":{
714 "summary":"Finds Pets by status",
715 "description":"Multiple status values can be provided with comma seperated strings",
716 "operationId":"findPetsByStatus",
725 "description":"Status values that need to be considered for filter",
735 "default":"available"
737 "collectionFormat":"csv"
742 "description":"successful operation",
746 "$ref":"#/definitions/Pet"
751 "description":"Invalid status value"
769 "summary":"Finds Pets by tags",
770 "description":"Muliple tags can be provided with comma seperated strings. Use tag1, tag2, tag3 for testing.",
771 "operationId":"findPetsByTags",
780 "description":"Tags to filter by",
786 "collectionFormat":"csv"
791 "description":"successful operation",
795 "$ref":"#/definitions/Pet"
800 "description":"Invalid tag value"
818 "summary":"Find pet by ID",
819 "description":"Returns a single pet",
820 "operationId":"getPetById",
829 "description":"ID of pet to return",
837 "description":"successful operation",
839 "$ref":"#/definitions/Pet"
843 "description":"Invalid ID supplied"
846 "description":"Pet not found"
859 "summary":"Updates a pet in the store with form data",
861 "operationId":"updatePetWithForm",
863 "application/x-www-form-urlencoded"
873 "description":"ID of pet that needs to be updated",
881 "description":"Updated name of the pet",
888 "description":"Updated status of the pet",
895 "description":"Invalid input"
911 "summary":"Deletes a pet",
913 "operationId":"deletePet",
928 "description":"Pet id to delete",
936 "description":"Invalid pet value"
949 "/pet/{petId}/uploadImage":{
954 "summary":"uploads an image",
956 "operationId":"uploadFile",
958 "multipart/form-data"
967 "description":"ID of pet to update",
973 "name":"additionalMetadata",
975 "description":"Additional data to pass to server",
982 "description":"file to upload",
989 "description":"successful operation",
991 "$ref":"#/definitions/ApiResponse"
1005 "/store/inventory":{
1010 "summary":"Returns pet inventories by status",
1011 "description":"Returns a map of status codes to quantities",
1012 "operationId":"getInventory",
1019 "description":"successful operation",
1022 "additionalProperties":{
1041 "summary":"Place an order for a pet",
1043 "operationId":"placeOrder",
1052 "description":"order placed for purchasing the pet",
1055 "$ref":"#/definitions/Order"
1061 "description":"successful operation",
1063 "$ref":"#/definitions/Order"
1067 "description":"Invalid Order"
1072 "/store/order/{orderId}":{
1077 "summary":"Find purchase order by ID",
1078 "description":"For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions",
1079 "operationId":"getOrderById",
1088 "description":"ID of pet that needs to be fetched",
1098 "description":"successful operation",
1100 "$ref":"#/definitions/Order"
1104 "description":"Invalid ID supplied"
1107 "description":"Order not found"
1115 "summary":"Delete purchase order by ID",
1116 "description":"For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors",
1117 "operationId":"deleteOrder",
1126 "description":"ID of the order that needs to be deleted",
1134 "description":"Invalid ID supplied"
1137 "description":"Order not found"
1147 "summary":"Create user",
1148 "description":"This can only be done by the logged in user.",
1149 "operationId":"createUser",
1158 "description":"Created user object",
1161 "$ref":"#/definitions/User"
1167 "description":"successful operation"
1172 "/user/createWithArray":{
1177 "summary":"Creates list of users with given input array",
1179 "operationId":"createUsersWithArrayInput",
1188 "description":"List of user object",
1193 "$ref":"#/definitions/User"
1200 "description":"successful operation"
1205 "/user/createWithList":{
1210 "summary":"Creates list of users with given input array",
1212 "operationId":"createUsersWithListInput",
1221 "description":"List of user object",
1226 "$ref":"#/definitions/User"
1233 "description":"successful operation"
1243 "summary":"Logs user into the system",
1245 "operationId":"loginUser",
1254 "description":"The user name for login",
1261 "description":"The password for login in clear text",
1268 "description":"successful operation",
1276 "description":"calls per hour allowed by the user"
1280 "format":"date-time",
1281 "description":"date in UTC when toekn expires"
1286 "description":"Invalid username/password supplied"
1296 "summary":"Logs out current logged in user session",
1298 "operationId":"logoutUser",
1306 "description":"successful operation"
1311 "/user/{username}":{
1316 "summary":"Get user by user name",
1318 "operationId":"getUserByName",
1327 "description":"The name that needs to be fetched. Use user1 for testing. ",
1334 "description":"successful operation",
1336 "$ref":"#/definitions/User"
1340 "description":"Invalid username supplied"
1343 "description":"User not found"
1351 "summary":"Updated user",
1352 "description":"This can only be done by the logged in user.",
1353 "operationId":"updateUser",
1362 "description":"name that need to be deleted",
1369 "description":"Updated user object",
1372 "$ref":"#/definitions/User"
1378 "description":"Invalid user supplied"
1381 "description":"User not found"
1389 "summary":"Delete user",
1390 "description":"This can only be done by the logged in user.",
1391 "operationId":"deleteUser",
1400 "description":"The name that needs to be deleted",
1407 "description":"Invalid username supplied"
1410 "description":"User not found"
1416 "securityDefinitions":{
1419 "authorizationUrl":"http://petstore.swagger.io/api/oauth/dialog",
1422 "write:pets":"modify pets in your account",
1423 "read:pets":"read your pets"
1450 "format":"date-time"
1454 "description":"Order Status",
1513 "description":"User Status"
1547 "$ref":"#/definitions/Category"
1570 "$ref":"#/definitions/Tag"
1575 "description":"pet status in the store",
1604 "description":"Find out more about Swagger",
1605 "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