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

Add more metadata to PlumberEndpoint #782

Merged
merged 6 commits into from
Mar 22, 2021
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.90001
Version: 1.0.0.90002
Roxygen: list(markdown = TRUE)
Authors@R: c(
person("Barret", "Schloerke", role = c("cre", "aut"), email = "barret@rstudio.com"),
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ plumber 1.0.0.9999 Development version

* To update a `PlumberEndpoint` path after initialization, call the new `PlumberEndpoint$setPath(path)`. This will update internal path matching meta data. (Active bindings were not used to avoid breaking changes.) (@blairj09 #770)

* `PlumberStep` (and `PlumberEndpoint` and `PlumberFilter`) received a new field `$srcref` and method `$getFunc()`. `$srcref` will contain the corresponding `srcref` information from original source file. `$getFunc()` will return the evaluated function. (#782)

### Bug fixes

* Fixed bug where `httpuv` would return a status of `500` with body `An exception occurred` if no headers were set on the response object. (#745)
Expand Down
9 changes: 6 additions & 3 deletions R/plumb-block.R
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ plumbBlock <- function(lineNum, file, envir = parent.frame()){
#' Evaluate and activate a "block" of code found in a plumber API file.
#' @noRd
evaluateBlock <- function(srcref, file, expr, envir, addEndpoint, addFilter, pr) {
lineNum <- srcref[1] - 1
lines <- srcref[c(1,3)]
lineNum <- lines[1] - 1

block <- plumbBlock(lineNum, file, envir)

Expand All @@ -278,7 +279,8 @@ evaluateBlock <- function(srcref, file, expr, envir, addEndpoint, addFilter, pr)
envir = envir,
serializer = block$serializer,
parsers = block$parsers,
lines = srcref,
lines = lines,
srcref = srcref,
params = block$params,
comments = block$comments,
responses = block$responses,
Expand All @@ -288,7 +290,8 @@ evaluateBlock <- function(srcref, file, expr, envir, addEndpoint, addFilter, pr)
addEndpoint(ep, block$preempt)
})
} else if (!is.null(block$filter)){
filter <- PlumberFilter$new(block$filter, expr, envir, block$serializer, srcref)
filter <- PlumberFilter$new(block$filter, expr, envir, block$serializer,
lines = lines, srcref = srcref)
addFilter(filter)

} else if (!is.null(block$assets)){
Expand Down
23 changes: 20 additions & 3 deletions R/plumber-step.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ PlumberStep <- R6Class(
"PlumberStep",
inherit=Hookable,
public = list(
#' @field srcref from step block
srcref = NULL,
#' @field lines lines from step block
lines = NA,
#' @field serializer step serializer function
Expand All @@ -36,8 +38,9 @@ PlumberStep <- R6Class(
#' @param envir step environment
#' @param lines step block
#' @param serializer step serializer
#' @param srcref `srcref` attribute from block
#' @return A new `PlumberStep` object
initialize = function(expr, envir, lines, serializer){
initialize = function(expr, envir, lines, serializer, srcref){
private$expr <- expr
if (is.expression(expr)) {
private$func <- eval(expr, envir)
Expand All @@ -47,6 +50,9 @@ PlumberStep <- R6Class(
throw_if_func_is_not_a_function(private$func)
private$envir <- envir

if (!missing(srcref)) {
self$srcref <- srcref
}
if (!missing(lines)){
self$lines <- lines
}
Expand Down Expand Up @@ -211,13 +217,14 @@ PlumberEndpoint <- R6Class(
#' @param envir Endpoint environment
#' @param serializer Endpoint serializer. Ex: [serializer_json()]
#' @template pr_setParsers__parsers
#' @param srcref `srcref` attribute from block
#' @param lines Endpoint block
#' @param params Endpoint params
#' @param comments,responses,tags Values to be used within the OpenAPI Spec
#' @details Parameters values are obtained from parsing blocks of lines in a plumber file.
#' They can also be provided manually for historical reasons.
#' @return A new `PlumberEndpoint` object
initialize = function(verbs, path, expr, envir, serializer, parsers, lines, params, comments, responses, tags) {
initialize = function(verbs, path, expr, envir, serializer, parsers, lines, params, comments, responses, tags, srcref) {

self$verbs <- verbs

Expand All @@ -239,6 +246,9 @@ PlumberEndpoint <- R6Class(
if (!missing(parsers) && !is.null(parsers)) {
self$parsers <- make_parser(parsers)
}
if (!missing(srcref)) {
self$srcref <- srcref
}
if (!missing(lines)){
self$lines <- lines
}
Expand All @@ -263,6 +273,10 @@ PlumberEndpoint <- R6Class(
getPathParams = function(path){
extractPathParams(private$regex, path)
},
#' @description retrieve endpoint function
getFunc = function() {
private$func
},
#' @description retrieve endpoint expression parameters
getFuncParams = function() {
getArgsMetadata(private$func)
Expand Down Expand Up @@ -300,7 +314,7 @@ PlumberFilter <- R6Class(
inherit = PlumberStep,
public = list(
name = NA,
initialize = function(name, expr, envir, serializer, lines){
initialize = function(name, expr, envir, serializer, lines, srcref){
self$name <- name
private$expr <- expr
if (is.expression(expr)){
Expand All @@ -314,6 +328,9 @@ PlumberFilter <- R6Class(
if (!missing(serializer)){
self_set_serializer(self, serializer)
}
if (!missing(srcref)) {
self$srcref <- srcref
}
if (!missing(lines)){
self$lines <- lines
}
Expand Down
4 changes: 2 additions & 2 deletions R/plumber.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Plumber <- R6Class(

# Add in the initial filters
for (fn in names(filters)){
fil <- PlumberFilter$new(fn, filters[[fn]], private$envir, private$default_serializer, NULL)
fil <- PlumberFilter$new(fn, filters[[fn]], private$envir, private$default_serializer, NULL, NULL)
private$filts <- c(private$filts, fil)
}

Expand All @@ -122,7 +122,7 @@ Plumber <- R6Class(
for (i in seq_len(length(private$parsed))) {
e <- private$parsed[i]

srcref <- attr(e, "srcref")[[1]][c(1,3)]
srcref <- attr(e, "srcref")[[1]]

evaluateBlock(srcref, private$lines, e, private$envir, private$addEndpointInternal,
private$addFilterInternal, self)
Expand Down
16 changes: 15 additions & 1 deletion man/PlumberEndpoint.Rd

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

6 changes: 5 additions & 1 deletion man/PlumberStep.Rd

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

12 changes: 12 additions & 0 deletions tests/testthat/test-parse-block.R
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,16 @@ test_that("block respect original order of lines for comments, tags and response
expect_equal(b$responses, list(`200`=list(description="ok"), `404` = list(description="not ok")))
})

test_that("srcref values are set while plumbing from a file", {

root <- plumb_api("plumber", "01-append")
endpt <- root$endpoints[[1]][[1]]
expect_s3_class(endpt$srcref, "srcref")

root_with_no_srcref <- pr() %>% pr_get("/", force)
endpt_with_no_srcref <- root_with_no_srcref$endpoints[[1]][[1]]
expect_equal(endpt_with_no_srcref$srcref, NULL)
})


# TODO: more testing around filter, assets, endpoint, etc.