rstudio / plumber

Turn your R code into a web API.
https://www.rplumber.io
Other
1.39k stars 256 forks source link

plumber 1.0.0 breaks api #726

Open kurt-o-sys opened 3 years ago

kurt-o-sys commented 3 years ago

I've got a project and I updated to the latest plumber version. I updated the plumber calls to Plumber:

Warning: `plumber` is deprecated as of plumber 1.0.0.
Please use `Plumber` instead.

All seemed fine, until I tried to start my server (startServer()):

router_balance <- Plumber$new("R/route_balance.R")
router_cost <- Plumber$new("R/route_cost.R")
router_info <- PlumberStatic$new("./public/packagename/")
router <- Plumber$new()

handle_options <- function(req, res) {
  res$setHeader("Access-Control-Allow-Methods","*")
  res$setHeader("Access-Control-Allow-Headers", req$HTTP_ACCESS_CONTROL_REQUEST_HEADERS)
  res$status <- 200
  return(list())
}

cors <- function(req, res) {
  res$setHeader("Access-Control-Allow-Origin", "*")
  if (req$REQUEST_METHOD == "OPTIONS") {
    handle_options(req, res)
  } else {
    plumber::forward()
  }
}

router$filter("cors", cors)
router$mount("/balance", router_balance)
router$mount("/cost", router_cost)
router$mount("/public", router_info)

startServer <- function(port) {
  if (missing(port))
    port <- Sys.getenv("PORT")
  if (port == "")
    port <- "8000"
  router$run(host = '0.0.0.0',
             port = strtoi(port),
             swagger = function(pr_, spec, ...) {
               spec$info$title<-"..."
               spec$info$description<-"..."
               spec$info$version<-" ..."
               spec$servers[[1]]$description<-"..."
               spec
             })
}

which results in

startServer()
Running plumber API at http://0.0.0.0:8000
Error: object of type 'symbol' is not subsettable

I say I needed to move/remove the swagger parameter. When I remove the swagger parameter (or move it to router$setApiSpec I still have the same issue. What am I missing?

meztez commented 3 years ago

you can use pr() instead of Plumber$new

What if you do not use swagger spec at all?

My guess is that there is a problem with the swagger doc as the server was able to start.

put a browser() in there and run each line to find the culprit.

meztez commented 3 years ago

@kurt-o-sys I think I've got you, your swagger functions should only have one parameter, which is the spec.

"a function that accepts the OpenAPI Specification autogenerated by plumber and returns a OpenAPI Specification formatted list object."

Let me find an example: https://www.rplumber.io/reference/pr_set_api_spec.html

In your case, your swagger provided function has two parameters, pr_ and spec

spec is left empty as it is unused, the actual spec will be mapped to pr_ when the call happens.

meztez commented 10 months ago

@kurt-o-sys Does this resolve your issue?