forked from loafle/openapi-generator-original
Support collectionFormat for array parameters in Clojure client
Closes #1655
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
{{=< >=}}(ns <package>.<classname>
|
||||
(:require [<baseNamespace>.core :refer [call-api check-required-params]])
|
||||
(:require [<baseNamespace>.core :refer [call-api check-required-params with-collection-format]])
|
||||
(:import (java.io File)))
|
||||
<#operations><#operation>
|
||||
(defn <operationId>
|
||||
@@ -9,10 +9,10 @@
|
||||
<#hasOptionalParams>(</hasOptionalParams>[<#allParams><#required><#isFile>^File </isFile><paramName> </required></allParams><#hasOptionalParams>{:keys [<#allParams><^required><#isFile>^File </isFile><paramName> </required></allParams>]}</hasOptionalParams>]<#hasRequiredParams>
|
||||
<#hasOptionalParams> </hasOptionalParams>(check-required-params<#allParams><#required> <paramName></required></allParams>)</hasRequiredParams>
|
||||
<#hasOptionalParams> </hasOptionalParams>(call-api "<path>" :<httpMethod>
|
||||
<#hasOptionalParams> </hasOptionalParams> {:path-params {<#pathParams>"<baseName>" <paramName> </pathParams>}
|
||||
<#hasOptionalParams> </hasOptionalParams> :header-params {<#headerParams>"<baseName>" <paramName> </headerParams>}
|
||||
<#hasOptionalParams> </hasOptionalParams> :query-params {<#queryParams>"<baseName>" <paramName> </queryParams>}
|
||||
<#hasOptionalParams> </hasOptionalParams> :form-params {<#formParams>"<baseName>" <paramName> </formParams>}<#bodyParam>
|
||||
<#hasOptionalParams> </hasOptionalParams> {:path-params {<#pathParams>"<baseName>" <#collectionFormat>(with-collection-format <paramName> :<collectionFormat>)</collectionFormat><^collectionFormat><paramName></collectionFormat> </pathParams>}
|
||||
<#hasOptionalParams> </hasOptionalParams> :header-params {<#headerParams>"<baseName>" <#collectionFormat>(with-collection-format <paramName> :<collectionFormat>)</collectionFormat><^collectionFormat><paramName></collectionFormat> </headerParams>}
|
||||
<#hasOptionalParams> </hasOptionalParams> :query-params {<#queryParams>"<baseName>" <#collectionFormat>(with-collection-format <paramName> :<collectionFormat>)</collectionFormat><^collectionFormat><paramName></collectionFormat> </queryParams>}
|
||||
<#hasOptionalParams> </hasOptionalParams> :form-params {<#formParams>"<baseName>" <#collectionFormat>(with-collection-format <paramName> :<collectionFormat>)</collectionFormat><^collectionFormat><paramName></collectionFormat> </formParams>}<#bodyParam>
|
||||
<#hasOptionalParams> </hasOptionalParams> :body-param <paramName></bodyParam>
|
||||
<#hasOptionalParams> </hasOptionalParams> :content-types [<#consumes>"<mediaType>"<#hasMore> </hasMore></consumes>]
|
||||
<#hasOptionalParams> </hasOptionalParams> :accepts [<#produces>"<mediaType>"<#hasMore> </hasMore></produces>]
|
||||
|
||||
@@ -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)))
|
||||
|
||||
|
||||
@@ -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"]
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)))
|
||||
|
||||
|
||||
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user