Fork me on GitHub
#reitit
<
2018-06-18
>
ikitommi09:06:53

@seantempesta did you set the :securityDefinitions?

seantempesta15:06:37

Yeah, I tried that. Here’s the full definition I’m using (borrowed from https://github.com/metosin/reitit/tree/master/examples/ring-swagger):

(swagger-ui/create-swagger-ui-handler
        {:path "/"
         :url "/api/swagger.json"
         :config {:securityDefinitions
                   {:api_key
                    {:type "apiKey"
                     :name "x-apikey"
                     :in "header"}}}})
and it’s still not showing up. Does anyone have an example that’s working?

seantempesta15:06:30

Yep, that did it. I feel stupid now. Thanks! @ikitommi For anyone else who is curious, this is the diff from the example:

diff --git a/examples/ring-swagger/src/example/server.clj b/examples/ring-swagger/src/example/server.clj
index 341342b..32d87d8 100644
--- a/examples/ring-swagger/src/example/server.clj
+++ b/examples/ring-swagger/src/example/server.clj
@@ -18,60 +18,65 @@
        {:swagger {:id ::math}}
 
        ["/swagger.json"
-        {:get {:no-doc true
-               :swagger {:info {:title "my-api"}}
+        {:get {:no-doc  true
+               :swagger {:info {:title "my-api"}
+                         :securityDefinitions
+                               {:api_key
+                                {:type "apiKey"
+                                 :name "x-apikey"
+                                 :in   "header"}}}
                :handler (swagger/create-swagger-handler)}}]

ikitommi09:06:26

e.f. set swagger-data:

{:securityDefinitions
 {:api_key
  {:type "apiKey"
   :name "x-apikey"
   :in "header"}}}

ikitommi09:06:20

that should trigger the Authorization-feature in the ui.

ikitommi09:06:26

maybe there should be a pre-integrated auth feature, which would emit these too…

valerauko15:06:04

speccing xml sounds like lots of typing -- the problem then will be that clojure's representation of xml (and my specs for it) will have to be converted to swagger's expected format as described at https://github.com/OAI/OpenAPI-Specification/blob/master/fixtures/v2.0/json/models/modelWithXmlAttributes.json

ikitommi15:06:13

@vale the reitit + spec + XML. If you have a XML body parsing middleware mounted, you need to describe the Swagger XML stuff manually for specs. Haven’t heard that there is a automatic converter for spec -> SwaggerXML (or XML Schema). Something like:

(require '[clojure.spec.alpha :as s])
(require '[spec-tools.core :as st])

(s/def ::id
  (st/spec
    {:spec int?
     :json-schema/xml {:attribute true
                      :namespac "ns1"
                      :prefix "urn1"}}))

(s/def ::items
  (st/spec
    {:spec (s/coll-of string?)
     :json-schema/xml {:wrapped true}}))

(s/def ::model
  (st/spec
    {:spec (s/keys :opt-un [::id ::items])
     :json-schema/xml {:name "XMLModel"}}))

(require '[spec-tools.swagger.core :as swagger])

(swagger/transform ::model)
;{:type "object"
; :xml {:name "XMLModel"}
; :properties {"id" {:type "integer"
;                    :format "int64"
;                    :xml {:attribute true
;                          :namespac "ns1"
;                          :prefix "urn1"}}
;              "items" {:type "array"
;                       :items {:type "string"}
;                       :xml {:wrapped true}}}}

ikitommi15:06:25

not sure it’s worth the trouble defining all the fields like that, you can always write the swagger spec by hand. Just paste anything valid under :swagger and it should work.

ikitommi15:06:49

… but the integration here is that any keys for Spec data that starts with json-schema is merged on top of the normal spec->swagger/json-schema conversion.

ikitommi15:06:12

(with the json-schema ns stripped off that is)

valerauko15:06:35

wow! i think i'll need to read this 3 times over to comprehend

valerauko15:06:24

this is great reference, thanks! i'll be sure to get back to you once i got it to work (hopefully tomorrow)

ikitommi15:06:24

your welcome

seantempesta15:06:37

Yeah, I tried that. Here’s the full definition I’m using (borrowed from https://github.com/metosin/reitit/tree/master/examples/ring-swagger):

(swagger-ui/create-swagger-ui-handler
        {:path "/"
         :url "/api/swagger.json"
         :config {:securityDefinitions
                   {:api_key
                    {:type "apiKey"
                     :name "x-apikey"
                     :in "header"}}}})
and it’s still not showing up. Does anyone have an example that’s working?

ikitommi15:06:10

@seantempesta you need to put it route data under :swagger, to any place that is in the path to the endpoint serving swagger spec. E.g. here: https://github.com/metosin/reitit/blob/master/examples/ring-swagger/src/example/server.clj#L22

ikitommi15:06:08

the :config in the swagger-ui-handler is for swagger-ui configuration, described here: https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/configuration.md

seantempesta15:06:30

Yep, that did it. I feel stupid now. Thanks! @ikitommi For anyone else who is curious, this is the diff from the example:

diff --git a/examples/ring-swagger/src/example/server.clj b/examples/ring-swagger/src/example/server.clj
index 341342b..32d87d8 100644
--- a/examples/ring-swagger/src/example/server.clj
+++ b/examples/ring-swagger/src/example/server.clj
@@ -18,60 +18,65 @@
        {:swagger {:id ::math}}
 
        ["/swagger.json"
-        {:get {:no-doc true
-               :swagger {:info {:title "my-api"}}
+        {:get {:no-doc  true
+               :swagger {:info {:title "my-api"}
+                         :securityDefinitions
+                               {:api_key
+                                {:type "apiKey"
+                                 :name "x-apikey"
+                                 :in   "header"}}}
                :handler (swagger/create-swagger-handler)}}]