Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent $run() method while plumb()ing file #574

Merged
merged 5 commits into from
Jul 3, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions R/plumber.R
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ 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)
schloerke marked this conversation as resolved.
Show resolved Hide resolved

for (i in 1:length(private$parsed)){
e <- private$parsed[i]
Expand Down Expand Up @@ -288,6 +290,10 @@ plumber <- R6Class(
debug = interactive(),
swaggerCallback = getOption('plumber.swagger.url', NULL)
) {
if (isTRUE(private$disable_run)) {
stop("Plumber router run method called from a router modifier expression (@plumber)")
schloerke marked this conversation as resolved.
Show resolved Hide resolved
}

port <- findPort(port)


Expand Down Expand Up @@ -1056,6 +1062,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")),
"Plumber router run method called")
schloerke marked this conversation as resolved.
Show resolved Hide resolved
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