git.iko.soy/typeable/swagger2
on master help sign in
Update SwaggerSpec to use lenses
d70163aNickolay Kudasov
test/Data/SwaggerSpec.hs+97−109
@@ -13,7 +13,7 @@ import Data.Text (Text)
13
13
14import Data.Swagger
14import Data.Swagger
15import SpecCommon
15import SpecCommon
16−import Test.Hspec
16+import Test.Hspec hiding (example)
17
17
18spec :: Spec
18spec :: Spec
19spec = do
19spec = do
@@ -44,13 +44,13 @@ main = hspec spec
44-- =======================================================================
44-- =======================================================================
45
45
46infoExample :: Info
46infoExample :: Info
47−infoExample = Info
47+infoExample = mempty
48− { _infoTitle = "Swagger Sample App"
48+ & title .~ "Swagger Sample App"
49− , _infoDescription = Just "This is a sample server Petstore server."
49+ & description ?~ "This is a sample server Petstore server."
50− , _infoTermsOfService = Just "http://swagger.io/terms/"
50+ & termsOfService ?~ "http://swagger.io/terms/"
51− , _infoContact = Just contactExample
51+ & contact ?~ contactExample
52− , _infoLicense = Just licenseExample
52+ & license ?~ licenseExample
53− , _infoVersion = "1.0.1" }
53+ & version .~ "1.0.1"
54
54
55infoExampleJSON :: Value
55infoExampleJSON :: Value
56infoExampleJSON = [aesonQQ|
56infoExampleJSON = [aesonQQ|
@@ -76,10 +76,10 @@ infoExampleJSON = [aesonQQ|
76-- =======================================================================
76-- =======================================================================
77
77
78contactExample :: Contact
78contactExample :: Contact
79−contactExample = Contact
79+contactExample = mempty
80− { _contactName = Just "API Support"
80+ & name ?~ "API Support"
81− , _contactUrl = Just (URL "http://www.swagger.io/support")
81+ & url ?~ URL "http://www.swagger.io/support"
82− , _contactEmail = Just "support@swagger.io" }
82+ & email ?~ "support@swagger.io"
83
83
84contactExampleJSON :: Value
84contactExampleJSON :: Value
85contactExampleJSON = [aesonQQ|
85contactExampleJSON = [aesonQQ|
@@ -95,9 +95,7 @@ contactExampleJSON = [aesonQQ|
95-- =======================================================================
95-- =======================================================================
96
96
97licenseExample :: License
97licenseExample :: 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"))
101
99
102licenseExampleJSON :: Value
100licenseExampleJSON :: Value
103licenseExampleJSON = [aesonQQ|
101licenseExampleJSON = [aesonQQ|
@@ -114,42 +112,35 @@ licenseExampleJSON = [aesonQQ|
114
112
115operationExample :: Operation
113operationExample :: Operation
116operationExample = mempty
114operationExample = mempty
117− { _operationTags = ["pet"]
115+ & tags .~ ["pet"]
118− , _operationSummary = Just "Updates a pet in the store with form data"
116+ & summary ?~ "Updates a pet in the store with form data"
119− , _operationDescription = Just ""
117+ & description ?~ ""
120− , _operationOperationId = Just "updatePetWithForm"
118+ & operationId ?~ "updatePetWithForm"
121− , _operationConsumes = Just (MimeList ["application/x-www-form-urlencoded"])
119+ & consumes ?~ MimeList ["application/x-www-form-urlencoded"]
122− , _operationProduces = Just (MimeList ["application/json", "application/xml"])
120+ & produces ?~ 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
121+ & parameters .~ map Inline
136− [ Param
122+ [ mempty
137− { _paramName = "petId"
123+ & name .~ "petId"
138− , _paramDescription = Just "ID of pet that needs to be updated"
124+ & description ?~ "ID of pet that needs to be updated"
139− , _paramRequired = Just True
125+ & required ?~ True
140− , _paramSchema = ParamOther (stringSchema ParamPath) }
126+ & schema .~ ParamOther (stringSchema ParamPath)
141− , Param
127+ , mempty
142− { _paramName = "name"
128+ & name .~ "name"
143− , _paramDescription = Just "Updated name of the pet"
129+ & description ?~ "Updated name of the pet"
144− , _paramRequired = Just False
130+ & required ?~ False
145− , _paramSchema = ParamOther (stringSchema ParamFormData) }
131+ & schema .~ ParamOther (stringSchema ParamFormData)
146− , Param
132+ , mempty
147− { _paramName = "status"
133+ & name .~ "status"
148− , _paramDescription = Just "Updated status of the pet"
134+ & description ?~ "Updated status of the pet"
149− , _paramRequired = Just False
135+ & required ?~ False
150− , _paramSchema = ParamOther (stringSchema ParamFormData) }
136+ & schema .~ ParamOther (stringSchema ParamFormData)
151 ]
137 ]
152
138
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
153 stringSchema :: ParamLocation -> ParamOtherSchema
144 stringSchema :: ParamLocation -> ParamOtherSchema
154 stringSchema loc = mempty
145 stringSchema loc = mempty
155 & in_ .~ loc
146 & in_ .~ loc
@@ -281,19 +272,21 @@ schemaModelDictExampleJSON = [aesonQQ|
281|]
272|]
282
273
283schemaWithExampleExample :: Schema
274schemaWithExampleExample :: Schema
284−schemaWithExampleExample = (mempty & type_ .~ SwaggerObject)
275+schemaWithExampleExample = mempty
285− { _schemaProperties =
276+ & type_ .~ SwaggerObject
277+ & properties .~
286 [ ("id", Inline $ mempty
278 [ ("id", Inline $ mempty
287 & type_ .~ SwaggerInteger
279 & type_ .~ SwaggerInteger
288 & format ?~ "int64" )
280 & format ?~ "int64" )
281+ , ("name", Inline $ mempty
289− , ("name", Inline (mempty & type_ .~ SwaggerString)) ]
282+ & type_ .~ SwaggerString) ]
290− , _schemaRequired = [ "name" ]
283+ & required .~ [ "name" ]
291− , _schemaExample = Just [aesonQQ|
284+ & example ?~ [aesonQQ|
292− {
285+ {
293− "name": "Puma",
286+ "name": "Puma",
294− "id": 1
287+ "id": 1
295− }
288+ }
296− |] }
289+ |]
297
290
298schemaWithExampleExampleJSON :: Value
291schemaWithExampleExampleJSON :: Value
299schemaWithExampleExampleJSON = [aesonQQ|
292schemaWithExampleExampleJSON = [aesonQQ|
@@ -376,21 +369,21 @@ definitionsExampleJSON = [aesonQQ|
376paramsDefinitionExample :: HashMap Text Param
369paramsDefinitionExample :: HashMap Text Param
377paramsDefinitionExample =
370paramsDefinitionExample =
378 [ ("skipParam", mempty
371 [ ("skipParam", mempty
379− { _paramName = "skip"
372+ & name .~ "skip"
380− , _paramDescription = Just "number of items to skip"
373+ & description ?~ "number of items to skip"
381− , _paramRequired = Just True
374+ & required ?~ True
382− , _paramSchema = ParamOther $ mempty
375+ & schema .~ ParamOther (mempty
383 & in_ .~ ParamQuery
376 & in_ .~ ParamQuery
384 & type_ .~ SwaggerInteger
377 & type_ .~ SwaggerInteger
385− & format ?~ "int32" })
378+ & format ?~ "int32" ))
386 , ("limitParam", mempty
379 , ("limitParam", mempty
387− { _paramName = "limit"
380+ & name .~ "limit"
388− , _paramDescription = Just "max records to return"
381+ & description ?~ "max records to return"
389− , _paramRequired = Just True
382+ & required ?~ True
390− , _paramSchema = ParamOther $ mempty
383+ & schema .~ ParamOther (mempty
391 & in_ .~ ParamQuery
384 & in_ .~ ParamQuery
392 & type_ .~ SwaggerInteger
385 & type_ .~ SwaggerInteger
393− & format ?~ "int32" }) ]
386+ & format ?~ "int32" )) ]
394
387
395paramsDefinitionExampleJSON :: Value
388paramsDefinitionExampleJSON :: Value
396paramsDefinitionExampleJSON = [aesonQQ|
389paramsDefinitionExampleJSON = [aesonQQ|
@@ -420,8 +413,8 @@ paramsDefinitionExampleJSON = [aesonQQ|
420
413
421responsesDefinitionExample :: HashMap Text Response
414responsesDefinitionExample :: HashMap Text Response
422responsesDefinitionExample =
415responsesDefinitionExample =
423− [ ("NotFound", mempty { _responseDescription = "Entity not found." })
416+ [ ("NotFound", mempty & description .~ "Entity not found.")
424− , ("IllegalInput", mempty { _responseDescription = "Illegal input for operation." }) ]
417+ , ("IllegalInput", mempty & description .~ "Illegal input for operation.") ]
425
418
426responsesDefinitionExampleJSON :: Value
419responsesDefinitionExampleJSON :: Value
427responsesDefinitionExampleJSON = [aesonQQ|
420responsesDefinitionExampleJSON = [aesonQQ|
@@ -478,44 +471,39 @@ securityDefinitionsExampleJSON = [aesonQQ|
478
471
479swaggerExample :: Swagger
472swaggerExample :: Swagger
480swaggerExample = mempty
473swaggerExample = mempty
481− { _swaggerBasePath = Just "/"
474+ & basePath ?~ "/"
482− , _swaggerSchemes = Just [Http]
475+ & schemes ?~ [Http]
483− , _swaggerInfo = mempty
476+ & info .~ (mempty
484− { _infoVersion = "1.0"
477+ & version .~ "1.0"
485− , _infoTitle = "Todo API"
478+ & title .~ "Todo API"
486− , _infoLicense = Just License
487− { _licenseName = "MIT"
488− , _licenseUrl = Just (URL "http://mit.com") }
479+ & license ?~ License "MIT" (Just (URL "http://mit.com"))
489− , _infoDescription = Just "This is a an API that tests servant-swagger support for a Todo API" }
480+ & description ?~ "This is a an API that tests servant-swagger support for a Todo API")
490− , _swaggerPaths =
491− [ ("/todo/{id}", mempty
481+ & paths.at "/todo/{id}" ?~ (mempty & get ?~ ((mempty :: Operation)
492− { _pathItemGet = Just mempty
493− { _operationResponses = mempty
494− { _responsesResponses =
495− [ (200, Inline mempty
482+ & at 200 ?~ Inline (mempty
483+ & description .~ "OK"
484+ & schema ?~ Inline (mempty
496− { _responseSchema = Just $ Inline (mempty & type_ .~ SwaggerObject)
485+ & type_ .~ SwaggerObject
497− { _schemaExample = Just [aesonQQ|
486+ & example ?~ [aesonQQ|
498− {
487+ {
499− "created": 100,
488+ "created": 100,
500− "description": "get milk"
489+ "description": "get milk"
501− } |]
490+ } |]
502− , _schemaDescription = Just "This is some real Todo right here"
491+ & description ?~ "This is some real Todo right here"
503− , _schemaProperties =
492+ & properties .~
504− [ ("created", Inline $ mempty
493+ [ ("created", Inline $ mempty
505− & type_ .~ SwaggerInteger
494+ & type_ .~ SwaggerInteger
506− & format ?~ "int32")
495+ & format ?~ "int32")
507− , ("description", Inline (mempty & type_ .~ SwaggerString)) ] }
496+ , ("description", Inline (mempty & type_ .~ SwaggerString))]))
508− , _responseDescription = "OK" }) ] }
509− , _operationProduces = Just (MimeList [ "application/json" ])
497+ & produces ?~ MimeList [ "application/json" ]
510− , _operationParameters =
498+ & parameters .~
511− [ Inline mempty
499+ [ Inline $ mempty
512− { _paramRequired = Just True
500+ & required ?~ True
513− , _paramName = "id"
501+ & name .~ "id"
514− , _paramDescription = Just "TodoId param"
502+ & description ?~ "TodoId param"
515− , _paramSchema = ParamOther $ mempty
503+ & schema .~ ParamOther (mempty
516− & in_ .~ ParamPath
504+ & in_ .~ ParamPath
517− & type_ .~ SwaggerString } ]
505+ & type_ .~ SwaggerString ) ]
518− , _operationTags = [ "todo" ] } }) ] }
506+ & tags .~ [ "todo" ] ))
519
507
520swaggerExampleJSON :: Value
508swaggerExampleJSON :: Value
521swaggerExampleJSON = [aesonQQ|
509swaggerExampleJSON = [aesonQQ|