mirror of
https://github.com/metosin/reitit.git
synced 2025-12-24 19:08:24 +00:00
Fixes after review
* Support catch-all-param as a swagger path template part
This commit is contained in:
parent
b7341265e9
commit
43f5a9ebe8
2 changed files with 8 additions and 16 deletions
|
|
@ -1,5 +1,6 @@
|
|||
(ns reitit.swagger
|
||||
(:require [reitit.core :as r]
|
||||
[reitit.impl :as impl]
|
||||
[meta-merge.core :refer [meta-merge]]
|
||||
[clojure.spec.alpha :as s]
|
||||
[clojure.set :as set]
|
||||
|
|
@ -63,20 +64,11 @@
|
|||
{:name ::swagger
|
||||
:spec ::spec})
|
||||
|
||||
(defn- path->template [path endpoint]
|
||||
(let [path-parameters (filter (fn [{:keys [in]}]
|
||||
(= in "path"))
|
||||
(mapcat (fn [[_ {:keys [parameters]}]]
|
||||
parameters)
|
||||
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- path->template [path]
|
||||
(->> (impl/segments path)
|
||||
(map #(if (impl/wild-or-catch-all-param? %)
|
||||
(str "{" (subs % 1) "}") %))
|
||||
(string/join "/")))
|
||||
|
||||
(defn create-swagger-handler []
|
||||
"Create a ring handler to emit swagger spec. Collects all routes from router which have
|
||||
|
|
@ -99,7 +91,7 @@
|
|||
(dissoc swagger :id))]))
|
||||
transform-path (fn [[p _ c]]
|
||||
(if-let [endpoint (some->> c (keep transform-endpoint) (seq) (into {}))]
|
||||
[(path->template p endpoint) endpoint]))]
|
||||
[(path->template p) endpoint]))]
|
||||
(if id
|
||||
(let [paths (->> router (r/routes) (filter accept-route) (map transform-path) (into {}))]
|
||||
{:status 200
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@
|
|||
{:status 200, :body {:total (+ x y z)}})}}]]
|
||||
|
||||
["/schema" {:coercion schema/coercion}
|
||||
["/plus/:z"
|
||||
["/plus/*z"
|
||||
{:get {:summary "plus"
|
||||
:parameters {:query {:x Int, :y Int}
|
||||
:path {:z Int}}
|
||||
|
|
|
|||
Loading…
Reference in a new issue