Merge pull request #91 from DeLaGuardo/master

Fix paths in swagger.json for endpoints with path parameters
This commit is contained in:
Tommi Reiman 2018-05-29 07:51:22 +03:00 committed by GitHub
commit c21dca9d6e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 74 additions and 50 deletions

View file

@ -1,8 +1,10 @@
(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]
[clojure.string :as string]
[reitit.coercion :as coercion]))
(s/def ::id (s/or :keyword keyword? :set (s/coll-of keyword? :into #{})))
@ -62,6 +64,12 @@
{:name ::swagger
:spec ::spec})
(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
an intersecting `[:swagger :id]` and which are not marked with `:no-doc` route data."
@ -83,7 +91,7 @@
(dissoc swagger :id))]))
transform-path (fn [[p _ c]]
(if-let [endpoint (some->> c (keep transform-endpoint) (seq) (into {}))]
[p endpoint]))]
[(path->template p) endpoint]))]
(if id
(let [paths (->> router (r/routes) (filter accept-route) (map transform-path) (into {}))]
{:status 200

View file

@ -19,20 +19,24 @@
:handler (swagger/create-swagger-handler)}}]
["/spec" {:coercion spec/coercion}
["/plus"
["/plus/:z"
{:get {:summary "plus"
:parameters {:query {:x int?, :y int?}}
:parameters {:query {:x int?, :y int?}
:path {:z int?}}
:responses {200 {:body {:total int?}}}
:handler (fn [{{{:keys [x y]} :query} :parameters}]
{:status 200, :body {:total (+ x y)}})}}]]
:handler (fn [{{{:keys [x y]} :query
{:keys [z]} :path} :parameters}]
{:status 200, :body {:total (+ x y z)}})}}]]
["/schema" {:coercion schema/coercion}
["/plus"
["/plus/*z"
{:get {:summary "plus"
:parameters {:query {:x Int, :y Int}}
:parameters {:query {:x Int, :y Int}
:path {:z Int}}
:responses {200 {:body {:total Int}}}
:handler (fn [{{{:keys [x y]} :query} :parameters}]
{:status 200, :body {:total (+ x y)}})}}]]]
:handler (fn [{{{:keys [x y]} :query
{:keys [z]} :path} :parameters}]
{:status 200, :body {:total (+ x y z)}})}}]]]
{:data {:middleware [swagger/swagger-feature
rrc/coerce-exceptions-middleware
@ -42,16 +46,16 @@
(deftest swagger-test
(testing "endpoints work"
(testing "spec"
(is (= {:body {:total 3}, :status 200}
(is (= {:body {:total 6}, :status 200}
(app
{:request-method :get
:uri "/api/spec/plus"
:uri "/api/spec/plus/3"
:query-params {:x "2", :y "1"}}))))
(testing "schema"
(is (= {:body {:total 3}, :status 200}
(is (= {:body {:total 6}, :status 200}
(app
{:request-method :get
:uri "/api/schema/plus"
:uri "/api/schema/plus/3"
:query-params {:x "2", :y "1"}})))))
(testing "swagger-spec"
(let [spec (:body (app
@ -60,7 +64,7 @@
(is (= {:x-id #{::math}
:swagger "2.0"
:info {:title "my-api"}
:paths {"/api/schema/plus" {:get {:parameters [{:description ""
:paths {"/api/schema/plus/{z}" {:get {:parameters [{:description ""
:format "int32"
:in "query"
:name "x"
@ -71,7 +75,13 @@
:in "query"
:name "y"
:required true
:type "integer"}]
:type "integer"}
{:in "path"
:name "z"
:description ""
:type "integer"
:required true
:format "int32"}]
:responses {200 {:description ""
:schema {:additionalProperties false
:properties {"total" {:format "int32"
@ -79,7 +89,7 @@
:required ["total"]
:type "object"}}}
:summary "plus"}}
"/api/spec/plus" {:get {:parameters [{:description ""
"/api/spec/plus/{z}" {:get {:parameters [{:description ""
:format "int64"
:in "query"
:name "x"
@ -90,7 +100,13 @@
:in "query"
:name "y"
:required true
:type "integer"}]
:type "integer"}
{:in "path"
:name "z"
:description ""
:type "integer"
:required true
:format "int64"}]
:responses {200 {:description ""
:schema {:properties {"total" {:format "int64"
:type "integer"}}