Skip to content

Commit

Permalink
Merge pull request #50 from harti2006/enable-multipart-params
Browse files Browse the repository at this point in the history
enable multipart parameters
  • Loading branch information
André Hartmann authored Nov 8, 2016
2 parents c566cde + 4ecf095 commit 532d126
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
12 changes: 8 additions & 4 deletions src/io/sarnowski/swagger1st/parser.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
[io.sarnowski.swagger1st.mapper :refer [split-path]]
[io.sarnowski.swagger1st.util.api :as api]
[clj-time.format :as f]
[ring.middleware.params :refer [params-request]])
[ring.middleware.params :refer [params-request]]
[ring.middleware.multipart-params :refer [multipart-params-request]])
(:import (org.joda.time DateTime)
(java.io PrintWriter)
(clojure.lang ExceptionInfo)))
Expand Down Expand Up @@ -69,8 +70,11 @@

(defn extract-parameter-form
"Extract a parameter from the request body form."
[{form-params :form-params} definition]
(get form-params (get definition "name")))
[{:keys [form-params multipart-params]} definition]
(let [param-name (get definition "name")]
(or
(get form-params param-name)
(get multipart-params param-name))))

(defn extract-parameter-body
"Extract a parameter from the request body."
Expand Down Expand Up @@ -342,7 +346,7 @@
(defn parse
"Executes all prepared functions for the request."
[{:keys [parsers]} next-handler request]
(let [request (params-request request)]
(let [request (-> request params-request multipart-params-request)]
(try
(let [parameters (->> (get parsers (-> request :swagger :key))
; execute all parsers of the request
Expand Down
16 changes: 11 additions & 5 deletions test/io/sarnowski/swagger1st/parser_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,14 @@
"properties" {"foo" {"type" "string"}}}
:allow-undefined-keys true))))

(deftest file-values
(testing "parameters of type 'file' are just passed through"
(let [value {:filename "screenshot.png",
:content-type "image/png",
:tempfile :a-file-handle,
:size 84647}]
(is (identical? value (parse value {"type" "file"}))))))

(deftest extract-path-parameters
(is (= "baz"
(p/extract-parameter-path
Expand All @@ -274,11 +282,9 @@
{"name" "bar"}))))

(deftest extract-form-parameters
(is (= "baz"
(p/extract-parameter-form
{:uri "/foo"
:form-params {"bar" "baz"}}
{"name" "bar"}))))
(are [request] (= "baz" (p/extract-parameter-form request {"name" "bar"}))
{:uri "/foo", :form-params {"bar" "baz"}}
{:uri "/foo", :multipart-params {"bar" "baz"}}))

(defn json-body
"Generates a reader that can be used to read serialized JSON."
Expand Down

0 comments on commit 532d126

Please sign in to comment.