Fork me on GitHub
#ring-swagger
<
2017-06-13
>
ikitommi10:06:30

for those, who want to test the latest - 2.0.0-alpha2 is out. Many small fixes + the spec coercion support: https://github.com/metosin/compojure-api/blob/master/CHANGELOG.md#200-alpha2-1362017

ikitommi10:06:28

supports both vanilla specs & data-specs. the latter feels like Schema:

(def app
  (api
    {:coercion :spec}

    (context "/math/:a" []
      ;; path-parameter should be a number
      :path-params [a :- spec/int?]

      (POST "/plus" []
        ;; query-parameters should be numbers, `c` defaults to 0
        :query-params [b :- spec/int?, {c :- spec/int? 0}]
        ;; body should be a map with a `:d` number
        :body [numbers {:d spec/int?}]
        ;; response for 200 should be a map with `:total` number
        :return {:total ::total}
        ;; input is already coerced here.
        (ok {:total (+ a b c (:d numbers))})))

    (context "/data-math" []
      (resource
        ;; to make coercion explicit
        {:coercion :spec
         :get {:parameters {:query-params {:x spec/int?, :y spec/int?}}
               :responses {200 {:schema ::total-body}}
               :handler (fn [{{:keys [x y]} :query-params}]
                          (ok {:total (+ x y)}))}}))))

(-> {:request-method :get
     :uri "/data-math"
     :query-params {:x "1", :y "2"}}
    (app)
    :body
    (slurp)
    (cheshire.core/parse-string true))
; {:total 3}

singen12:06:26

@ikitommi hey, is there a way convenient to wrap swagger url with middleware? for example say, if you wanted your api-docs to be accessible from only one ip

singen12:06:51

never mind, I found it

singen12:06:13

Doc is a bit misleading regarding imports on this area

plins17:06:36

@ikitommi, im testing the new alpha, but im unable to start the server

Exception in thread "main" java.io.FileNotFoundException: Could not locate compojure/api/request__init.class or compojure/api/request.clj on classpath., compiling:(compojure/api/coercion/schema.clj:1:1)
	at clojure.lang.Compiler.load(Compiler.java:7391)
	at clojure.lang.RT.loadResourceScript(RT.java:372)
	......
do i need to do anything else besides updating the deps to [metosin/compojure-api "2.0.0-alpha2"] ?

juhoteperi17:06:11

Huh, for some reason compojure.api.request ns is in test folder

plins17:06:42

is there some workaround that can be done ?

juhoteperi17:06:21

Except for cloning the repo and fixing this no, test files are not included in jar so new release is needed

plins17:06:39

hmm ok 😕

ikitommi17:06:48

oh, I’ll put a new alpha out.

ikitommi18:06:07

@plins [metosin/compojure-api "2.0.0-alpha3"]

plins18:06:12

thanks 🙂

plins18:06:18

@ikitommi, everything seems fine now, thank you

assoc-in23:06:50

Has anyone tried out the new alpha with swagger? I wasn't able to get the body and query params to show up as an option on the example for /math/:a/plus. The output in my swagger json is

"/math/{a}/plus":{  
         "post":{  
            "parameters":[  
               {  
                  "in":"path",
                  "name":"a",
                  "description":"",
                  "required":true,
                  "type":"string"
               }
            ],