Skip to content

Commit

Permalink
Merge pull request #849 from opqdonut/fix/map-of-decode-819
Browse files Browse the repository at this point in the history
fix: properly compose interceptors in :map-of json-transformer
  • Loading branch information
ikitommi authored Feb 8, 2023
2 parents a8d6344 + 09ad217 commit a87b424
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
18 changes: 15 additions & 3 deletions src/malli/transform.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@
(defn -transform-map-keys [f]
#(cond->> % (map? %) (into {} (map (fn [[k v]] [(f k) v])))))

(defn -transform-if-valid [f schema]
(let [validator (m/-validator schema)]
(fn [x]
(let [out (f x)]
(if (validator out)
out
x)))))

;;
;; sequential
;;
Expand Down Expand Up @@ -354,9 +362,13 @@
{:name :json
:decoders (-> (-json-decoders)
(assoc :map-of {:compile (fn [schema _]
(or (some-> schema (m/children) (first) (m/type) map-of-key-decoders
(m/-comp m/-keyword->string) (-transform-map-keys))
(-transform-map-keys m/-keyword->string)))})
(let [key-schema (some-> schema (m/children) (first))]
(or (some-> key-schema (m/type) map-of-key-decoders
(-interceptor schema {}) m/-intercepting
(m/-comp m/-keyword->string)
(-transform-if-valid key-schema)
(-transform-map-keys))
(-transform-map-keys m/-keyword->string))))})
(cond-> json-vectors (assoc :vector -sequential->vector)))
:encoders (-json-encoders)})))

Expand Down
25 changes: 20 additions & 5 deletions test/malli/transform_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@
(testing "map-of"
(is (= {1 :abba, 2 :jabba} (m/decode [:map-of int? keyword?] {"1" "abba", "2" "jabba"} mt/string-transformer)))
(is (= {1 :abba, 2 :jabba} (m/decode [:map-of int? keyword?] {"1" "abba", "2" "jabba"} mt/json-transformer)))
(testing "keeps original value if decoding fails"
(is (= {1 :abba, :xyz :jabba} (m/decode [:map-of int? keyword?] {"1" "abba", :xyz "jabba"} mt/json-transformer)))
(is (= {1 :abba, :xyz :jabba} (m/decode [:map-of int? keyword?] {"1" "abba", :xyz "jabba"} mt/json-transformer))))
(is (= nil (m/decode [:map-of int? keyword?] nil mt/string-transformer)))
(is (= ::invalid (m/decode [:map-of int? keyword?] ::invalid mt/json-transformer))))

Expand Down Expand Up @@ -348,6 +351,18 @@
[:repeat {:min 2, :max 4} int?] [1 :kikka] [1 :kikka]
[:repeat {:min 2, :max 4} int?] [1 2 3 4 5] [1 2 3 4 5]))))

(deftest map-of-enum-transform-test-819
(let [schema [:map-of
[:enum {:encode/json name
:decode/json keyword}
:a :b :c]
int?]
m {:a 1 :b 2}
encoded (m/encode schema m mt/json-transformer)
decoded (m/decode schema encoded mt/json-transformer)]
(is (= {"a" 1 "b" 2} encoded))
(is (= m decoded))))

(deftest collection-transform-test
(testing "decode"
(is (= #{1 2 3} (m/decode [:set int?] [1 2 3] mt/collection-transformer)))
Expand Down Expand Up @@ -889,11 +904,11 @@
{"0" "2ac307dc-4ec8-4046-9b7e-57716b7ecfd2"
"1" "820e5003-6fff-480b-9e2b-ec3cdc5d2f78"
"2" "017de28f-5801-8c62-9ce9-cef70883794a"}]]

(is (= {0 #uuid"2ac307dc-4ec8-4046-9b7e-57716b7ecfd2"
1 #uuid"820e5003-6fff-480b-9e2b-ec3cdc5d2f78"
2 #uuid"017de28f-5801-8c62-9ce9-cef70883794a"}
(m/decode schema data mt/json-transformer))))))
(testing data
(is (= {0 #uuid"2ac307dc-4ec8-4046-9b7e-57716b7ecfd2"
1 #uuid"820e5003-6fff-480b-9e2b-ec3cdc5d2f78"
2 #uuid"017de28f-5801-8c62-9ce9-cef70883794a"}
(m/decode schema data mt/json-transformer)))))))

#?(:clj
(deftest -safe-test
Expand Down

0 comments on commit a87b424

Please sign in to comment.