Skip to content

Commit 37fb856

Browse files
authored
add validation for query enum parameters in r client (#13415)
1 parent 7ff47dc commit 37fb856

File tree

5 files changed

+78
-0
lines changed

5 files changed

+78
-0
lines changed

modules/openapi-generator/src/main/resources/r/api.mustache

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,15 +385,62 @@
385385
{{#isExplode}}
386386
# explore
387387
for (query_item in `{{{paramName}}}`) {
388+
{{#items}}
389+
{{#isEnum}}
390+
# validate enum values
391+
if (!(query_item %in% c({{#_enum}}"{{{.}}}"{{^-last}}, {{/-last}}{{/_enum}}))) {
392+
{{#useDefaultExceptionHandling}}
393+
stop("Invalid value for {{{paramName}}} when calling {{classname}}${{operationId}}. Must be {{_enum}}.")
394+
{{/useDefaultExceptionHandling}}
395+
{{#useRlangExceptionHandling}}
396+
rlang::abort(message = "Invalid value for `{{paramName}}` when calling {{classname}}${{operationId}}. Must be {{_enum}}.",
397+
.subclass = "ApiException",
398+
ApiException = ApiException$new(status = 0,
399+
reason = "Invalid value for `{{paramName}}` when calling {{classname}}${{operationId}}. Must be {{_enum}}."))
400+
{{/useRlangExceptionHandling}}
401+
}
402+
{{/isEnum}}
403+
{{/items}}
388404
query_params[["{{{baseName}}}"]] <- c(query_params[["{{{baseName}}}"]], list(`{{{baseName}}}` = query_item))
389405
}
390406
{{/isExplode}}
391407
{{^isExplode}}
392408
# no explore
409+
{{#items}}
410+
{{#isEnum}}
411+
# validate enum values
412+
for (query_item in `{{{paramName}}}`) {
413+
if (!(query_item %in% c({{#_enum}}"{{{.}}}"{{^-last}}, {{/-last}}{{/_enum}}))) {
414+
{{#useDefaultExceptionHandling}}
415+
stop("Invalid value for {{{paramName}}} when calling {{classname}}${{operationId}}. Must be {{_enum}}.")
416+
{{/useDefaultExceptionHandling}}
417+
{{#useRlangExceptionHandling}}
418+
rlang::abort(message = "Invalid value for `{{paramName}}` when calling {{classname}}${{operationId}}. Must be {{_enum}}.",
419+
.subclass = "ApiException",
420+
ApiException = ApiException$new(status = 0,
421+
reason = "Invalid value for `{{paramName}}` when calling {{classname}}${{operationId}}. Must be {{_enum}}."))
422+
{{/useRlangExceptionHandling}}
423+
}
424+
}
425+
{{/isEnum}}
426+
{{/items}}
393427
query_params[["{{{baseName}}}"]] <- I(paste(lapply(`{{{paramName}}}`, URLencode, reserved = TRUE), collapse = ","))
394428
{{/isExplode}}
395429
{{/isArray}}
396430
{{^isArray}}
431+
{{#isEnum}}
432+
if (!(`{{paramName}}` %in% c({{#_enum}}"{{{.}}}"{{^-last}}, {{/-last}}{{/_enum}}))) {
433+
{{#useDefaultExceptionHandling}}
434+
stop("Invalid value for {{{paramName}}} when calling {{classname}}${{operationId}}. Must be {{_enum}}.")
435+
{{/useDefaultExceptionHandling}}
436+
{{#useRlangExceptionHandling}}
437+
rlang::abort(message = "Invalid value for `{{paramName}}` when calling {{classname}}${{operationId}}. Must be {{_enum}}.",
438+
.subclass = "ApiException",
439+
ApiException = ApiException$new(status = 0,
440+
reason = "Invalid value for `{{paramName}}` when calling {{classname}}${{operationId}}. Must be {{_enum}}."))
441+
{{/useRlangExceptionHandling}}
442+
}
443+
{{/isEnum}}
397444
query_params[["{{baseName}}"]] <- `{{paramName}}`
398445
{{/isArray}}
399446

samples/client/petstore/R-httr2-wrapper/R/pet_api.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,13 @@ PetApi <- R6::R6Class(
943943

944944
# explore
945945
for (query_item in `status`) {
946+
# validate enum values
947+
if (!(query_item %in% c("available", "pending", "sold"))) {
948+
rlang::abort(message = "Invalid value for `status` when calling PetApi$find_pets_by_status. Must be [available, pending, sold].",
949+
.subclass = "ApiException",
950+
ApiException = ApiException$new(status = 0,
951+
reason = "Invalid value for `status` when calling PetApi$find_pets_by_status. Must be [available, pending, sold]."))
952+
}
946953
query_params[["status"]] <- c(query_params[["status"]], list(`status` = query_item))
947954
}
948955

samples/client/petstore/R-httr2-wrapper/tests/testthat/test_petstore.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,16 @@ test_that("GetPetById with data_file", {
235235
})
236236

237237
test_that("find_pets_by_status", {
238+
# input invalid
239+
var_status <- c("something") # array[character] | Tags to filter by
240+
result <- tryCatch(pet_api$find_pets_by_status(var_status),
241+
ApiException = function(ex) ex
242+
)
243+
244+
expect_equal(result$ApiException$reason, "Invalid value for `status` when calling PetApi$find_pets_by_status. Must be [available, pending, sold].")
245+
})
246+
247+
test_that("find_pets_by_tags", {
238248
pet_tag_test <- Pet$new("name_test",
239249
photoUrls = list("photo_test", "second test"),
240250
category = Category$new(id = 4455, name = "test_cat"),

samples/client/petstore/R-httr2/R/pet_api.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,13 @@ PetApi <- R6::R6Class(
943943

944944
# explore
945945
for (query_item in `status`) {
946+
# validate enum values
947+
if (!(query_item %in% c("available", "pending", "sold"))) {
948+
rlang::abort(message = "Invalid value for `status` when calling PetApi$find_pets_by_status. Must be [available, pending, sold].",
949+
.subclass = "ApiException",
950+
ApiException = ApiException$new(status = 0,
951+
reason = "Invalid value for `status` when calling PetApi$find_pets_by_status. Must be [available, pending, sold]."))
952+
}
946953
query_params[["status"]] <- c(query_params[["status"]], list(`status` = query_item))
947954
}
948955

samples/client/petstore/R/R/pet_api.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,13 @@ PetApi <- R6::R6Class(
943943

944944
# explore
945945
for (query_item in `status`) {
946+
# validate enum values
947+
if (!(query_item %in% c("available", "pending", "sold"))) {
948+
rlang::abort(message = "Invalid value for `status` when calling PetApi$FindPetsByStatus. Must be [available, pending, sold].",
949+
.subclass = "ApiException",
950+
ApiException = ApiException$new(status = 0,
951+
reason = "Invalid value for `status` when calling PetApi$FindPetsByStatus. Must be [available, pending, sold]."))
952+
}
946953
query_params[["status"]] <- c(query_params[["status"]], list(`status` = query_item))
947954
}
948955

0 commit comments

Comments
 (0)