From 49e8d887da0059c2617102511cc2ee0dac5eeca3 Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Sun, 30 Jun 2024 17:55:47 +0300 Subject: [PATCH] fixes #679 --- modules/reitit-core/src/reitit/impl.cljc | 18 +++++++++++++++++- test/cljc/reitit/core_test.cljc | 8 ++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/modules/reitit-core/src/reitit/impl.cljc b/modules/reitit-core/src/reitit/impl.cljc index e7374548..a7e318c7 100644 --- a/modules/reitit-core/src/reitit/impl.cljc +++ b/modules/reitit-core/src/reitit/impl.cljc @@ -34,12 +34,28 @@ l m))] (-path-vals [] [] m))) +(defn -copy-meta [to from] + (letfn [(-with-meta [x m] + (try (with-meta x m) (catch #?(:clj Exception, :cljs js/Error) _))) + (-copy [l p m] + (reduce-kv + (fn [l k v] + (let [p' (conj p k) + m' (meta (get-in from p'))] + (cond + (and m' (not (var? v))) (update-in l p' -with-meta m') + (and (map? v) (not (record? v)) (seq v)) (-copy l p' v) + :else l))) + l m))] + (-copy to [] to))) + (defn -assoc-in-path-vals [c] (reduce (partial apply assoc-in) {} c)) (defn path-update [m path-map] (-> (-path-vals m path-map) - (-assoc-in-path-vals))) + (-assoc-in-path-vals) + (-copy-meta m))) (defn accumulator? [x] (-> x meta ::accumulator)) diff --git a/test/cljc/reitit/core_test.cljc b/test/cljc/reitit/core_test.cljc index 8c9b31c5..d60f2f41 100644 --- a/test/cljc/reitit/core_test.cljc +++ b/test/cljc/reitit/core_test.cljc @@ -442,3 +442,11 @@ (deftest routing-bug-test-538 (let [router (r/router [["/:a"] ["/:b"]] {:conflicts nil})] (is (nil? (r/match-by-path router ""))))) + +(deftest metadata-regression-679 + (is (= ["/context/leaf" {:roles {:foo true}}] + (-> ["/context" {:roles {:foo false :bar true}} + ["/leaf" {:roles ^:replace {:foo true}}]] + (r/router) + (r/routes) + (first)))))