From 492d5e2f2ba58497b8d5c756840105c58338d6ed Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Mon, 10 Dec 2018 19:04:07 +0200 Subject: [PATCH] Segment-router doesn't allow empty path-parameters --- CHANGELOG.md | 6 ++++++ modules/reitit-core/src/reitit/segment.cljc | 2 +- test/cljc/reitit/core_test.cljc | 15 ++++++++++++++- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 011aefdf..004c7f73 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## UNRELEASED + +## `reitit-core` + +* `segment-router` doesn't accept empty segments as path-parameters, fixes [#181](https://github.com/metosin/reitit/issues/181). + ## 0.2.9 (2018-11-21) ## `reitit-spec` diff --git a/modules/reitit-core/src/reitit/segment.cljc b/modules/reitit-core/src/reitit/segment.cljc index 0675a8c4..c96de655 100644 --- a/modules/reitit-core/src/reitit/segment.cljc +++ b/modules/reitit-core/src/reitit/segment.cljc @@ -40,7 +40,7 @@ (if (nil? p) (when match (assoc match :path-params path-params)) (or (-lookup (impl/fast-get children' p) ps path-params) - (if wilds? (some #(-lookup (impl/fast-get children' %) ps (assoc path-params % p)) wilds)) + (if (and wilds? (not (str/blank? p))) (some #(-lookup (impl/fast-get children' %) ps (assoc path-params % p)) wilds)) (if catch-all (-catch-all children' catch-all path-params p ps))))))))) (defn insert [root path data] diff --git a/test/cljc/reitit/core_test.cljc b/test/cljc/reitit/core_test.cljc index 8a7e3482..86bcddfd 100644 --- a/test/cljc/reitit/core_test.cljc +++ b/test/cljc/reitit/core_test.cljc @@ -79,7 +79,20 @@ (is (= ::jabba2 (matches "/abba/2"))) (is (= ::doo (matches "/abba/1/doo"))) (is (= ::boo (matches "/abba/1/boo"))) - (is (= ::wild (matches "/olipa/kerran/avaruus/vaan/ei/toista/kertaa")))))) + (is (= ::wild (matches "/olipa/kerran/avaruus/vaan/ei/toista/kertaa"))))) + + (testing "empty path segments" + (let [router (r/router + [["/items" ::list] + ["/items/:id" ::item] + ["/items/:id/:side" ::deep]] + {:router r}) + matches #(-> router (r/match-by-path %) :data :name)] + (is (= ::list (matches "/items"))) + (is (= nil (matches "/items/"))) + (is (= ::item (matches "/items/1"))) + (is (= ::deep (matches "/items/1/2"))) + (is (= nil (matches "/items//2")))))) r/linear-router :linear-router r/segment-router :segment-router