Skip to content

Commit

Permalink
deep-merge-swagger-data => set-swagger-data with user set update-in f…
Browse files Browse the repository at this point in the history
…n. defaults to deep-merge
  • Loading branch information
ikitommi committed May 2, 2015
1 parent 8f81d2f commit 9e1d7bb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
7 changes: 3 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
17 changes: 10 additions & 7 deletions src/ring/swagger/middleware.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,26 @@
;; 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))

(defn wrap-swagger-data
"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
Expand Down
8 changes: 4 additions & 4 deletions test/ring/swagger/middleware_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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))
Expand Down

0 comments on commit 9e1d7bb

Please sign in to comment.