ring-swagger

ring-swagger & compojure-api
petr.mensik 2017-11-10T08:18:04.000170Z

Ok, I've realized that ring.middleware.format is not a way since Compojure-api 2.0. is usig Muutanja for content negotiation. So I've ended up the code above without middleware section but it breaks Swagged docs generation

petr.mensik 2017-11-10T10:57:24.000009Z

omg, I've read all the documentation except this part 😄

petr.mensik 2017-11-10T10:57:33.000125Z

I'll try right now, thanks

ikitommi 2017-11-10T10:57:33.000203Z

(the docs are a mess as there are both pre- and post- 2.0.0 stuff. need to fix before release)

petr.mensik 2017-11-10T11:36:16.000278Z

I didn't actually help, I can access the static content under "/" and api under "/api" however /docs returns 404

petr.mensik 2017-11-10T11:37:11.000076Z

(defapi app
  api-config
  (context "/api" []
    api-routes-calendar ; all of them normal (defroutes (context "/calendar" [] (GET ..etc)))
    api-routes-tasks
    api-routes-users)
  (undocumented app-routes ; static content
                  not-found-routes))

ikitommi 2017-11-10T11:37:55.000103Z

how does your api-config look? (the swagger-parts)

petr.mensik 2017-11-10T11:37:58.000052Z

api-config goes like this

(def api-config
  {:swagger
   {:ui "/docs"
    :spec "/swagger.json"
    :data {:info {:title "Bizziapp REST API documentation"
                  :description "Documentation which describes all REST endpoints provided by Bizziapp"}
           :tags [{:name "Authentication" :description "API for auth operations"}
                  {:name "Users" :description "API for users operations"}]}}
   :exceptions {:handlers {::ex/default bizzi-exception-handler
                           ::custom-error bizzi-error-handler}}})

ikitommi 2017-11-10T11:39:18.000359Z

does the /swagger.json return the swagger json?

petr.mensik 2017-11-10T11:40:50.000228Z

java.lang.IllegalArgumentException: don't know how to convert class java.io.InputStream into a Swagger Schema. Check out ring-swagger docs for details.

petr.mensik 2017-11-10T11:40:56.000173Z

That's what I see in the log

petr.mensik 2017-11-10T11:41:29.000038Z

So I guess thats the Muutanja wrap-format missing?

petr.mensik 2017-11-10T11:43:39.000160Z

nope, :middleware [muuntaja.middleware/wrap-format] makes the errors go away however then /swagger.json returns 404

ikitommi 2017-11-10T11:46:44.000189Z

I did a sample from compojure-api template, with latest alpha and this handler:

ikitommi 2017-11-10T11:46:47.000174Z

(ns tst.handler
  (:require [compojure.api.sweet :refer :all]
            [ring.util.http-response :refer :all]
            [schema.core :as s]))

(s/defschema Pizza
  {:name s/Str
   (s/optional-key :description) s/Str
   :size (s/enum :L :M :S)
   :origin {:country (s/enum :FI :PO)
            :city s/Str}})

(def app
  (api
    {:swagger
       {:ui "/docs"
        :spec "/swagger.json"
        :data {:info {:title "Bizziapp REST API documentation"
                      :description "Documentation which describes all REST endpoints provided by Bizziapp"}
               :tags [{:name "Authentication" :description "API for auth operations"}
                      {:name "Users" :description "API for users operations"}]}}}
                      
    (context "/api" []
      :tags ["api"]

      (GET "/plus" []
        :return {:result Long}
        :query-params [x :- Long, y :- Long]
        :summary "adds two numbers together"
        (ok {:result (+ x y)}))

      (POST "/echo" []
        :return Pizza
        :body [pizza Pizza]
        :summary "echoes a Pizza"
        (ok pizza)))

    (undocumented
      (constantly {:status 200, :body "not found"}))))

ikitommi 2017-11-10T11:47:28.000292Z

it works ok. api binds the muuntaja-stuff with defaults.

petr.mensik 2017-11-10T11:47:53.000069Z

And is there any difference between (defapi app and (def app (api?

ikitommi 2017-11-10T11:48:48.000119Z

no. same with (def r (routes ...)) and (defroutes r ...)

ikitommi 2017-11-10T11:49:22.000090Z

I think the defapi and defroutes could be removed, as they just save few marks, no special handling in them.

ikitommi 2017-11-10T11:50:34.000087Z

but, the don't know how to convert class java.io.InputStream seems to be the problem, some of your routes is marked to return InputStream .

petr.mensik 2017-11-10T11:50:54.000155Z

yes, I have a file upload there

ikitommi 2017-11-10T11:51:02.000144Z

There is no mapping for it by default, you can add one with the ring-swagger multimethods.

petr.mensik 2017-11-10T11:51:18.000373Z

Routes with

:multipart-params [file :- upload/TempFileUpload]
      :middleware [upload/wrap-multipart-params]

petr.mensik 2017-11-10T11:51:44.000080Z

reffered from [ring.swagger.upload :as upload]

ikitommi 2017-11-10T11:52:56.000308Z

but do you have somewhere :return InputStream?

petr.mensik 2017-11-10T11:53:17.000033Z

Yes, file download

petr.mensik 2017-11-10T11:53:26.000184Z

(GET ":/id/download" []
      :return java.io.InputStream
      :summary "Returns files content for download"
      :path-params [id :- Long]
      :query-params [{preview :- Boolean false}]
      (controller/download-file id preview))

petr.mensik 2017-11-10T11:53:31.000254Z

So that's the problem?

ikitommi 2017-11-10T11:54:07.000011Z

yes. there is no code to map InputStream into a swagger definition, so the swagger-doc generation fails

ikitommi 2017-11-10T11:54:55.000214Z

(ring.swagger.json-schema/defmethod convert-class java.io.InputStream            [_ _] {:type "file"})

ikitommi 2017-11-10T11:54:58.000204Z

should fix that.

ikitommi 2017-11-10T11:55:07.000149Z

or just remove the :return

petr.mensik 2017-11-10T11:55:31.000030Z

Ok, thanks a lot, I guess I would spent a whole day digging in the code 🙂

ikitommi 2017-11-10T11:56:18.000190Z

np. I think the original error java.lang.IllegalArgumentException: don't know how to convert class java.io.InputStream into a Swagger Schema. Check out ring-swagger docs for details. said a lot, but could have said more - like how to fix that.

ikitommi 2017-11-10T11:57:07.000081Z

https://github.com/metosin/ring-swagger/issues/130

juhoteperi 2017-11-10T11:57:10.000134Z

Exception could also say which key caused this, and maybe which route

ikitommi 2017-11-10T11:57:28.000058Z

true that.

ikitommi 2017-11-10T11:58:47.000139Z

PR welcome

petr.mensik 2017-11-10T12:05:56.000278Z

I'll take a look at it

oahner 2017-11-10T21:34:13.000451Z

I have a strange issue with compojure-api; my :body-params is sometimes nil even tho :body does contain a valid payload

oahner 2017-11-10T21:36:06.000404Z

Even stranger, it only happens with Undertow; with http-kit it works fine