Fixes after review

* Support catch-all-param as a swagger path template part
This commit is contained in:
Kirill Chernyshov 2018-05-29 01:07:22 +02:00
parent b7341265e9
commit 43f5a9ebe8
2 changed files with 8 additions and 16 deletions

View file

@ -1,5 +1,6 @@
(ns reitit.swagger (ns reitit.swagger
(:require [reitit.core :as r] (:require [reitit.core :as r]
[reitit.impl :as impl]
[meta-merge.core :refer [meta-merge]] [meta-merge.core :refer [meta-merge]]
[clojure.spec.alpha :as s] [clojure.spec.alpha :as s]
[clojure.set :as set] [clojure.set :as set]
@ -63,20 +64,11 @@
{:name ::swagger {:name ::swagger
:spec ::spec}) :spec ::spec})
(defn- path->template [path endpoint] (defn- path->template [path]
(let [path-parameters (filter (fn [{:keys [in]}] (->> (impl/segments path)
(= in "path")) (map #(if (impl/wild-or-catch-all-param? %)
(mapcat (fn [[_ {:keys [parameters]}]] (str "{" (subs % 1) "}") %))
parameters) (string/join "/")))
endpoint))]
(loop [{:keys [name] :as path-parameter} (first path-parameters)
path-parameters (rest path-parameters)
path path]
(if path-parameter
(recur (first path-parameters)
(rest path-parameters)
(string/replace path (re-pattern (str ":" name)) (str "{" name "}")))
path))))
(defn create-swagger-handler [] (defn create-swagger-handler []
"Create a ring handler to emit swagger spec. Collects all routes from router which have "Create a ring handler to emit swagger spec. Collects all routes from router which have
@ -99,7 +91,7 @@
(dissoc swagger :id))])) (dissoc swagger :id))]))
transform-path (fn [[p _ c]] transform-path (fn [[p _ c]]
(if-let [endpoint (some->> c (keep transform-endpoint) (seq) (into {}))] (if-let [endpoint (some->> c (keep transform-endpoint) (seq) (into {}))]
[(path->template p endpoint) endpoint]))] [(path->template p) endpoint]))]
(if id (if id
(let [paths (->> router (r/routes) (filter accept-route) (map transform-path) (into {}))] (let [paths (->> router (r/routes) (filter accept-route) (map transform-path) (into {}))]
{:status 200 {:status 200

View file

@ -29,7 +29,7 @@
{:status 200, :body {:total (+ x y z)}})}}]] {:status 200, :body {:total (+ x y z)}})}}]]
["/schema" {:coercion schema/coercion} ["/schema" {:coercion schema/coercion}
["/plus/:z" ["/plus/*z"
{:get {:summary "plus" {:get {:summary "plus"
:parameters {:query {:x Int, :y Int} :parameters {:query {:x Int, :y Int}
:path {:z Int}} :path {:z Int}}