Skip to content

Commit

Permalink
add validation for query enum parameters in r client (#13415)
Browse files Browse the repository at this point in the history
  • Loading branch information
wing328 authored Sep 14, 2022
1 parent 7ff47dc commit 37fb856
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
47 changes: 47 additions & 0 deletions modules/openapi-generator/src/main/resources/r/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
7 changes: 7 additions & 0 deletions samples/client/petstore/R-httr2-wrapper/R/pet_api.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
7 changes: 7 additions & 0 deletions samples/client/petstore/R-httr2/R/pet_api.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down
7 changes: 7 additions & 0 deletions samples/client/petstore/R/R/pet_api.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down

0 comments on commit 37fb856

Please sign in to comment.