From 850b47134ad293f81e00296f0803db34993e01e0 Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Wed, 6 Mar 2019 19:24:16 +0200 Subject: [PATCH] Don't reorder routes with :linear-router, fixes #229 --- modules/reitit-core/java-src/reitit/Trie.java | 12 +++++++----- modules/reitit-core/src/reitit/core.cljc | 2 +- modules/reitit-core/src/reitit/trie.cljc | 12 ++++++------ test/cljc/reitit/core_test.cljc | 7 +++++++ 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/modules/reitit-core/java-src/reitit/Trie.java b/modules/reitit-core/java-src/reitit/Trie.java index 83be7bb3..7befcc3a 100644 --- a/modules/reitit-core/java-src/reitit/Trie.java +++ b/modules/reitit-core/java-src/reitit/Trie.java @@ -233,17 +233,19 @@ public class Trie { } } - public static LinearMatcher linearMatcher(List childs) { - return new LinearMatcher(childs); + public static LinearMatcher linearMatcher(List childs, boolean inOrder) { + return new LinearMatcher(childs, inOrder); } static final class LinearMatcher implements Matcher { private final Matcher[] childs; private final int size; - LinearMatcher(List childs) { + LinearMatcher(List childs, boolean inOrder) { this.childs = childs.toArray(new Matcher[0]); - Arrays.sort(this.childs, Comparator.comparing(Matcher::depth).thenComparing(Matcher::length).reversed()); + if (!inOrder) { + Arrays.sort(this.childs, Comparator.comparing(Matcher::depth).thenComparing(Matcher::length).reversed()); + } this.size = childs.size(); } @@ -286,7 +288,7 @@ public class Trie { linearMatcher( Arrays.asList( staticMatcher("login", dataMatcher(null, 1)), - staticMatcher("recovery", dataMatcher(null, 2))))))); + staticMatcher("recovery", dataMatcher(null, 2))), true))), true); System.err.println(matcher); System.out.println(lookup(matcher, "/auth/login")); System.out.println(lookup(matcher, "/auth/recovery")); diff --git a/modules/reitit-core/src/reitit/core.cljc b/modules/reitit-core/src/reitit/core.cljc index 3c16434c..4b121eda 100644 --- a/modules/reitit-core/src/reitit/core.cljc +++ b/modules/reitit-core/src/reitit/core.cljc @@ -96,7 +96,7 @@ [[] {}] compiled-routes) lookup (impl/fast-map nl) - matcher (trie/linear-matcher compiler pl) + matcher (trie/linear-matcher compiler pl true) match-by-path (trie/path-matcher matcher compiler) routes (impl/uncompile-routes compiled-routes)] ^{:type ::router} diff --git a/modules/reitit-core/src/reitit/trie.cljc b/modules/reitit-core/src/reitit/trie.cljc index edc056f1..089a7628 100644 --- a/modules/reitit-core/src/reitit/trie.cljc +++ b/modules/reitit-core/src/reitit/trie.cljc @@ -24,7 +24,7 @@ (static-matcher [this path matcher]) (wild-matcher [this key end matcher]) (catch-all-matcher [this key params data]) - (linear-matcher [this matchers]) + (linear-matcher [this matchers ordered?]) (-pretty [this matcher]) (-path-matcher [this matcher])) @@ -244,8 +244,8 @@ (view [_] [key [data]]) (depth [_] 1) (length [_])))) - (linear-matcher [_ matchers] - (let [matchers (vec (reverse (sort-by (juxt depth length) matchers))) + (linear-matcher [_ matchers ordered?] + (let [matchers (vec (if ordered? matchers (reverse (sort-by (juxt depth length) matchers)))) size (count matchers)] (reify Matcher (match [_ i max path] @@ -275,8 +275,8 @@ (Trie/wildMatcher key (if end (Character. end)) matcher)) (catch-all-matcher [_ key params data] (Trie/catchAllMatcher key params data)) - (linear-matcher [_ matchers] - (Trie/linearMatcher matchers)) + (linear-matcher [_ matchers ordered?] + (Trie/linearMatcher matchers ordered?)) (-pretty [_ matcher] (-> matcher str read-string eval)) (-path-matcher [_ matcher] @@ -328,7 +328,7 @@ (wild-matcher compiler pv (ffirst ends) (compile c compiler (conj cp pv))))))) (into (for [[p c] catch-all] (catch-all-matcher compiler (:value p) params (:data c)))))] (cond - (> (count matchers) 1) (linear-matcher compiler matchers) + (> (count matchers) 1) (linear-matcher compiler matchers false) (= (count matchers) 1) (first matchers) :else (data-matcher compiler {} nil))))) diff --git a/test/cljc/reitit/core_test.cljc b/test/cljc/reitit/core_test.cljc index 5d2e372d..cc91149d 100644 --- a/test/cljc/reitit/core_test.cljc +++ b/test/cljc/reitit/core_test.cljc @@ -382,3 +382,10 @@ (let [router (r/router ["/endpoint" (->Named :kikka)])] (is (= [["/endpoint" {:name :kikka}]] (r/routes router))))) + +(deftest routing-order-test-229 + (let [router (r/router + [["/" :root] + ["/" {:name :create :method :post}]] + {:conflicts nil})] + (is (= :root (-> (r/match-by-path router "/") :data :name))))) \ No newline at end of file