Skip to content

Commit

Permalink
Prevent $run() method while plumb()ing file (#574)
Browse files Browse the repository at this point in the history
Co-authored-by: Bruno Tremblay <meztez@neoxone.com>
Co-authored-by: Barret Schloerke <barret@rstudio.com>
  • Loading branch information
3 people authored Jul 3, 2020
1 parent 8c6a80a commit 9fa934a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
9 changes: 9 additions & 0 deletions R/plumber.R
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ plumber <- R6Class(
if (!is.null(file)){
private$lines <- readUTF8(file)
private$parsed <- parseUTF8(file)
private$disable_run <- TRUE
on.exit({
private$disable_run <- FALSE
}, add = TRUE)

for (i in 1:length(private$parsed)){
e <- private$parsed[i]
Expand Down Expand Up @@ -288,6 +292,10 @@ plumber <- R6Class(
debug = interactive(),
swaggerCallback = getOption('plumber.swagger.url', NULL)
) {
if (isTRUE(private$disable_run)) {
stop("Plumber router `$run()` method should not be called while `plumb()`ing a file")
}

port <- findPort(port)


Expand Down Expand Up @@ -1056,6 +1064,7 @@ plumber <- R6Class(
lines = NULL, # The lines constituting the API
parsed = NULL, # The parsed representation of the API
globalSettings = list(info=list()), # Global settings for this API. Primarily used for OpenAPI Specification.
disable_run = NULL, # Disable run method during parsing of the Plumber file

errorHandler = NULL,
notFoundHandler = NULL,
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/files/router-modifier-run.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#* @plumber
function(pr){
pr$run()
}
5 changes: 4 additions & 1 deletion tests/testthat/test-router-modifier.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
context("Router modifier (@plumber tag)")

test_that("router modifier tag works correctly", {
test_that("router modifier works, run does nothing", {
expect_error(
plumber$new(test_path("files/router-modifier-run.R")),
"method should not be called while")
pr <- plumber$new(test_path("files/router-modifier.R"))
expect_equal(class(pr$routes[[1]])[1], "PlumberEndpoint")
expect_equal(names(pr$routes), "avatartare")
Expand Down

0 comments on commit 9fa934a

Please sign in to comment.