diff --git a/modules/openapi-generator/src/main/resources/r/api.mustache b/modules/openapi-generator/src/main/resources/r/api.mustache index 460a92365a0..9606add1c3b 100644 --- a/modules/openapi-generator/src/main/resources/r/api.mustache +++ b/modules/openapi-generator/src/main/resources/r/api.mustache @@ -385,15 +385,62 @@ {{#isExplode}} # explore for (query_item in `{{{paramName}}}`) { + {{#items}} + {{#isEnum}} + # validate enum values + if (!(query_item %in% c({{#_enum}}"{{{.}}}"{{^-last}}, {{/-last}}{{/_enum}}))) { + {{#useDefaultExceptionHandling}} + stop("Invalid value for {{{paramName}}} when calling {{classname}}${{operationId}}. Must be {{_enum}}.") + {{/useDefaultExceptionHandling}} + {{#useRlangExceptionHandling}} + rlang::abort(message = "Invalid value for `{{paramName}}` when calling {{classname}}${{operationId}}. Must be {{_enum}}.", + .subclass = "ApiException", + ApiException = ApiException$new(status = 0, + reason = "Invalid value for `{{paramName}}` when calling {{classname}}${{operationId}}. Must be {{_enum}}.")) + {{/useRlangExceptionHandling}} + } + {{/isEnum}} + {{/items}} query_params[["{{{baseName}}}"]] <- c(query_params[["{{{baseName}}}"]], list(`{{{baseName}}}` = query_item)) } {{/isExplode}} {{^isExplode}} # no explore + {{#items}} + {{#isEnum}} + # validate enum values + for (query_item in `{{{paramName}}}`) { + if (!(query_item %in% c({{#_enum}}"{{{.}}}"{{^-last}}, {{/-last}}{{/_enum}}))) { + {{#useDefaultExceptionHandling}} + stop("Invalid value for {{{paramName}}} when calling {{classname}}${{operationId}}. Must be {{_enum}}.") + {{/useDefaultExceptionHandling}} + {{#useRlangExceptionHandling}} + rlang::abort(message = "Invalid value for `{{paramName}}` when calling {{classname}}${{operationId}}. Must be {{_enum}}.", + .subclass = "ApiException", + ApiException = ApiException$new(status = 0, + reason = "Invalid value for `{{paramName}}` when calling {{classname}}${{operationId}}. Must be {{_enum}}.")) + {{/useRlangExceptionHandling}} + } + } + {{/isEnum}} + {{/items}} query_params[["{{{baseName}}}"]] <- I(paste(lapply(`{{{paramName}}}`, URLencode, reserved = TRUE), collapse = ",")) {{/isExplode}} {{/isArray}} {{^isArray}} + {{#isEnum}} + if (!(`{{paramName}}` %in% c({{#_enum}}"{{{.}}}"{{^-last}}, {{/-last}}{{/_enum}}))) { + {{#useDefaultExceptionHandling}} + stop("Invalid value for {{{paramName}}} when calling {{classname}}${{operationId}}. Must be {{_enum}}.") + {{/useDefaultExceptionHandling}} + {{#useRlangExceptionHandling}} + rlang::abort(message = "Invalid value for `{{paramName}}` when calling {{classname}}${{operationId}}. Must be {{_enum}}.", + .subclass = "ApiException", + ApiException = ApiException$new(status = 0, + reason = "Invalid value for `{{paramName}}` when calling {{classname}}${{operationId}}. Must be {{_enum}}.")) + {{/useRlangExceptionHandling}} + } + {{/isEnum}} query_params[["{{baseName}}"]] <- `{{paramName}}` {{/isArray}} diff --git a/samples/client/petstore/R-httr2-wrapper/R/pet_api.R b/samples/client/petstore/R-httr2-wrapper/R/pet_api.R index ef23b725e3d..9308019bb17 100644 --- a/samples/client/petstore/R-httr2-wrapper/R/pet_api.R +++ b/samples/client/petstore/R-httr2-wrapper/R/pet_api.R @@ -943,6 +943,13 @@ PetApi <- R6::R6Class( # explore for (query_item in `status`) { + # validate enum values + if (!(query_item %in% c("available", "pending", "sold"))) { + rlang::abort(message = "Invalid value for `status` when calling PetApi$find_pets_by_status. Must be [available, pending, sold].", + .subclass = "ApiException", + ApiException = ApiException$new(status = 0, + reason = "Invalid value for `status` when calling PetApi$find_pets_by_status. Must be [available, pending, sold].")) + } query_params[["status"]] <- c(query_params[["status"]], list(`status` = query_item)) } diff --git a/samples/client/petstore/R-httr2-wrapper/tests/testthat/test_petstore.R b/samples/client/petstore/R-httr2-wrapper/tests/testthat/test_petstore.R index 2fe987360bc..9758fd3fb3c 100644 --- a/samples/client/petstore/R-httr2-wrapper/tests/testthat/test_petstore.R +++ b/samples/client/petstore/R-httr2-wrapper/tests/testthat/test_petstore.R @@ -235,6 +235,16 @@ test_that("GetPetById with data_file", { }) test_that("find_pets_by_status", { + # input invalid + var_status <- c("something") # array[character] | Tags to filter by + result <- tryCatch(pet_api$find_pets_by_status(var_status), + ApiException = function(ex) ex + ) + + expect_equal(result$ApiException$reason, "Invalid value for `status` when calling PetApi$find_pets_by_status. Must be [available, pending, sold].") +}) + +test_that("find_pets_by_tags", { pet_tag_test <- Pet$new("name_test", photoUrls = list("photo_test", "second test"), category = Category$new(id = 4455, name = "test_cat"), diff --git a/samples/client/petstore/R-httr2/R/pet_api.R b/samples/client/petstore/R-httr2/R/pet_api.R index 891f456f355..c040ca153e7 100644 --- a/samples/client/petstore/R-httr2/R/pet_api.R +++ b/samples/client/petstore/R-httr2/R/pet_api.R @@ -943,6 +943,13 @@ PetApi <- R6::R6Class( # explore for (query_item in `status`) { + # validate enum values + if (!(query_item %in% c("available", "pending", "sold"))) { + rlang::abort(message = "Invalid value for `status` when calling PetApi$find_pets_by_status. Must be [available, pending, sold].", + .subclass = "ApiException", + ApiException = ApiException$new(status = 0, + reason = "Invalid value for `status` when calling PetApi$find_pets_by_status. Must be [available, pending, sold].")) + } query_params[["status"]] <- c(query_params[["status"]], list(`status` = query_item)) } diff --git a/samples/client/petstore/R/R/pet_api.R b/samples/client/petstore/R/R/pet_api.R index df3e541a33d..c3d7f8f1d38 100644 --- a/samples/client/petstore/R/R/pet_api.R +++ b/samples/client/petstore/R/R/pet_api.R @@ -943,6 +943,13 @@ PetApi <- R6::R6Class( # explore for (query_item in `status`) { + # validate enum values + if (!(query_item %in% c("available", "pending", "sold"))) { + rlang::abort(message = "Invalid value for `status` when calling PetApi$FindPetsByStatus. Must be [available, pending, sold].", + .subclass = "ApiException", + ApiException = ApiException$new(status = 0, + reason = "Invalid value for `status` when calling PetApi$FindPetsByStatus. Must be [available, pending, sold].")) + } query_params[["status"]] <- c(query_params[["status"]], list(`status` = query_item)) }