From 3e8eaa48d7f6b5caa721f6d27ad4bb6ba5f0b6ba Mon Sep 17 00:00:00 2001 From: piotr-yuxuan Date: Tue, 19 May 2020 23:30:44 +0100 Subject: [PATCH 1/2] Fix default description https://github.com/metosin/reitit/issues/403 --- modules/reitit-swagger/src/reitit/swagger.cljc | 1 + test/cljc/reitit/swagger_test.clj | 11 ++++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/modules/reitit-swagger/src/reitit/swagger.cljc b/modules/reitit-swagger/src/reitit/swagger.cljc index 64a9c8c8..f09986aa 100644 --- a/modules/reitit-swagger/src/reitit/swagger.cljc +++ b/modules/reitit-swagger/src/reitit/swagger.cljc @@ -92,6 +92,7 @@ (apply meta-merge (keep (comp :swagger :data) interceptors)) (if coercion (coercion/get-apidocs coercion :swagger data)) + (when-not (:responses data) {:responses {:default {:description ""}}}) (select-keys data [:tags :summary :description]) (strip-top-level-keys swagger))])) transform-path (fn [[p _ c]] diff --git a/test/cljc/reitit/swagger_test.clj b/test/cljc/reitit/swagger_test.clj index 5ef99a63..9b774bb7 100644 --- a/test/cljc/reitit/swagger_test.clj +++ b/test/cljc/reitit/swagger_test.clj @@ -25,7 +25,9 @@ ["/spec" {:coercion spec/coercion} ["/plus/:z" - {:get {:summary "plus" + {:patch {:summary "patch" + :handler (constantly {:status 200})} + :get {:summary "plus" :parameters {:query {:x int?, :y int?} :path {:z int?}} :swagger {:responses {400 {:schema {:type "string"} @@ -120,7 +122,10 @@ expected {:x-id #{::math} :swagger "2.0" :info {:title "my-api"} - :paths {"/api/spec/plus/{z}" {:get {:parameters [{:in "query" + :paths {"/api/spec/plus/{z}" {:patch {:parameters [] + :summary "patch" + :responses {:default {:description ""}}} + :get {:parameters [{:in "query" :name "x" :description "" :required true @@ -312,7 +317,7 @@ (is (= ["/common/ping" "/one/ping" "/two/ping" "/two/deep/ping"] (spec-paths app "/one-two/swagger.json"))))) -(deftest swagger-ui-congif-test +(deftest swagger-ui-config-test (let [app (swagger-ui/create-swagger-ui-handler {:path "/" :config {:jsonEditor true}})] From 6e332e46f1f7496bf94ceb42a415bfc4ef1b49ed Mon Sep 17 00:00:00 2001 From: piotr-yuxuan Date: Wed, 20 May 2020 22:44:51 +0100 Subject: [PATCH 2/2] Low-priority default :responses in Swagger spec According to meta-merge documentation, ^:displace is a good solution for default values. This is still inside the meta-merge, as I don't see where else I could put it. Perhaps a better solution is available. --- modules/reitit-swagger/src/reitit/swagger.cljc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/reitit-swagger/src/reitit/swagger.cljc b/modules/reitit-swagger/src/reitit/swagger.cljc index f09986aa..fb0c0742 100644 --- a/modules/reitit-swagger/src/reitit/swagger.cljc +++ b/modules/reitit-swagger/src/reitit/swagger.cljc @@ -82,17 +82,18 @@ :x-id ids})) accept-route (fn [route] (-> route second :swagger :id (or ::default) (trie/into-set) (set/intersection ids) seq)) + base-swagger-spec {:responses ^:displace {:default {:description ""}}} transform-endpoint (fn [[method {{:keys [coercion no-doc swagger] :as data} :data middleware :middleware interceptors :interceptors}]] (if (and data (not no-doc)) [method (meta-merge + base-swagger-spec (apply meta-merge (keep (comp :swagger :data) middleware)) (apply meta-merge (keep (comp :swagger :data) interceptors)) (if coercion (coercion/get-apidocs coercion :swagger data)) - (when-not (:responses data) {:responses {:default {:description ""}}}) (select-keys data [:tags :summary :description]) (strip-top-level-keys swagger))])) transform-path (fn [[p _ c]]