This commit is contained in:
Tommi Reiman 2018-07-16 09:34:52 +03:00
parent 88c16bd64d
commit 346fbad775
4 changed files with 35 additions and 12 deletions

View file

@ -1,3 +1,18 @@
## UNRELEASED
## `reitit-swagger-ui`
* **BREAKING**: pass swagger-ui `:config` as-is (instead of mixed-casing keys) to swagger-ui, fixes [#109](https://github.com/metosin/reitit/issues/109):
* see [docs](https://github.com/swagger-api/swagger-ui/tree/2.x#parameters) for available parameters.
```clj
(swagger-ui/create-swagger-ui-handler
{:path "/"
:url "/api/swagger.json"
:config {:jsonEditor true
:validatorUrl nil}})
```
## 0.1.3 (2018-6-25)
## `reitit-core`

View file

@ -55,7 +55,7 @@ If you need to post-process the generated spec, just wrap the handler with a cus
| :root | optional resource root, defaults to `"swagger-ui"`
| :url | path to swagger endpoint, defaults to `/swagger.json`
| :path | optional path to mount the handler to. Works only if mounted outside of a router.
| :config | parameters passed to swaggger-ui, keys transformed into camelCase. See [the docs](https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/configuration.md)
| :config | parameters passed to swaggger-ui as-is. See [the docs](https://github.com/swagger-api/swagger-ui/tree/2.x#parameters)
We use swagger-ui from [ring-swagger-ui](https://github.com/metosin/ring-swagger-ui), which can be easily configured from routing application. It stores files `swagger-ui` in the resource classpath.

View file

@ -14,9 +14,9 @@
| :root | optional resource root, defaults to `\"swagger-ui\"`
| :url | path to swagger endpoint, defaults to `/swagger.json`
| :path | optional path to mount the handler to. Works only if mounted outside of a router.
| :config | parameters passed to swaggger-ui, keys transformed into camelCase.
| :config | parameters passed to swaggger-ui as-is.
See https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/configuration.md
See https://github.com/swagger-api/swagger-ui/tree/2.x#parameters
for all available :config options
Examples:
@ -24,24 +24,20 @@
;; with defaults
(create-swagger-ui-handler)
;; with path and url set, swagger validator disabled
;; with path and url set, swagger validator disabled, jsonEditor enabled
(swagger-ui/create-swagger-ui-handler
{:path \"/\"
:url \"/api/swagger.json\"
:config {:validator-url nil})"
:config {:validatorUrl nil
:jsonEditor true})"
([]
(create-swagger-ui-handler nil))
([options]
(let [mixed-case (fn [k]
(let [[f & rest] (str/split (name k) #"-")]
(apply str (str/lower-case f) (map str/capitalize rest))))
mixed-case-key (fn [[k v]] [(mixed-case k) v])
config-json (fn [{:keys [url config]}] (j/write-value-as-string (merge config {:url url})))
(let [config-json (fn [{:keys [url config]}] (j/write-value-as-string (merge config {:url url})))
conf-js (fn [opts] (str "window.API_CONF = " (config-json opts) ";"))
options (as-> options $
(update $ :root (fnil identity "swagger-ui"))
(update $ :url (fnil identity "/swagger.json"))
(update $ :config #(->> % (map mixed-case-key) (into {})))
(assoc $ :paths {"/conf.js" {:headers {"Content-Type" "application/javascript"}
:status 200
:body (conf-js $)}

View file

@ -2,10 +2,12 @@
(:require [clojure.test :refer :all]
[reitit.ring :as ring]
[reitit.swagger :as swagger]
[reitit.swagger-ui :as swagger-ui]
[reitit.ring.coercion :as rrc]
[reitit.coercion.spec :as spec]
[reitit.coercion.schema :as schema]
[schema.core :refer [Int]]))
[schema.core :refer [Int]]
[muuntaja.core :as m]))
(def app
(ring/ring-handler
@ -156,3 +158,13 @@
(spec-paths "/two/swagger.json")))
(is (= ["/common/ping" "/one/ping" "/two/ping" "/two/deep/ping"]
(spec-paths "/one-two/swagger.json")))))
(deftest swagger-ui-congif-test
(let [app (swagger-ui/create-swagger-ui-handler
{:path "/"
:config {:jsonEditor true}})]
(is (= 302 (:status (app {:request-method :get, :uri "/"}))))
(is (= 200 (:status (app {:request-method :get, :uri "/index.html"}))))
(is (= {:jsonEditor true, :url "/swagger.json"}
(->> {:request-method :get, :uri "/config.json"}
(app) :body (m/decode m/instance "application/json"))))))