From 9e1d7bb2750d30825226253573dc30ff66c23d69 Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Sat, 2 May 2015 20:22:29 +0300 Subject: [PATCH] deep-merge-swagger-data => set-swagger-data with user set update-in fn. defaults to deep-merge --- CHANGELOG.md | 7 +++---- src/ring/swagger/middleware.clj | 17 ++++++++++------- test/ring/swagger/middleware_test.clj | 8 ++++---- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7263a910..812f3c43 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,9 @@ ## 0.20.2 - add `:type "object"` to Swagger 2.0 Definitions, requested by [Ron](https://github.com/webron). -- in `ring.swagger.middleware`: - - middlewares can publish swagger-data to request via `deep-merge-swagger-data`. - - clients can extract data with `get-swagger-data`. - - `wrap-swagger-data` middleware to easy swagger-data publishing. +- Middlewares can define extra swagger data, set into a request, read out by swagger-docs + - setting data via `set-swagger-data`, reading data via `get-swagger-data`. + - `wrap-swagger-data` middleware for easy publishing of swagger-data. - updated dependencies: ```clojure diff --git a/src/ring/swagger/middleware.clj b/src/ring/swagger/middleware.clj index eb8914a9..b68fc44c 100644 --- a/src/ring/swagger/middleware.clj +++ b/src/ring/swagger/middleware.clj @@ -11,15 +11,18 @@ ;; middleware-based swagger parameters ;; -(defn deep-merge-swagger-data - "Deep-merges top-level swagger-data into a request. - Can be read with get-swagger-data." - [request data] - (update-in request [::data] deep-merge data)) +(defn set-swagger-data + "Sets extra top-level swagger-data into a request. + By default, deep-merges gives data in. Data + can be read with get-swagger-data." + ([request data] + (set-swagger-data request deep-merge data)) + ([request f & data] + (update-in request [::data] (partial apply f) data))) (defn get-swagger-data "Reads top-level swagger-data from request, pushed in by - deep-merge-swagger-data." + set-swagger-data." [request] (::data request)) @@ -27,7 +30,7 @@ "Middleware that adds top level swagger-data into request." [handler data] (fn [request] - (handler (deep-merge-swagger-data request data)))) + (handler (set-swagger-data request data)))) ;; ;; common utilities diff --git a/test/ring/swagger/middleware_test.clj b/test/ring/swagger/middleware_test.clj index dc77e74d..5faeed80 100644 --- a/test/ring/swagger/middleware_test.clj +++ b/test/ring/swagger/middleware_test.clj @@ -53,7 +53,7 @@ ((mw3 identity) ..request..) => {:abba 3 :jabba 3} ((mw3 identity :abba 4 :jabba 4 :doo 4) ..request..) => {:abba 4 :jabba 4 :doo 4})) -(fact "publishing and reading swagger-data by middlewares" +(fact "setting and getting swagger-data by middlewares" (let [request {:uri ..uri.., :request-method ..method..}] (fact "by default, no swagger-data is attached" @@ -62,10 +62,10 @@ (fact "middlwares can attach swagger-data to request" (let [enchanced (-> request - (deep-merge-swagger-data + (set-swagger-data {:produces [:json :edn]}) - (deep-merge-swagger-data - {:consumes [:json :edn]}))] + (set-swagger-data + assoc :consumes [:json :edn]))] (fact "original request is preserved" enchanced => (contains request))