Skip to content

Commit ba352bb

Browse files
r0mandakrone
authored andcommitted
Separate Transit reader and writer options
As mentioned in issue dakrone#292, the Transit reader and writer require different options. At the moment `:transit-opts` is passed to both reader and writer, which means you can either specify read handlers or write handlers, but not both. Transit will complain if `:handlers` is a mix of read and write handlers. This patch splits up `:transit-opts` into a `:encode` and `:decode` section. The options in `:encode` will be passed to the writer, the options in `:decode` to the reader.
1 parent 12242fb commit ba352bb

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

README.org

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,12 +270,16 @@ content encodings.
270270
;; Send form params as a Transit encoded JSON body (POST or PUT) with options
271271
(client/post "http://site.com" {:form-params {:foo "bar"}
272272
:content-type :transit+json
273-
:transit-opts {:handlers {}}})
273+
:transit-opts
274+
{:encode {:handlers {}}
275+
:decode {:handlers {}}}})
274276

275277
;; Send form params as a Transit encoded MessagePack body (POST or PUT) with options
276278
(client/post "http://site.com" {:form-params {:foo "bar"}
277279
:content-type :transit+msgpack
278-
:transit-opts {:handlers {}}})
280+
:transit-opts
281+
{:encode {:handlers {}}
282+
:decode {:handlers {}}}})
279283

280284
;; Multipart form uploads/posts
281285
;; takes a vector of maps, to preserve the order of entities, :name

src/clj_http/client.clj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
{:pre [transit-enabled?]}
7373
(let [reader (ns-resolve 'cognitect.transit 'reader)
7474
read (ns-resolve 'cognitect.transit 'read)]
75-
(read (reader in type opts))))
75+
(read (reader in type (:decode opts)))))
7676

7777
(defn ^:dynamic transit-encode
7878
"Resolve and apply Transit's JSON/MessagePack encoding."
@@ -81,7 +81,7 @@
8181
(let [output (ByteArrayOutputStream.)
8282
writer (ns-resolve 'cognitect.transit 'writer)
8383
write (ns-resolve 'cognitect.transit 'write)]
84-
(write (writer output type opts) out)
84+
(write (writer output type (:encode opts)) out)
8585
(.toByteArray output)))
8686

8787
(defn ^:dynamic json-encode

test/clj_http/test/client_test.clj

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
[clojure.string :as str]
88
[clojure.java.io :refer [resource]]
99
[clojure.test :refer :all]
10+
[cognitect.transit :as transit]
1011
[ring.util.codec :refer [form-decode-str]]
1112
[ring.middleware.nested-params :refer [parse-nested-keys]])
1213
(:import (java.net UnknownHostException)
@@ -467,6 +468,26 @@
467468
client/parse-url
468469
:user-info))))
469470

471+
(defrecord Point [x y])
472+
473+
(def write-point
474+
"Write a point in Transit format."
475+
(transit/write-handler
476+
(constantly "point")
477+
(fn [point] [(:x point) (:y point)])
478+
(constantly nil)))
479+
480+
(def read-point
481+
"Read a point in Transit format."
482+
(transit/read-handler
483+
(fn [[x y]]
484+
(->Point x y))))
485+
486+
(def transit-opts
487+
"Transit read and write options."
488+
{:encode {:handlers {Point write-point}}
489+
:decode {:handlers {"point" read-point}}})
490+
470491
(deftest apply-on-form-params
471492
(testing "With form params"
472493
(let [param-client (client/wrap-form-params identity)
@@ -522,7 +543,7 @@
522543
(testing "With EDN form params"
523544
(doseq [method [:post :put :patch]]
524545
(let [param-client (client/wrap-form-params identity)
525-
params {:param1 "value1" :param2 "value2"}
546+
params {:param1 "value1" :param2 (Point. 1 2)}
526547
resp (param-client {:request-method method
527548
:content-type :edn
528549
:form-params params})]
@@ -533,12 +554,14 @@
533554
(testing "With Transit/JSON form params"
534555
(doseq [method [:post :put :patch]]
535556
(let [param-client (client/wrap-form-params identity)
536-
params {:param1 "value1" :param2 "value2"}
557+
params {:param1 "value1" :param2 (Point. 1 2)}
537558
resp (param-client {:request-method method
538559
:content-type :transit+json
539-
:form-params params})]
560+
:form-params params
561+
:transit-opts transit-opts})]
540562
(is (= params (client/parse-transit
541-
(ByteArrayInputStream. (:body resp)) :json)))
563+
(ByteArrayInputStream. (:body resp))
564+
:json transit-opts)))
542565
(is (= "application/transit+json" (:content-type resp)))
543566
(is (not (contains? resp :form-params))))))
544567

@@ -548,9 +571,11 @@
548571
params {:param1 "value1" :param2 "value2"}
549572
resp (param-client {:request-method method
550573
:content-type :transit+msgpack
551-
:form-params params})]
574+
:form-params params
575+
:transit-opts transit-opts})]
552576
(is (= params (client/parse-transit
553-
(ByteArrayInputStream. (:body resp)) :msgpack)))
577+
(ByteArrayInputStream. (:body resp))
578+
:msgpack transit-opts)))
554579
(is (= "application/transit+msgpack" (:content-type resp)))
555580
(is (not (contains? resp :form-params))))))
556581

0 commit comments

Comments
 (0)