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 does this help? https://github.com/metosin/compojure-api/wiki/2.0.0-Content-Negotiation-(with-Muuntaja)#using-muuntaja-outside-of-the-api
omg, I've read all the documentation except this part 😄
I'll try right now, thanks
(the docs are a mess as there are both pre- and post- 2.0.0 stuff. need to fix before release)
I didn't actually help, I can access the static content under "/" and api under "/api" however /docs returns 404
(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))
how does your api-config look? (the swagger-parts)
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}}})
does the /swagger.json
return the swagger json?
java.lang.IllegalArgumentException: don't know how to convert class java.io.InputStream into a Swagger Schema. Check out ring-swagger docs for details.
That's what I see in the log
So I guess thats the Muutanja wrap-format missing?
nope, :middleware [muuntaja.middleware/wrap-format]
makes the errors go away however then /swagger.json returns 404
I did a sample from compojure-api template, with latest alpha and this handler:
(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"}))))
it works ok. api
binds the muuntaja-stuff with defaults.
And is there any difference between (defapi app
and (def app (api
?
no. same with (def r (routes ...))
and (defroutes r ...)
I think the defapi
and defroutes
could be removed, as they just save few marks, no special handling in them.
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
.
yes, I have a file upload there
There is no mapping for it by default, you can add one with the ring-swagger multimethods.
Routes with
:multipart-params [file :- upload/TempFileUpload]
:middleware [upload/wrap-multipart-params]
reffered from [ring.swagger.upload :as upload]
but do you have somewhere :return InputStream
?
Yes, file download
(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))
So that's the problem?
yes. there is no code to map InputStream into a swagger definition, so the swagger-doc generation fails
(ring.swagger.json-schema/defmethod convert-class java.io.InputStream [_ _] {:type "file"})
should fix that.
or just remove the :return
Ok, thanks a lot, I guess I would spent a whole day digging in the code 🙂
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.
Exception could also say which key caused this, and maybe which route
true that.
PR welcome
I'll take a look at it
I have a strange issue with compojure-api; my :body-params
is sometimes nil even tho :body
does contain a valid payload
Even stranger, it only happens with Undertow; with http-kit it works fine