ring-swagger

ring-swagger & compojure-api
acron 2017-10-20T10:33:02.000095Z

@ikitommi Hello! I noticed another Swagger validation error and I think I've tracked it down to the 'ANY' macro:

acron 2017-10-20T10:33:52.000013Z

=> (def app (context "/a" []
             (routes
               (context "/" []
                 (ANY "/*" [] identity)))))
=> (extract-paths app)
#linked/map [["/a/*" {nil {}}]]

acron 2017-10-20T10:35:26.000013Z

I think that nil is contributing to a Swagger doc which looks like:

"/*": {
  "": {
    "responses": {
      "default": {
        "description": ""
      }
    }
  }
}

acron 2017-10-20T10:36:01.000142Z

It's the empty string key that's causing the error

acron 2017-10-20T10:37:12.000232Z

I was hoping for some advice regarding the best place to implement a fix for this. I'm not sure if it's compojure-api which should ignore ANY in extract-paths or something in spec-tools, or something else.

acron 2017-10-20T10:52:45.000324Z

=> (swagger2/swagger-json {:paths {"/*" {nil {}}}})
{:swagger "2.0", :info {:title "Swagger API", :version "0.0.1"}, :produces ["application/json"], :consumes ["application/json"], :paths {"/*" {nil {:responses {:default {:description ""}}}}}, :definitions {}}

acron 2017-10-20T10:54:24.000161Z

I guess the other question is, when it encounters ANY what should the behaviour be? Should extract-paths create an entry for each HTTP verb or omit the path?

ikitommi 2017-10-20T12:56:49.000408Z

@acron hmm… I think ANY could map to all verbs. Because all of them work. And I believe the fix should be here: https://github.com/metosin/compojure-api/blob/master/src/compojure/api/routes.clj#L67-L76

acron 2017-10-20T13:03:34.000355Z

Thanks, I will take a look!

acron 2017-10-20T13:20:44.000408Z

@ikitommi I'm worried that fiddling with this will impact the way routes are matched, or am I wrong?

ikitommi 2017-10-20T13:21:27.000047Z

no, it’s just docs. the routing still uses Compojure directly.

ikitommi 2017-10-20T13:22:28.000128Z

if you evaluate any route macro at repl, you’ll see a Route record. It has a :handler key, which is used for actual dispatch + a lot of extra info, extracted for docs.

acron 2017-10-20T13:32:22.000349Z

(-get-routes [this options]
    (let [this (-> this realize-childs)
          valid-childs (filter-routes this options)
          make-method-path-fn (fn [m] [path m info])]
      (if (-> this filter-childs :childs seq)
        (vec
         (for [[p m i] (mapcat #(-get-routes % options) valid-childs)]
           [(->paths path p) m (rsc/deep-merge info i)]))
        (into [] (cond
                   (and path method) [(make-method-path-fn method)]
                   path (mapv make-method-path-fn #{:get :post :head :etc}))))))
something like this?

acron 2017-10-20T13:33:12.000223Z

method is nil in the case of ANY

ikitommi 2017-10-20T14:31:22.000401Z

looks good to me.

acron 2017-10-20T14:31:43.000266Z

@ikitommi Thanks, I've put in a PR. No rush though https://github.com/metosin/compojure-api/pull/346