From 15b56d0f9fdde198fb3e966f70068cc580cc56dd Mon Sep 17 00:00:00 2001 From: Dmitry Dzhus Date: Mon, 25 Nov 2024 18:56:42 +0000 Subject: [PATCH 1/7] Failing test --- test/malli/core_test.cljc | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/malli/core_test.cljc b/test/malli/core_test.cljc index 8c4641381..1b4219eec 100644 --- a/test/malli/core_test.cljc +++ b/test/malli/core_test.cljc @@ -997,6 +997,18 @@ (mt/key-transformer {:decode #(-> % name (str "_key") keyword)})))) + (testing "JSON transformer decodes map schema keys" + (let [schema [:map + [:a :uuid] + [:b [:enum :x :y :z]]] + value {"a" "b699671c-d34d-b33f-1337-dbdbfd337e73" + "b" "x"} + decoded-value (m/decode schema value mt/json-transformer)] + (is (= {:a #uuid "b699671c-d34d-b33f-1337-dbdbfd337e73" + :b :x} + decoded-value)) + (is (m/validate schema decoded-value)))) + (is (= {:x 32} (m/decode [:map {:decode/string '{:enter #(update % :x inc), :leave #(update % :x (partial * 2))}} From 1abc8e3b42514313c6c4a41f255613de5fef228c Mon Sep 17 00:00:00 2001 From: Dmitry Dzhus Date: Mon, 25 Nov 2024 18:58:48 +0000 Subject: [PATCH 2/7] Direct fix --- src/malli/transform.cljc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/malli/transform.cljc b/src/malli/transform.cljc index 5cc6e7877..0dc410bdf 100644 --- a/src/malli/transform.cljc +++ b/src/malli/transform.cljc @@ -415,6 +415,8 @@ (-transform-if-valid key-schema) (-transform-map-keys)) (-transform-map-keys m/-keyword->string))))}) + (assoc :map {:compile (fn [_ _] + (-transform-map-keys -string->keyword))}) (cond-> json-vectors (assoc :vector -sequential->vector))) :encoders (-json-encoders)}))) From 7d44b1c10c1484eb0439cbf6068350c708065f17 Mon Sep 17 00:00:00 2001 From: Dmitry Dzhus Date: Mon, 25 Nov 2024 19:02:39 +0000 Subject: [PATCH 3/7] CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 92a344374..5866e876e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ Malli is in well matured [alpha](README.md#alpha). * **BREAKING**: `:gen/fmap` property requires its schema to create a generator. * previous behavior defaulted to a `nil`-returning generator, even if the schema doesn't accept `nil` * use `:gen/return nil` property to restore this behavior + * Decode map keys into keywords for `[:map` schemas in `json-transformer` [#1135](https://github.com/metosin/malli/issues/1135) * FIX: `malli.registry/{mode,type}` not respected in Babashka [#1124](https://github.com/metosin/malli/issues/1124) * Updated dependencies: From cb8e7f73106c8cfc6dad570c1d119b39b57c4fe5 Mon Sep 17 00:00:00 2001 From: Dmitry Dzhus Date: Tue, 26 Nov 2024 16:28:02 +0000 Subject: [PATCH 4/7] Unnest changelog entry --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5866e876e..c74a8f150 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,7 @@ Malli is in well matured [alpha](README.md#alpha). * **BREAKING**: `:gen/fmap` property requires its schema to create a generator. * previous behavior defaulted to a `nil`-returning generator, even if the schema doesn't accept `nil` * use `:gen/return nil` property to restore this behavior - * Decode map keys into keywords for `[:map` schemas in `json-transformer` [#1135](https://github.com/metosin/malli/issues/1135) +* **BREAKING**: Decode map keys into keywords for `[:map` schemas in `json-transformer` [#1135](https://github.com/metosin/malli/issues/1135) * FIX: `malli.registry/{mode,type}` not respected in Babashka [#1124](https://github.com/metosin/malli/issues/1124) * Updated dependencies: From 761d62f6fd66c0c52b7c3825c03186ac0665f2b1 Mon Sep 17 00:00:00 2001 From: Dmitry Dzhus Date: Tue, 26 Nov 2024 16:29:49 +0000 Subject: [PATCH 5/7] Update test --- test/malli/core_test.cljc | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/test/malli/core_test.cljc b/test/malli/core_test.cljc index 1b4219eec..a1f49b579 100644 --- a/test/malli/core_test.cljc +++ b/test/malli/core_test.cljc @@ -997,16 +997,22 @@ (mt/key-transformer {:decode #(-> % name (str "_key") keyword)})))) - (testing "JSON transformer decodes map schema keys" - (let [schema [:map - [:a :uuid] - [:b [:enum :x :y :z]]] - value {"a" "b699671c-d34d-b33f-1337-dbdbfd337e73" - "b" "x"} - decoded-value (m/decode schema value mt/json-transformer)] - (is (= {:a #uuid "b699671c-d34d-b33f-1337-dbdbfd337e73" - :b :x} - decoded-value)) + (testing "JSON transformer can decode map schema keys" + (let [schema + [:map + [:a :uuid] + [:b [:enum :x :y :z]] + ["s" :boolean [:enum :f1 :f2]]] + value + {"a" "b699671c-d34d-b33f-1337-dbdbfd337e73" + "b" "x" + "s" "f1"} + expected-decoded-value + {:a #uuid "b699671c-d34d-b33f-1337-dbdbfd337e73" + :b :x + "s" :f1} + decoded-value (m/decode schema value (mt/json-transformer {::mt/keywordize-map-keys true}))] + (is (= expected-decoded-value decoded-value)) (is (m/validate schema decoded-value)))) (is (= {:x 32} From 5b1dbaf5fdafbe5661fc311feb8cdcab9d435bc9 Mon Sep 17 00:00:00 2001 From: Dmitry Dzhus Date: Tue, 26 Nov 2024 16:32:22 +0000 Subject: [PATCH 6/7] Opt-in, do not touch non-keyword schema keys --- src/malli/transform.cljc | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/malli/transform.cljc b/src/malli/transform.cljc index 0dc410bdf..c15dee72b 100644 --- a/src/malli/transform.cljc +++ b/src/malli/transform.cljc @@ -1,6 +1,7 @@ (ns malli.transform #?(:cljs (:refer-clojure :exclude [Inst Keyword UUID])) (:require [malli.core :as m] + [malli.util :as mu] [clojure.math :as math] #?(:cljs [goog.date.UtcDateTime]) #?(:cljs [goog.date.Date])) @@ -191,8 +192,11 @@ (catch #?(:clj Exception, :cljs js/Error) _ x)) x)) -(defn -transform-map-keys [f] - #(cond->> % (map? %) (into {} (map (fn [[k v]] [(f k) v]))))) +(defn -transform-map-keys + ([f] + #(cond->> % (map? %) (into {} (map (fn [[k v]] [(f k) v]))))) + ([ks f] + #(cond->> % (map? %) (into {} (map (fn [[k v]] [(cond-> k (contains? ks k) f) v])))))) (defn -transform-if-valid [f schema] (let [validator (m/-validator schema)] @@ -403,7 +407,9 @@ (defn json-transformer ([] (json-transformer nil)) - ([{::keys [json-vectors map-of-key-decoders] :or {map-of-key-decoders (-string-decoders)}}] + ([{::keys [json-vectors + keywordize-map-keys + map-of-key-decoders] :or {map-of-key-decoders (-string-decoders)}}] (transformer {:name :json :decoders (-> (-json-decoders) @@ -415,8 +421,13 @@ (-transform-if-valid key-schema) (-transform-map-keys)) (-transform-map-keys m/-keyword->string))))}) - (assoc :map {:compile (fn [_ _] - (-transform-map-keys -string->keyword))}) + (cond-> keywordize-map-keys + (assoc :map {:compile (fn [schema _] + (let [keyword-keys (->> (mu/keys schema) + (filter keyword?) + (map name) + set)] + (-transform-map-keys keyword-keys -string->keyword)))})) (cond-> json-vectors (assoc :vector -sequential->vector))) :encoders (-json-encoders)}))) From 6a1f515f772ca23d7cc106659ade96522b2875da Mon Sep 17 00:00:00 2001 From: Dmitry Dzhus Date: Tue, 26 Nov 2024 16:30:48 +0000 Subject: [PATCH 7/7] This is no longer a breaking change --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c74a8f150..678bf4470 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,7 @@ Malli is in well matured [alpha](README.md#alpha). * **BREAKING**: `:gen/fmap` property requires its schema to create a generator. * previous behavior defaulted to a `nil`-returning generator, even if the schema doesn't accept `nil` * use `:gen/return nil` property to restore this behavior -* **BREAKING**: Decode map keys into keywords for `[:map` schemas in `json-transformer` [#1135](https://github.com/metosin/malli/issues/1135) +* Support decoding map keys into keywords for `[:map` schemas in `json-transformer` [#1135](https://github.com/metosin/malli/issues/1135) * FIX: `malli.registry/{mode,type}` not respected in Babashka [#1124](https://github.com/metosin/malli/issues/1124) * Updated dependencies: