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

Fix #692, set openapi spec with file path #696

Closed
wants to merge 4 commits into from
Closed
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
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
plumber 1.0.0.9999 Development version
--------------------------------------------------------------------------------

### New features

* OpenAPI Specification can be set using a file path.

### Bug fixes

* When calling `Plumber$handle()` and defining a new `PlumberEndpoint`, `...` will be checked for invalid names #677
Expand Down
11 changes: 11 additions & 0 deletions R/plumber.R
Original file line number Diff line number Diff line change
Expand Up @@ -887,9 +887,20 @@ Plumber <- R6Class(
#' @param api This can be
#' * an OpenAPI Specification formatted list object
#' * a function that accepts the OpenAPI Specification autogenerated by `plumber` and returns a OpenAPI Specification formatted list object.
#' * a path to an OpenAPI Specification
#'
#' The value returned will not be validated for OAS compatibility.
setApiSpec = function(api = NULL) {
if (is.character(api) && length(api) == 1 && file.exists(api)) {
if (tools::file_ext(api) %in% c("yaml", "yml")) {
if (!requireNamespace("yaml", quietly = TRUE)) {
stop("yaml must be installed to read yaml format")
}
api <- yaml::read_yaml(api, eval.expr = FALSE)
} else {
api <- jsonlite::read_json(api, simplifyVector = TRUE)
}
}
api_fun <-
if (is.null(api)) {
identity
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/files/openapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
servers:
- url: ''
openapi: 3.0.3
info:
description: API Description
title: Foo
version: 1.0.0
paths:
/health-check:
get:
summary: ' Determine if the API is running and listening as expected'
responses:
default:
description: Default response.
parameters: []
5 changes: 5 additions & 0 deletions tests/testthat/test-zzz-openapi.R
Original file line number Diff line number Diff line change
Expand Up @@ -327,3 +327,8 @@ test_that("no params plumber router still produces spec when there is a func par
spec <- pr$getApiSpec()
expect_equal(spec$paths$`/sum`$get$parameters[[1]]$name, "num")
})

test_that("Api spec can be set using a file path", {
pr <- pr() %>% pr_set_api_spec(test_path("files/openapi.yaml"))
expect_equal(pr$getApiSpec()$paths$`/health-check`$get$summary, " Determine if the API is running and listening as expected")
})