Support collectionFormat for array parameters in Clojure client

Closes #1655
This commit is contained in:
xhh
2015-12-03 17:25:27 +08:00
parent afb7e31e21
commit 9d6b1bb224
7 changed files with 85 additions and 28 deletions

View File

@@ -1,5 +1,5 @@
(ns swagger-petstore.api.pet
(:require [swagger-petstore.core :refer [call-api check-required-params]])
(:require [swagger-petstore.core :refer [call-api check-required-params with-collection-format]])
(:import (java.io File)))
(defn update-pet
@@ -40,7 +40,7 @@
(call-api "/pet/findByStatus" :get
{:path-params {}
:header-params {}
:query-params {"status" status }
:query-params {"status" (with-collection-format status :multi) }
:form-params {}
:content-types []
:accepts ["application/json" "application/xml"]
@@ -54,7 +54,7 @@
(call-api "/pet/findByTags" :get
{:path-params {}
:header-params {}
:query-params {"tags" tags }
:query-params {"tags" (with-collection-format tags :multi) }
:form-params {}
:content-types []
:accepts ["application/json" "application/xml"]

View File

@@ -1,5 +1,5 @@
(ns swagger-petstore.api.store
(:require [swagger-petstore.core :refer [call-api check-required-params]])
(:require [swagger-petstore.core :refer [call-api check-required-params with-collection-format]])
(:import (java.io File)))
(defn get-inventory

View File

@@ -1,5 +1,5 @@
(ns swagger-petstore.api.user
(:require [swagger-petstore.core :refer [call-api check-required-params]])
(:require [swagger-petstore.core :refer [call-api check-required-params with-collection-format]])
(:import (java.io File)))
(defn create-user

View File

@@ -29,8 +29,8 @@
[api-context & body]
`(let [api-context# ~api-context
api-context# (-> *api-context*
(merge api-context#)
(assoc :auths (merge (:auths *api-context*) (:auths api-context#))))]
(merge api-context#)
(assoc :auths (merge (:auths *api-context*) (:auths api-context#))))]
(binding [*api-context* api-context#]
~@body)))
@@ -43,6 +43,11 @@
(throw (IllegalArgumentException. ~(str "The parameter \"" p "\" is required"))))))
(list* 'do)))
(defn with-collection-format
"Attach collection-format to meta data of the given parameter."
[param collection-format]
(and param (with-meta param {:collection-format collection-format})))
(defn- ^SimpleDateFormat make-date-format
([^String format-str] (make-date-format format-str nil))
([^String format-str ^String time-zone]
@@ -119,23 +124,40 @@
[auth-names]
(reduce process-auth {} auth-names))
(declare normalize-param)
(defn make-url
"Make full URL by adding base URL and filling path parameters."
[path path-params]
(let [path (reduce (fn [p [k v]]
(str/replace p (re-pattern (str "\\{" k "\\}")) (param->str v)))
(str/replace p (re-pattern (str "\\{" k "\\}")) (normalize-param v)))
path
path-params)]
(str (:base-url *api-context*) path)))
(defn normalize-array-param
"Normalize array paramater according to :collection-format specified in the parameter's meta data.
When the parameter contains File, a seq is returned so as to keep File parameters.
For :multi collection format, a seq is returned which will be handled properly by clj-http.
For other cases, a string is returned."
[xs]
(if (some (partial instance? File) xs)
(map normalize-param xs)
(case (-> (meta xs) :collection-format (or :csv))
:csv (str/join "," (map normalize-param xs))
:ssv (str/join " " (map normalize-param xs))
:tsv (str/join "\t" (map normalize-param xs))
:pipes (str/join "|" (map normalize-param xs))
:multi (map normalize-param xs))))
(defn normalize-param
"Normalize parameter value, handling three cases:
for sequential value, normalize each elements of it;
for File value, do nothing with it;
otherwise, call `param->str`."
for sequential value, apply `normalize-array-param` which handles collection format;
for File value, use current value;
otherwise, apply `param->str`."
[param]
(cond
(sequential? param) (map normalize-param param)
(sequential? param) (normalize-array-param param)
(instance? File param) param
:else (param->str param)))

View File

@@ -124,14 +124,27 @@
(let [file (-> "hello.txt" io/resource io/file)]
(are [param expected]
(is (= expected (normalize-param param)))
[12 "34"] ["12" "34"]
file file
"abc" "abc"
[[12 "34"] file "abc"] [["12" "34"] file "abc"])))
[12 "34"] "12,34"
^{:collection-format :csv} [12 "34"] "12,34"
^{:collection-format :ssv} [12 "34"] "12 34"
^{:collection-format :tsv} [12 "34"] "12\t34"
(with-collection-format [12 "34"] :pipes) "12|34"
(with-collection-format [12 "34"] :multi) ["12" "34"]
[[12 "34"] file "abc"] ["12,34" file "abc"])))
(deftest test-normalize-params
(is (= {:a "123" :b ["4" ["5" "6"]]}
(normalize-params {:a 123 :b [4 [5 "6"]] :c nil}))))
(is (= {:a "123" :b "4,5,6"}
(normalize-params {:a 123 :b [4 [5 "6"]] :c nil})))
(is (= {:a "123" :b ["4" "5,6"]}
(normalize-params {:a 123
:b ^{:collection-format :multi} [4 [5 "6"]]
:c nil})))
(is (= {:a "123" :b "4 5|6"}
(normalize-params {:a 123
:b (with-collection-format [4 (with-collection-format [5 "6"] :pipes)] :ssv)
:c nil}))))
(deftest test-json-mime?
(are [mime expected]