From 346fbad775487c10b40b7bb39a7e4bea84ce9e05 Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Mon, 16 Jul 2018 09:34:52 +0300 Subject: [PATCH] Fix #109 --- CHANGELOG.md | 15 +++++++++++++++ doc/ring/swagger.md | 2 +- .../reitit-swagger-ui/src/reitit/swagger_ui.cljc | 16 ++++++---------- test/cljc/reitit/swagger_test.clj | 14 +++++++++++++- 4 files changed, 35 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 44acbf60..4064cb67 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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` diff --git a/doc/ring/swagger.md b/doc/ring/swagger.md index 243f4dc3..5dfaa9b8 100644 --- a/doc/ring/swagger.md +++ b/doc/ring/swagger.md @@ -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. diff --git a/modules/reitit-swagger-ui/src/reitit/swagger_ui.cljc b/modules/reitit-swagger-ui/src/reitit/swagger_ui.cljc index 137dbcb5..a16c6dfc 100644 --- a/modules/reitit-swagger-ui/src/reitit/swagger_ui.cljc +++ b/modules/reitit-swagger-ui/src/reitit/swagger_ui.cljc @@ -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 $)} diff --git a/test/cljc/reitit/swagger_test.clj b/test/cljc/reitit/swagger_test.clj index 97ed485d..f3411042 100644 --- a/test/cljc/reitit/swagger_test.clj +++ b/test/cljc/reitit/swagger_test.clj @@ -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"))))))