Segment-router doesn't allow empty path-parameters

This commit is contained in:
Tommi Reiman 2018-12-10 19:04:07 +02:00
parent 147034d914
commit 492d5e2f2b
3 changed files with 21 additions and 2 deletions

View file

@ -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) ## 0.2.9 (2018-11-21)
## `reitit-spec` ## `reitit-spec`

View file

@ -40,7 +40,7 @@
(if (nil? p) (if (nil? p)
(when match (assoc match :path-params path-params)) (when match (assoc match :path-params path-params))
(or (-lookup (impl/fast-get children' p) ps 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))))))))) (if catch-all (-catch-all children' catch-all path-params p ps)))))))))
(defn insert [root path data] (defn insert [root path data]

View file

@ -79,7 +79,20 @@
(is (= ::jabba2 (matches "/abba/2"))) (is (= ::jabba2 (matches "/abba/2")))
(is (= ::doo (matches "/abba/1/doo"))) (is (= ::doo (matches "/abba/1/doo")))
(is (= ::boo (matches "/abba/1/boo"))) (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/linear-router :linear-router
r/segment-router :segment-router r/segment-router :segment-router