From e5e4e8fc079390d47ae333f951cf58e744846741 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Thu, 28 Jul 2022 17:06:52 +0800 Subject: [PATCH] throw exception if username, password is not set in http auth in r client (#13035) --- .../src/main/resources/r/api.mustache | 11 +++++++++++ samples/client/petstore/R/R/pet_api.R | 6 ++++++ .../client/petstore/R/tests/testthat/test_petstore.R | 5 +++-- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/r/api.mustache b/modules/openapi-generator/src/main/resources/r/api.mustache index 48c8aea4b7fe..a4cd30b7641c 100644 --- a/modules/openapi-generator/src/main/resources/r/api.mustache +++ b/modules/openapi-generator/src/main/resources/r/api.mustache @@ -308,6 +308,17 @@ {{#isBasic}} {{#isBasicBasic}} # HTTP basic auth + if (is.null(self$api_client$username) || is.null(self$api_client$password)) { + {{#useDefaultExceptionHandling}} + stop("username, password in `api_client` must be set for authentication in the endpoint `{{{operationId}}}`.") + {{/useDefaultExceptionHandling}} + {{#useRlangExceptionHandling}} + rlang::abort(message = "username, password in `api_client` must be set for authentication in the endpoint `{{{operationId}}}`.", + .subclass = "ApiException", + ApiException = ApiException$new(status = 0, + reason = "username, password in `api_client` must be set for authentication in the endpoint `{{{operationId}}}`.")) + {{/useRlangExceptionHandling}} + } header_params["Authorization"] <- paste("Basic", base64enc::base64encode(charToRaw(paste(self$api_client$username, self$api_client$password, sep = ":")))) {{/isBasicBasic}} {{#isBasicBearer}} diff --git a/samples/client/petstore/R/R/pet_api.R b/samples/client/petstore/R/R/pet_api.R index a26ff9ad251a..88b3b70342f7 100644 --- a/samples/client/petstore/R/R/pet_api.R +++ b/samples/client/petstore/R/R/pet_api.R @@ -586,6 +586,12 @@ PetApi <- R6::R6Class( local_var_url_path <- "/pet" # HTTP basic auth + if (is.null(self$api_client$username) || is.null(self$api_client$password)) { + rlang::abort(message = "username, password in `api_client` must be set for authentication in the endpoint `AddPet`.", + .subclass = "ApiException", + ApiException = ApiException$new(status = 0, + reason = "username, password in `api_client` must be set for authentication in the endpoint `AddPet`.")) + } header_params["Authorization"] <- paste("Basic", base64enc::base64encode(charToRaw(paste(self$api_client$username, self$api_client$password, sep = ":")))) # The Accept request HTTP header diff --git a/samples/client/petstore/R/tests/testthat/test_petstore.R b/samples/client/petstore/R/tests/testthat/test_petstore.R index e9d6090e393c..9a0d059f56d1 100644 --- a/samples/client/petstore/R/tests/testthat/test_petstore.R +++ b/samples/client/petstore/R/tests/testthat/test_petstore.R @@ -11,8 +11,9 @@ pet <- Pet$new("name_test", ), status = "available" ) -pet_api$api_client$username <- "username123" -pet_api$api_client$password <- "password123" + +pet_api$api_client$username <- "" +pet_api$api_client$password <- "" result <- pet_api$AddPet(pet) test_that("Test toJSONString", {