Skip to content

Commit

Permalink
Fix swagger endpoints and refactor code (#261)
Browse files Browse the repository at this point in the history
* Fix swagger endpoints and refactor code

* Apply cljstyle

* Fix test errors

Co-authored-by: Iain Wood <iain.wood@flexiana.com>

---------

Co-authored-by: Iain Wood <iain.wood@flexiana.com>
  • Loading branch information
gmsvalente and Iain Wood authored Nov 10, 2023
1 parent 776663a commit fb6c4b6
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 55 deletions.
110 changes: 57 additions & 53 deletions src/xiana/swagger.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,64 +13,68 @@
[reitit.trie :as trie]
[ring.util.response]))

;; "xiana-route->reitit-route is taking route entry of our custom shape of routes
;; and transforms it into proper reitit route entry that is valid on the Swagger
;; implemention of reitit.

;; (xiana-route->reitit-route [\"/swagger-ui\" {:action :swagger-ui
;; :some-values true}])
;; ;; => [\"/swagger-ui\"
;; {:get
;; {:handler #function[clojure.core/identity], :action :swagger-ui},
;; :patch
;; {:handler #function[clojure.core/identity], :action :swagger-ui},
;; :trace
;; {:handler #function[clojure.core/identity], :action :swagger-ui},
;; :connect
;; {:handler #function[clojure.core/identity], :action :swagger-ui},
;; :delete
;; {:handler #function[clojure.core/identity], :action :swagger-ui},
;; :head
;; {:handler #function[clojure.core/identity], :action :swagger-ui},
;; :post
;; {:handler #function[clojure.core/identity], :action :swagger-ui},
;; :action :swagger-ui,
;; :options
;; {:handler #function[clojure.core/identity], :action :swagger-ui},
;; :put
;; {:handler #function[clojure.core/identity], :action :swagger-ui},
;; :some-values true}]
;; "

(def all-methods
[:get :patch :trace :connect :delete :head :post :options :put])

(defn- no-method?
[opt-map]
(:action opt-map))

(defn- reduce-opt-map
[opt-map all-methods]
(reduce (fn [acc method]
(if (get acc method)
(if (get-in acc [method :handler])
acc
(-> acc
(assoc-in [method :handler] identity)))
acc))
opt-map
all-methods))

(defn- process-opt-map
[opt-map all-methods]
(if (no-method? opt-map)
(-> opt-map
(assoc-in [:get :handler] identity)
(assoc-in [:get :action] (:action opt-map)))
(reduce-opt-map opt-map all-methods)))

(defn xiana-route->reitit-route
"xiana-route->reitit-route is taking route entry of our custom shape of routes
and transforms it into proper reitit route entry that is valid on the Swagger
implemention of reitit.
(xiana-route->reitit-route [\"/swagger-ui\" {:action :swagger-ui
:some-values true}])
;; => [\"/swagger-ui\"
{:get
{:handler #function[clojure.core/identity], :action :swagger-ui},
:patch
{:handler #function[clojure.core/identity], :action :swagger-ui},
:trace
{:handler #function[clojure.core/identity], :action :swagger-ui},
:connect
{:handler #function[clojure.core/identity], :action :swagger-ui},
:delete
{:handler #function[clojure.core/identity], :action :swagger-ui},
:head
{:handler #function[clojure.core/identity], :action :swagger-ui},
:post
{:handler #function[clojure.core/identity], :action :swagger-ui},
:action :swagger-ui,
:options
{:handler #function[clojure.core/identity], :action :swagger-ui},
:put
{:handler #function[clojure.core/identity], :action :swagger-ui},
:some-values true}]
"
[[url opt-map & nested-routes :as route] all-methods]
(let [new-opt-map (if (:action opt-map)
(let [action' (:action opt-map)
swagger-base-of-endpoint (:swagger-* opt-map)]
(reduce (fn [acc method]
(-> acc
(assoc-in [method :handler] identity)
(assoc-in [method :action] action')
(merge swagger-base-of-endpoint)))
opt-map
all-methods))
(let [swagger-base-of-endpoint (get opt-map :swagger-* {})]
(reduce (fn [acc method]
(if (get acc method)
(if (get-in acc [method :handler])
acc
(-> acc
(assoc-in [method :handler] identity)
(merge swagger-base-of-endpoint)))
acc))
opt-map
all-methods)))]
(if (-> route meta :no-doc)
nil
(apply conj [url new-opt-map]
(map #(xiana-route->reitit-route % all-methods) nested-routes)))))
(when-not (-> route meta :no-doc)
(apply conj
[url (process-opt-map opt-map all-methods)]
(map #(xiana-route->reitit-route % all-methods) nested-routes))))

(defn xiana-routes->reitit-routes
"Transforms routes to the proper reitit form."
Expand Down
4 changes: 2 additions & 2 deletions test/xiana/swagger_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
count)]
(is (= generated-route-count
1))))
(testing "Actions should generate every methods"
(testing "Actions should generate only get methods if not specified"
(let [index-generated-methods-by-sample (->
generated-swagger-data
:paths
Expand All @@ -66,4 +66,4 @@
set)]
(is (=
index-generated-methods-by-sample
(set sut/all-methods)))))))))
(set [:get])))))))))

0 comments on commit fb6c4b6

Please sign in to comment.