reitit/doc/swagger.md

59 lines
2 KiB
Markdown
Raw Normal View History

2018-04-23 05:27:16 +00:00
# Swagger
Reitit supports [Swagger](https://swagger.io/) to generate route documentation. Documentation is extracted from existing coercion definitions `:parameters`, `:responses` and from a set of new doumentation keys.
### Example
Current `reitit-swagger` draft (with `reitit-ring` & data-specs):
```clj
(require '[reitit.ring :as ring])
(require '[reitit.ring.swagger :as swagger])
(require '[reitit.ring.coercion :as rrc])
(require '[reitit.coercion.spec :as spec])
(def app
(ring/ring-handler
(ring/router
["/api"
;; identify a swagger api
;; there can be several in a routing tree
{:swagger {:id :math}}
;; the (undocumented) swagger spec endpoint
["/swagger.json"
{:get {:no-doc true
:swagger {:info {:title "my-api"}}
:handler (swagger/create-swagger-handler)}}]
;; the (undocumented) swagger-ui
;; [org.webjars/swagger-ui "3.13.4"]
["/docs/*"
{:get {:no-doc true
:handler (ring/create-resource-handler
{:root "META-INF/resources/webjars/swagger-ui"})}}]
["/minus"
{:get {:summary "minus"
:parameters {:query {:x int?, :y int?}}
:responses {200 {:body {:total int?}}}
:handler (fn [{{{:keys [x y]} :query} :parameters}]
{:status 200, :body {:total (- x y)}})}}]
["/plus"
{:get {:summary "plus"
:parameters {:query {:x int?, :y int?}}
:responses {200 {:body {:total int?}}}
:handler (fn [{{{:keys [x y]} :query} :parameters}]
{:status 200, :body {:total (+ x y)}})}}]]
2018-04-25 10:44:44 +00:00
{:data {:middleware [rrc/coerce-exceptions-middleware
2018-04-23 05:27:16 +00:00
rrc/coerce-request-middleware
2018-04-25 10:44:44 +00:00
rrc/coerce-response-middleware
;; provides just route data specs
swagger/swagger-feature]
2018-04-23 05:27:16 +00:00
:coercion spec/coercion}})))
```