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

Provide arg names to all PlumberEndpoint variables in pr$handle() #677

Merged
merged 6 commits into from
Sep 25, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Encoding: UTF-8
Package: plumber
Type: Package
Title: An API Generator for R
Version: 1.0.0
Version: 1.0.0.9999
Roxygen: list(markdown = TRUE)
Authors@R: c(
person("Barret", "Schloerke", role = c("cre", "aut"), email = "barret@rstudio.com"),
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
plumber 1.0.0.9999 Development version
--------------------------------------------------------------------------------

### Bug fixes

* When calling `Plumber$handle()` and defining a new `PlumberEndpoint`, `...` will be checked for invalid names #677

plumber 1.0.0
--------------------------------------------------------------------------------

Expand Down
16 changes: 13 additions & 3 deletions R/plumber.R
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ Plumber <- R6Class(
#' @param serializer a serializer function.
#' @param parsers a named list of parsers.
#' @param endpoint a `PlumberEndpoint` object.
#' @param ... additional arguments for `PlumberEndpoint` creation
#' @param ... additional arguments for [PlumberEndpoint] `new` method (namely `lines`, `params`, `comments`, `responses` and `tags`. Excludes `envir`).
#' @examples
#' \dontrun{
#' pr <- pr()
Expand All @@ -342,7 +342,7 @@ Plumber <- R6Class(
#' }
handle = function(methods, path, handler, preempt, serializer, parsers, endpoint, ...) {
epdef <- !missing(methods) || !missing(path) || !missing(handler) || !missing(serializer) || !missing(parsers)
if (!missing(endpoint) && epdef){
if (!missing(endpoint) && epdef) {
stop("You must provide either the components for an endpoint (handler and serializer) OR provide the endpoint yourself. You cannot do both.")
}

Expand All @@ -353,8 +353,18 @@ Plumber <- R6Class(
if (missing(parsers)) {
parsers <- private$parsers
}
forbid <- c("verbs", "expr", "envir")
forbid_check <- forbid %in% names(list(...))
if (any(forbid_check)) {
stop(paste0("`", forbid[forbid_check], "`", collapse = ", "), " can not be supplied to `pr$handle()` method.")
}

endpoint <- PlumberEndpoint$new(methods, path, handler, private$envir, serializer, parsers, ...)
endpoint <- PlumberEndpoint$new(verbs = methods,
path = path,
expr = handler,
envir = private$envir,
serializer = serializer,
parsers = parsers, ...)
}
private$addEndpointInternal(endpoint, preempt)
},
Expand Down
2 changes: 1 addition & 1 deletion man/Plumber.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions tests/testthat/test-plumber.R
Original file line number Diff line number Diff line change
Expand Up @@ -523,3 +523,13 @@ test_that("routes that don't start with a slash are prepended with a slash", {
expect_equal(length(pr$endpoints[[1]]), 1L)
expect_equal(pr$endpoints[[1]][[1]]$path, "/nested/path/here")
})

test_that("handle method rejects forbidden arguments", {
pr <- pr()
expect_error(pr$handle("GET", "nested/path/here", function(){}, envir = new.env()),
"can not be supplied to", )
expect_error(pr$handle("GET", "nested/path/here", function(){}, verbs = "GET"),
"can not be supplied to")
expect_error(pr$handle("GET", "nested/path/here", function(){}, expr = function(){}),
"can not be supplied to")
})