Fork me on GitHub
#ring-swagger
<
2017-10-20
>
acron10:10:02

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

acron10:10:52

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

acron10:10:26

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

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

acron10:10:01

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

acron10:10:12

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.

acron10:10:45

=> (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 {}}

acron10:10:24

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?

ikitommi12:10:49

@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

acron13:10:34

Thanks, I will take a look!

acron13:10:44

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

ikitommi13:10:27

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

ikitommi13:10:28

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.

acron13:10:22

(-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?

acron13:10:12

method is nil in the case of ANY

ikitommi14:10:22

looks good to me.