From 1957aabcff64260ea25480b7f6054e0e52a13993 Mon Sep 17 00:00:00 2001 From: Heikki Hokkanen Date: Wed, 10 Feb 2016 11:26:50 +0200 Subject: [PATCH 1/3] Teach swagger-routes to heed basePath for swagger-docs path If {:data {:basePath "/app"}} is given, default to including it as a prefix to swagger-ui :swagger-docs path. The basePath is already passed to swagger-docs, so this makes the swagger-ui consistent with the generated json. The swagger-docs path can also now be completely overridden via: {:options {:ui {:swagger-docs "/override.json"}}} --- src/compojure/api/swagger.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compojure/api/swagger.clj b/src/compojure/api/swagger.clj index 431ea642..e6d89be4 100644 --- a/src/compojure/api/swagger.clj +++ b/src/compojure/api/swagger.clj @@ -109,7 +109,7 @@ (if options (let [{:keys [ui spec data] {ui-options :ui spec-options :spec} :options} (merge swagger-defaults options)] (c/routes - (if ui (apply swagger-ui ui (mapcat identity (merge ui-options (if spec {:swagger-docs spec}))))) + (if ui (apply swagger-ui ui (mapcat identity (merge (if spec {:swagger-docs (apply str (remove clojure.string/blank? [(:basePath data) spec]))}) ui-options)))) (if spec (apply swagger-docs spec (mapcat identity data)))))))) (defn validate From 3a9c4cf141a972f4de368fcb31260f8679520722 Mon Sep 17 00:00:00 2001 From: Heikki Hokkanen Date: Wed, 10 Feb 2016 11:34:53 +0200 Subject: [PATCH 2/3] Add basePath as example option to swagger-routes --- src/compojure/api/swagger.clj | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/compojure/api/swagger.clj b/src/compojure/api/swagger.clj index e6d89be4..c9a4780a 100644 --- a/src/compojure/api/swagger.clj +++ b/src/compojure/api/swagger.clj @@ -94,7 +94,8 @@ :spec \"/swagger.json\" :options {:ui {:jsonEditor true} :spec {}} - :data {:info {:version \"1.0.0\" + :data {:basePath \"/app\" + :info {:version \"1.0.0\" :title \"Sausages\" :description \"Sausage description\" :termsOfService \"http://helloreverb.com/terms/\" From 07738e0a29951eec8ee6bcafa6f9b42074190997 Mon Sep 17 00:00:00 2001 From: Heikki Hokkanen Date: Wed, 10 Feb 2016 14:32:39 +0200 Subject: [PATCH 3/3] Add test for changing swagger-routes basePath --- test/compojure/api/swagger_test.clj | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/compojure/api/swagger_test.clj b/test/compojure/api/swagger_test.clj index cc87b270..e47cfd63 100644 --- a/test/compojure/api/swagger_test.clj +++ b/test/compojure/api/swagger_test.clj @@ -170,3 +170,18 @@ :path-params [foo :- String] identity)) => {"/:foo.json" {:get {:parameters {:path {:foo String}}}}}) + +(facts + (tabular + (fact "swagger-routes basePath can be changed" + (let [app (api (swagger-routes ?given-options))] + (-> + (get* app "/swagger.json") + (nth 1) + :basePath) + => ?expected-base-path + (nth (raw-get* app "/conf.js") 1) => (str "window.API_CONF = {\"url\":\"" ?expected-swagger-docs-path "\"};"))) + ?given-options ?expected-swagger-docs-path ?expected-base-path + {} "/swagger.json" "/" + {:data {:basePath "/app"}} "/app/swagger.json" "/app" + {:data {:basePath "/app"} :options {:ui {:swagger-docs "/imaginary.json"}}} "/imaginary.json" "/app"))