Skip to content

Commit

Permalink
feat: look inside :ref when searching for default for :map child
Browse files Browse the repository at this point in the history
When a default value gets produced up via the normal transformer
pipeline, the -transform method of -ref-schema will delegate to the
child schema. However, when we're building up the default values for a
:map in add-defaults, we're not using the transformer pipeline, but
calling get-default manually. Thus, we need to manually deref.

fixes #1145
  • Loading branch information
opqdonut committed Dec 10, 2024
1 parent 87baaa5 commit 38168dd
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/malli/transform.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,9 @@
(when (or (not optional) add-optional-keys)
(let [e (find p key)]
(when-some [f (if e (constantly (val e))
(get-default v))]
(or (get-default v)
(when (m/-ref-schema? v)
(get-default (m/-deref v)))))]
[k (fn [] (default-fn schema (f)))])))))
(m/children schema))]
(when (seq defaults)
Expand Down
6 changes: 2 additions & 4 deletions test/malli/transform_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1042,11 +1042,9 @@
(is (= 7 (m/decode [:ref "bing"] nil opts transformer)))
(is (= [7] (m/decode [:vector [:ref "bing"]] [nil] opts transformer)))
(is (= {:a 7} (m/decode [:map [:a [:ref "bing"]]] {:a nil} opts transformer)))
;; TODO fails
#_(is (= {:a 7} (m/decode [:map [:a [:ref "bing"]]] {} opts transformer)))
(is (= {:a 7} (m/decode [:map [:a [:ref "bing"]]] {} opts transformer)))
(is (= {:a 8} (m/decode [:map [:a [:ref {:default 8} "bing"]]] {:a nil} opts transformer)))
;; TODO fails
#_(is (= {:a 8} (m/decode [:map [:a [:ref {:default 8} "bing"]]] {} opts transformer))))))
(is (= {:a 8} (m/decode [:map [:a [:ref {:default 8} "bing"]]] {} opts transformer))))))

(deftest type-properties-based-transformations
(is (= 12 (m/decode malli.core-test/Over6 "12" mt/string-transformer))))
Expand Down

0 comments on commit 38168dd

Please sign in to comment.