git.iko.soy/typeable/swagger2
on master help sign in
Update SwaggerSpec to use lenses
commitd70163aa41eee86efb34f8369af383a2af5c039a authorNickolay Kudasov <nickolay.kudasov@gmail.com> dateThu 21 Jan 2016, 17:35 (11 years ago) parentd3b6fdf
1 file changed+97−109
test/Data/SwaggerSpec.hs+97−109
test/Data/SwaggerSpec.hs+97−109
@@ -13,7 +13,7 @@ import Data.Text (Text)
1313
1414import Data.Swagger
1515import SpecCommon
16−import Test.Hspec
16+import Test.Hspec hiding (example)
1717
1818spec :: Spec
1919spec = do
@@ -44,13 +44,13 @@ main = hspec spec
4444-- =======================================================================
4545
4646infoExample :: Info
47−infoExample = Info
48− { _infoTitle = "Swagger Sample App"
49− , _infoDescription = Just "This is a sample server Petstore server."
50− , _infoTermsOfService = Just "http://swagger.io/terms/"
51− , _infoContact = Just contactExample
52− , _infoLicense = Just licenseExample
53− , _infoVersion = "1.0.1" }
47+infoExample = mempty
48+ & title .~ "Swagger Sample App"
49+ & description ?~ "This is a sample server Petstore server."
50+ & termsOfService ?~ "http://swagger.io/terms/"
51+ & contact ?~ contactExample
52+ & license ?~ licenseExample
53+ & version .~ "1.0.1"
5454
5555infoExampleJSON :: Value
5656infoExampleJSON = [aesonQQ|
@@ -76,10 +76,10 @@ infoExampleJSON = [aesonQQ|
7676-- =======================================================================
7777
7878contactExample :: Contact
79−contactExample = Contact
80− { _contactName = Just "API Support"
81− , _contactUrl = Just (URL "http://www.swagger.io/support")
82− , _contactEmail = Just "support@swagger.io" }
79+contactExample = mempty
80+ & name ?~ "API Support"
81+ & url ?~ URL "http://www.swagger.io/support"
82+ & email ?~ "support@swagger.io"
8383
8484contactExampleJSON :: Value
8585contactExampleJSON = [aesonQQ|
@@ -95,9 +95,7 @@ contactExampleJSON = [aesonQQ|
9595-- =======================================================================
9696
9797licenseExample :: License
98−licenseExample = License
99− { _licenseName = "Apache 2.0"
100− , _licenseUrl = Just (URL "http://www.apache.org/licenses/LICENSE-2.0.html") }
98+licenseExample = License "Apache 2.0" (Just (URL "http://www.apache.org/licenses/LICENSE-2.0.html"))
10199
102100licenseExampleJSON :: Value
103101licenseExampleJSON = [aesonQQ|
@@ -114,42 +112,35 @@ licenseExampleJSON = [aesonQQ|
114112
115113operationExample :: Operation
116114operationExample = mempty
117− { _operationTags = ["pet"]
118− , _operationSummary = Just "Updates a pet in the store with form data"
119− , _operationDescription = Just ""
120− , _operationOperationId = Just "updatePetWithForm"
121− , _operationConsumes = Just (MimeList ["application/x-www-form-urlencoded"])
122− , _operationProduces = Just (MimeList ["application/json", "application/xml"])
123− , _operationParameters = params
124− , _operationResponses = responses
125− , _operationSecurity = security
126− }
127− where
128− security = [SecurityRequirement [("petstore_auth", ["write:pets", "read:pets"])]]
129−
130− responses = mempty
131− { _responsesResponses =
132− [ (200, Inline mempty { _responseDescription = "Pet updated." })
133− , (405, Inline mempty { _responseDescription = "Invalid input" }) ] }
134−
135− params = map Inline
136− [ Param
137− { _paramName = "petId"
138− , _paramDescription = Just "ID of pet that needs to be updated"
139− , _paramRequired = Just True
140− , _paramSchema = ParamOther (stringSchema ParamPath) }
141− , Param
142− { _paramName = "name"
143− , _paramDescription = Just "Updated name of the pet"
144− , _paramRequired = Just False
145− , _paramSchema = ParamOther (stringSchema ParamFormData) }
146− , Param
147− { _paramName = "status"
148− , _paramDescription = Just "Updated status of the pet"
149− , _paramRequired = Just False
150− , _paramSchema = ParamOther (stringSchema ParamFormData) }
115+ & tags .~ ["pet"]
116+ & summary ?~ "Updates a pet in the store with form data"
117+ & description ?~ ""
118+ & operationId ?~ "updatePetWithForm"
119+ & consumes ?~ MimeList ["application/x-www-form-urlencoded"]
120+ & produces ?~ MimeList ["application/json", "application/xml"]
121+ & parameters .~ map Inline
122+ [ mempty
123+ & name .~ "petId"
124+ & description ?~ "ID of pet that needs to be updated"
125+ & required ?~ True
126+ & schema .~ ParamOther (stringSchema ParamPath)
127+ , mempty
128+ & name .~ "name"
129+ & description ?~ "Updated name of the pet"
130+ & required ?~ False
131+ & schema .~ ParamOther (stringSchema ParamFormData)
132+ , mempty
133+ & name .~ "status"
134+ & description ?~ "Updated status of the pet"
135+ & required ?~ False
136+ & schema .~ ParamOther (stringSchema ParamFormData)
151137 ]
152138
139+ & responses .~ (mempty & responses .~
140+ [ (200, Inline (mempty & description .~ "Pet updated."))
141+ , (405, Inline (mempty & description .~ "Invalid input")) ])
142+ & security .~ [SecurityRequirement [("petstore_auth", ["write:pets", "read:pets"])]]
143+ where
153144 stringSchema :: ParamLocation -> ParamOtherSchema
154145 stringSchema loc = mempty
155146 & in_ .~ loc
@@ -281,19 +272,21 @@ schemaModelDictExampleJSON = [aesonQQ|
281272|]
282273
283274schemaWithExampleExample :: Schema
284−schemaWithExampleExample = (mempty & type_ .~ SwaggerObject)
285− { _schemaProperties =
275+schemaWithExampleExample = mempty
276+ & type_ .~ SwaggerObject
277+ & properties .~
286278 [ ("id", Inline $ mempty
287279 & type_ .~ SwaggerInteger
288280 & format ?~ "int64" )
289− , ("name", Inline (mempty & type_ .~ SwaggerString)) ]
290− , _schemaRequired = [ "name" ]
291− , _schemaExample = Just [aesonQQ|
292− {
293− "name": "Puma",
294− "id": 1
295− }
296− |] }
281+ , ("name", Inline $ mempty
282+ & type_ .~ SwaggerString) ]
283+ & required .~ [ "name" ]
284+ & example ?~ [aesonQQ|
285+ {
286+ "name": "Puma",
287+ "id": 1
288+ }
289+ |]
297290
298291schemaWithExampleExampleJSON :: Value
299292schemaWithExampleExampleJSON = [aesonQQ|
@@ -376,21 +369,21 @@ definitionsExampleJSON = [aesonQQ|
376369paramsDefinitionExample :: HashMap Text Param
377370paramsDefinitionExample =
378371 [ ("skipParam", mempty
379− { _paramName = "skip"
380− , _paramDescription = Just "number of items to skip"
381− , _paramRequired = Just True
382− , _paramSchema = ParamOther $ mempty
372+ & name .~ "skip"
373+ & description ?~ "number of items to skip"
374+ & required ?~ True
375+ & schema .~ ParamOther (mempty
383376 & in_ .~ ParamQuery
384377 & type_ .~ SwaggerInteger
385− & format ?~ "int32" })
378+ & format ?~ "int32" ))
386379 , ("limitParam", mempty
387− { _paramName = "limit"
388− , _paramDescription = Just "max records to return"
389− , _paramRequired = Just True
390− , _paramSchema = ParamOther $ mempty
380+ & name .~ "limit"
381+ & description ?~ "max records to return"
382+ & required ?~ True
383+ & schema .~ ParamOther (mempty
391384 & in_ .~ ParamQuery
392385 & type_ .~ SwaggerInteger
393− & format ?~ "int32" }) ]
386+ & format ?~ "int32" )) ]
394387
395388paramsDefinitionExampleJSON :: Value
396389paramsDefinitionExampleJSON = [aesonQQ|
@@ -420,8 +413,8 @@ paramsDefinitionExampleJSON = [aesonQQ|
420413
421414responsesDefinitionExample :: HashMap Text Response
422415responsesDefinitionExample =
423− [ ("NotFound", mempty { _responseDescription = "Entity not found." })
424− , ("IllegalInput", mempty { _responseDescription = "Illegal input for operation." }) ]
416+ [ ("NotFound", mempty & description .~ "Entity not found.")
417+ , ("IllegalInput", mempty & description .~ "Illegal input for operation.") ]
425418
426419responsesDefinitionExampleJSON :: Value
427420responsesDefinitionExampleJSON = [aesonQQ|
@@ -478,44 +471,39 @@ securityDefinitionsExampleJSON = [aesonQQ|
478471
479472swaggerExample :: Swagger
480473swaggerExample = mempty
481− { _swaggerBasePath = Just "/"
482− , _swaggerSchemes = Just [Http]
483− , _swaggerInfo = mempty
484− { _infoVersion = "1.0"
485− , _infoTitle = "Todo API"
486− , _infoLicense = Just License
487− { _licenseName = "MIT"
488− , _licenseUrl = Just (URL "http://mit.com") }
489− , _infoDescription = Just "This is a an API that tests servant-swagger support for a Todo API" }
490− , _swaggerPaths =
491− [ ("/todo/{id}", mempty
492− { _pathItemGet = Just mempty
493− { _operationResponses = mempty
494− { _responsesResponses =
495− [ (200, Inline mempty
496− { _responseSchema = Just $ Inline (mempty & type_ .~ SwaggerObject)
497− { _schemaExample = Just [aesonQQ|
498− {
499− "created": 100,
500− "description": "get milk"
501− } |]
502− , _schemaDescription = Just "This is some real Todo right here"
503− , _schemaProperties =
504− [ ("created", Inline $ mempty
505− & type_ .~ SwaggerInteger
506− & format ?~ "int32")
507− , ("description", Inline (mempty & type_ .~ SwaggerString)) ] }
508− , _responseDescription = "OK" }) ] }
509− , _operationProduces = Just (MimeList [ "application/json" ])
510− , _operationParameters =
511− [ Inline mempty
512− { _paramRequired = Just True
513− , _paramName = "id"
514− , _paramDescription = Just "TodoId param"
515− , _paramSchema = ParamOther $ mempty
516− & in_ .~ ParamPath
517− & type_ .~ SwaggerString } ]
518− , _operationTags = [ "todo" ] } }) ] }
474+ & basePath ?~ "/"
475+ & schemes ?~ [Http]
476+ & info .~ (mempty
477+ & version .~ "1.0"
478+ & title .~ "Todo API"
479+ & license ?~ License "MIT" (Just (URL "http://mit.com"))
480+ & description ?~ "This is a an API that tests servant-swagger support for a Todo API")
481+ & paths.at "/todo/{id}" ?~ (mempty & get ?~ ((mempty :: Operation)
482+ & at 200 ?~ Inline (mempty
483+ & description .~ "OK"
484+ & schema ?~ Inline (mempty
485+ & type_ .~ SwaggerObject
486+ & example ?~ [aesonQQ|
487+ {
488+ "created": 100,
489+ "description": "get milk"
490+ } |]
491+ & description ?~ "This is some real Todo right here"
492+ & properties .~
493+ [ ("created", Inline $ mempty
494+ & type_ .~ SwaggerInteger
495+ & format ?~ "int32")
496+ , ("description", Inline (mempty & type_ .~ SwaggerString))]))
497+ & produces ?~ MimeList [ "application/json" ]
498+ & parameters .~
499+ [ Inline $ mempty
500+ & required ?~ True
501+ & name .~ "id"
502+ & description ?~ "TodoId param"
503+ & schema .~ ParamOther (mempty
504+ & in_ .~ ParamPath
505+ & type_ .~ SwaggerString ) ]
506+ & tags .~ [ "todo" ] ))
519507
520508swaggerExampleJSON :: Value
521509swaggerExampleJSON = [aesonQQ|