plumber icon indicating copy to clipboard operation
plumber copied to clipboard

Automated Swagger docs for programmatic routes

Open nuest opened this issue 7 years ago • 4 comments

I've changed my implementation from annotated routes to programatically defined one (see https://github.com/r-lib/debugme/issues/30).

Can I somehow manually provide the (parameters) information for the Swagger UI generation?

nuest avatar Dec 18 '17 13:12 nuest

Not obviously, no. I'll keep this open to consider how we could support that.

trestletech avatar Jan 10 '18 14:01 trestletech

bump

schloerke avatar Dec 07 '18 03:12 schloerke

I guess this still doesn't work? I'd like to use plumber in a package, but that's an issue:

  • when using files and annotations for defining the routes, packaging doesn't work properly (can't point to files when packaging, one should always point to objects)
  • when defining the routes programmatically, the generated openapi misses the parameters (and probably other stuff)

Wouldn't it be possible to define a routes object, which can be passed to plumber$new(), instead of reading it from a file?

kurt-o-sys avatar Apr 25 '20 14:04 kurt-o-sys

Ok, for everyone looking to do this, here is an example. We do it by passing arguments via ... to function like pr_get and pr_handle.

Here is the structure of the arguments you can use with ..., they are the one you can define in a plumber block.

params : a named list of named lists

params[[{parameter name}]] <- list(desc= {description}, type={OpenAPIType}, required={logical}, isArray={logical})

See OpenAPIType

comments : a character string

responses : a named list of named list

responses[[{response label}]] <- list(description={response description})

You could probably fill responses with a valid openapi spec and it will generate correctly. tags : a character vector

So let see an example

This

#* @get /bob
#* @param a:[int]* parameter a
#* @param b:chr parameter b
#* @tag uno
#* @tag dos
#* @response 201 Created
function(a, b) {
  ans <- list()
  ans[[b]] <- sum(as.numeric(a))
}

Can be written programmatically like this

f <- function(a, b) {
  ans <- list()
  ans[[b]] <- sum(as.numeric(a))
}
pr() %>% pr_get("/bob", f, tags = c("uno", "dos"), params = 
                  list(a = list(
                      desc = "parameter a",
                      type = "integer",
                      required = TRUE,
                      isArray = TRUE),
                    b = list(
                      desc = "parameter b",
                      type = "string",
                      required = FALSE,
                      isArray = FALSE))) %>% pr_run()

meztez avatar Oct 02 '20 04:10 meztez