Skip to content

Swagger integration

Tommi Reiman edited this page Feb 2, 2016 · 20 revisions

Swagger

http://swagger.io/

Getting started

In order to use swagger, a swagger spec needs to be generated. Optionally a local a swagger ui can be mounted.

Simplest way to get both the spec and the ui is to use the compojure.api.swagger/swagger-routes handler. Without any parameters, it uses defaults where swagger-ui is mounted to / and the spec is generated to /swagger.json.

(defapi app
  (swagger-routes)
  (GET "/ping" []
    (ok {:message "pong"})))

Same can be achieved by using api-options :swagger (recommended for larger apps, keeps all configuration in on place):

(defapi app
  {:swagger {:ui "/", :spec "/swagger.json"}}
  (GET "/ping" []
    (ok {:message "pong"})))

Advanced

All the possible api-options are described in the source code.

See also swagger-ui options and a swagger spec.

(api
  {:swagger
   {:ui "/api-docs"
    :spec "/swagger.json"
    :options {:ui {:validatorUrl nil}}
    :data {:info {:version "1.0.0"
                  :title "Thingies API"
                  :description "the description"
                  :termsOfService "http://www.metosin.fi"
                  :contact {:name "My API Team"
                            :email "[email protected]"
                            :url "http://www.metosin.fi"}
                  :license {:name "Eclipse Public License"
                            :url "http://www.eclipse.org/legal/epl-v10.html"}}
           :tags [{:name "math", :description "Math with parameters"}
                  {:name "pizzas", :description "Pizza API"}
                  {:name "failing", :description "Handling uncaught exceptions"}
                  {:name "dates", :description "Dates API"}
                  {:name "responses", :description "Responses demo"}
                  {:name "primitives", :description "Returning primitive values"}
                  {:name "context", :description "context routes"}
                  {:name "echo", :description "Echoes data"}
                  {:name "ordered", :description "Ordered routes"}
                  {:name "file", :description "File upload"}]}}}
  ...)
Clone this wiki locally