git.iko.soy/typeable/swagger2
on master help sign in
Change helpers to accept Operation traversal instead of Swagger
commitbaee645f3f9e65e2afc7936800c08c6cc78bd1f7 authorNickolay Kudasov <nickolay.kudasov@gmail.com> dateMon 1 Feb 2016, 23:49 (11 years ago) parent0368033
1 file changed+12−8
src/Data/Swagger/Operation.hs+12−8
src/Data/Swagger/Operation.hs+12−8
@@ -31,6 +31,10 @@ prependPath path = paths %~ mapKeys (path </>)
3131
3232 trim = dropWhile (== '/') . dropWhileEnd (== '/')
3333
34+-- | All operations of a Swagger spec.
35+allOperations :: Traversal' Swagger Operation
36+allOperations = paths.traverse.template
37+
3438-- $setup
3539-- >>> import Data.Aeson
3640-- >>> import Data.Proxy
@@ -66,9 +70,9 @@ operationsOf sub = paths.itraversed.withIndex.subops
6670
6771-- | Apply tags to a part of Swagger spec and update the global
6872-- list of tags.
69−applyTagsFor :: Swagger -> [Tag] -> Swagger -> Swagger
70−applyTagsFor sub ts swag = swag
71− & operationsOf sub . tags %~ (map _tagName ts ++)
73+applyTagsFor :: Traversal' Swagger Operation -> [Tag] -> Swagger -> Swagger
74+applyTagsFor ops ts swag = swag
75+ & ops . tags %~ (map _tagName ts ++)
7276 & tags %~ (ts ++)
7377
7478-- | Construct a response with @'Schema'@ while declaring all
@@ -81,17 +85,17 @@ declareResponse proxy = do
8185 s <- declareSchemaRef proxy
8286 return (mempty & schema ?~ s)
8387
84−-- | Set response for all operations of a subspec.
88+-- | Set response for specified operations.
8589-- This will also update global schema definitions.
8690--
8791-- >>> let api = (mempty :: Swagger) & paths .~ [("/user", mempty & get ?~ mempty)]
8892-- >>> let res = declareResponse (Proxy :: Proxy Day)
89−-- >>> encode $ api & setResponseFor api 200 res
93+-- >>> encode $ api & setResponseFor allOperations 200 res
9094-- "{\"swagger\":\"2.0\",\"info\":{\"version\":\"\",\"title\":\"\"},\"definitions\":{\"Day\":{\"format\":\"date\",\"type\":\"string\"}},\"paths\":{\"/user\":{\"get\":{\"responses\":{\"200\":{\"schema\":{\"$ref\":\"#/definitions/Day\"},\"description\":\"\"}}}}}}"
91−setResponseFor :: Swagger -> HttpStatusCode -> Declare (Definitions Schema) Response -> Swagger -> Swagger
92−setResponseFor sub code dres swag = swag
95+setResponseFor :: Traversal' Swagger Operation -> HttpStatusCode -> Declare (Definitions Schema) Response -> Swagger -> Swagger
96+setResponseFor ops code dres swag = swag
9397 & definitions %~ (<> defs)
94− & operationsOf sub . at code ?~ Inline res
98+ & ops . at code ?~ Inline res
9599 where
96100 (defs, res) = runDeclare dres mempty
97101