Skip to content

Commit 828f153

Browse files
authored
Merge pull request #110 from ambiorix-web/109-re-order-routes-correctly
fix: re-order routes correctly
2 parents 8c7072d + 600d203 commit 828f153

File tree

2 files changed

+8
-15
lines changed

2 files changed

+8
-15
lines changed

R/routing.R

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -433,29 +433,22 @@ Routing <- R6::R6Class(
433433
# we reorder the routes before launching the app
434434
# we make sure the longest patterns are checked first
435435
# this makes sure /:id/x matches BEFORE /:id does
436-
# howerver we also want to try to match extact paths
437-
# BEFORE dynamic once
436+
# however we also want to try to match exact paths
437+
# BEFORE dynamic ones
438438
# e.g. /hello should be matched before /:id
439-
# TODO https://github.com/devOpifex/ambiorix/issues/47
440439
reorder_routes = function() {
441440
indices <- seq_along(private$.routes)
442-
pats <- lapply(private$.routes, \(route) {
441+
paths <- lapply(private$.routes, function(route) {
443442
data.frame(
444-
pattern = route$route$pattern,
443+
nchar = nchar(route$route$path),
445444
dynamic = route$route$dynamic
446445
)
447446
})
448-
df <- do.call(rbind, pats)
447+
df <- do.call(rbind, paths)
449448
df$order <- seq_len(nrow(df))
450-
df$nchar <- nchar(df$pattern)
451449
df <- df[order(df$dynamic, -df$nchar), ]
452450

453-
new_routes <- as.list(c(seq_len(nrow(df))))
454-
for(i in seq_len(nrow(df))) {
455-
new_routes[[i]] <- private$.routes[[df$order[i]]]
456-
}
457-
458-
private$.routes <- rev(new_routes)
451+
private$.routes <- private$.routes[df$order]
459452
},
460453
.call = function(req){
461454

tests/testthat/test-request.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ test_that("Request", {
1313

1414
expect_error(req$set())
1515
expect_error(req$set("hello"))
16-
expect_warning(req$set("hello", "world"))
16+
expect_error(req$set("hello", "world"))
1717

1818
expect_error(req$get())
19-
expect_warning(req$get("hello"))
19+
expect_error(req$get("hello"))
2020

2121
expect_error(req$get_header())
2222
})

0 commit comments

Comments
 (0)