Fork me on GitHub
Jacob Molin10:02:33

Hi! I'm trying to host a swagger-ui on a pedestal route which depends on manually written documentation, i.e. a swagger.json file. I've looked at and I've gotten it to work by autogenerating the swagger.json file as described in route-swagger. However, I haven't figured out how I can make it depend on a manually written swagger.json file that I've hosted myself on a different endpoint (I've solved the hosting of a manually written swagger.json). I've also looked at using the directly to wrap the swagger-ui middleware in a Pedestal interceptor, but haven't succeded. This would be my preferred method, if possible. Anyone got any suggestions? And please let me know if something's unclear or there's an even better way! Here's an example of what I would like to achieve in building the Pedestal routes table:

(ns nice.nm
  (:require [cheshire.core :as json]
            [ring.swagger.swagger-ui :refer :all]

(def swagger-json
  {:name ::swagger-json-interceptor
   :enter (fn [context]
            (assoc context :response {:status  200
                                      :headers {"Content-Type" "application/json; charset=utf-8"}
                                      :body (cheshire.core/parse-string (slurp "<path-to-dir>/swagger.json"))}))})

(def swagger-ui
  {:name ::swagger-ui-interceptor
   :enter (fn [context]
            (ring.swagger.swagger-ui/swagger-ui {:path "/test"
                                                 :swagger-docs "/applications/api/v1/swagger-json-static"}))})

(defn routes
     :post [interceptor1
     :route-name :endpoint1]
     :get [endpoint2]
     :route-name :endpoint2] 
       :get [swagger-json]
       :route-name :swagger-json]
       :get [swagger-ui]
       :route-name :swagger-ui]})


@U04GATKRD35, even I am trying to do it now, one thing is swagger-json is not creating an interceptor, it supposed to bound between (interceptor/interceptor right ?


may be I am wrong! I am trying it first time

Jacob Molin12:03:39

You're right!

Jacob Molin12:03:45

In the end I actually just used the Swagger-UI as a static resource by placing the content of the Swagger-UI /dist folder in the "resources/public" directory in my project, as explained in the (last line in General section: "If you are looking for plain ol' HTML/JS/CSS, and copy the contents of the /dist folder to your server."). You can read about static routes in Pedestal


Thanks for the instructions @U04GATKRD35, I went down this route as well. Adding one more change I needed to make to get everything working: I added

{:content-security-policy-settings "object-src 'none'; script-src 'self' 'unsafe-inline' 'unsafe-eval' https: http:;"}
to the service map, because the browser refused to load
<script src="./swagger-ui-bundle.js" charset="UTF-8"> </script>
    <script src="./swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
    <script src="./swagger-initializer.js" charset="UTF-8"> </script>
in /dist/index.html due to the default Content Security Policy. What I’ve changed in content-security-policy-settings is adding 'self' in script-src. Everything else is the default.