better validation for r parameters (#13119)

This commit is contained in:
William Cheng 2022-08-06 21:06:39 +08:00 committed by GitHub
parent 462f927b87
commit d3a268924f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 188 additions and 0 deletions

View File

@ -253,6 +253,100 @@
}
{{/requiredParams}}
{{#allParams}}
{{#maxLength}}
if (nchar(`{{paramName}}`) > {{maxLength}}) {
{{#useDefaultExceptionHandling}}
stop("Invalid length for `{{paramName}}` when calling {{classname}}${{operationId}}, must be smaller than or equal to {{maxLength}}.")
{{/useDefaultExceptionHandling}}
{{#useRlangExceptionHandling}}
rlang::abort(message = "Invalid length for `{{paramName}}` when calling {{classname}}${{operationId}}, must be smaller than or equal to {{maxLength}}.",
.subclass = "ApiException",
ApiException = ApiException$new(status = 0,
reason = "Invalid length for ${{paramName}} when calling {{classname}}.{{operationId}}, must be smaller than or equal to {{maxLength}}."))
{{/useRlangExceptionHandling}}
}
{{/maxLength}}
{{#minLength}}
if (nchar(`{{paramName}}`) < {{minLength}}) {
{{#useDefaultExceptionHandling}}
stop("Invalid length for `{{paramName}}` when calling {{classname}}${{operationId}}, must be bigger than or equal to {{minLength}}.")
{{/useDefaultExceptionHandling}}
{{#useRlangExceptionHandling}}
rlang::abort(message = "Invalid length for `{{paramName}}` when calling {{classname}}${{operationId}}, must be bigger than or equal to {{minLength}}.",
.subclass = "ApiException",
ApiException = ApiException$new(status = 0,
reason = "Invalid length for `{{paramName}}` when calling {{classname}}${{operationId}}, must be bigger than or equal to {{minLength}}."))
{{/useRlangExceptionHandling}}
}
{{/minLength}}
{{#maximum}}
if (`{{paramName}}` >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}}) {
{{#useDefaultExceptionHandling}}
stop("Invalid value for `{{paramName}}` when calling {{classname}}${{operationId}}, must be smaller than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}{{maximum}}.")
{{/useDefaultExceptionHandling}}
{{#useRlangExceptionHandling}}
rlang::abort(message = "Invalid value for `{{paramName}}` when calling {{classname}}${{operationId}}, must be smaller than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}{{maximum}}.",
.subclass = "ApiException",
ApiException = ApiException$new(status = 0,
reason = "Invalid value for `{{paramName}}` when calling {{classname}}${{operationId}}, must be smaller than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}{{maximum}}."))
{{/useRlangExceptionHandling}}
}
{{/maximum}}
{{#minimum}}
if (`{{paramName}}` <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}}) {
{{#useDefaultExceptionHandling}}
stop("Invalid value for `{{paramName}}` when calling {{classname}}${{operationId}}, must be bigger than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}{{minimum}}.")
{{/useDefaultExceptionHandling}}
{{#useRlangExceptionHandling}}
rlang::abort(message = "Invalid value for `{{paramName}}` when calling {{classname}}${{operationId}}, must be bigger than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}{{minimum}}.",
.subclass = "ApiException",
ApiException = ApiException$new(status = 0,
reason = "Invalid value for `{{paramName}}` when calling {{classname}}${{operationId}}, must be bigger than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}{{minimum}}."))
{{/useRlangExceptionHandling}}
}
{{/minimum}}
{{#pattern}}
if (!str_detect(`{{paramName}}`, "{{{pattern}}}")) {
{{#useDefaultExceptionHandling}}
stop("Invalid value for `{{paramName}}` when calling {{classname}}${{operationId}}, must conform to the pattern {{{pattern}}}.")
{{/useDefaultExceptionHandling}}
{{#useRlangExceptionHandling}}
rlang::abort(message = "Invalid value for `{{paramName}}` when calling {{classname}}${{operationId}}, must conform to the pattern {{{pattern}}}.",
.subclass = "ApiException",
ApiException = ApiException$new(status = 0,
reason = "Invalid value for `{{paramName}}` when calling {{classname}}${{operationId}}, must conform to the pattern {{{pattern}}}."))
{{/useRlangExceptionHandling}}
}
{{/pattern}}
{{#maxItems}}
if (length(`{{paramName}}`) > {{maxItems}}) {
{{#useDefaultExceptionHandling}}
stop("Invalid length for `{{paramName}}` when calling {{classname}}${{operationId}}, number of items must be less than or equal to {{maxItems}}.")
{{/useDefaultExceptionHandling}}
{{#useRlangExceptionHandling}}
rlang::abort(message = "Invalid length for `{{paramName}}` when calling {{classname}}${{operationId}}, number of items must be less than or equal to {{maxItems}}.",
.subclass = "ApiException",
ApiException = ApiException$new(status = 0,
reason = "Invalid length for `{{paramName}}` when calling {{classname}}${{operationId}}, number of items must be less than or equal to {{maxItems}}."))
{{/useRlangExceptionHandling}}
}
{{/maxItems}}
{{#minItems}}
if (length(`{{paramName}}`) < {{minItems}}) {
{{#useDefaultExceptionHandling}}
stop("Invalid length for `{{paramName}}` when calling {{classname}}${{operationId}}, number of items must be greater than or equal to {{minItems}}.")
{{/useDefaultExceptionHandling}}
{{#useRlangExceptionHandling}}
rlang::abort(message = "Invalid length for `{{paramName}}` when calling {{classname}}${{operationId}}, number of items must be greater than or equal to {{minItems}}.",
.subclass = "ApiException",
ApiException = ApiException$new(status = 0,
reason = "Invalid length for `{{paramName}}` when calling {{classname}}${{operationId}}, number of items must be greater than or equal to {{minItems}}."))
{{/useRlangExceptionHandling}}
}
{{/minItems}}
{{/allParams}}
{{#headerParams}}
header_params["{{baseName}}"] <- `{{paramName}}`

View File

@ -136,6 +136,8 @@ FakeApi <- R6::R6Class(
reason = "Missing required parameter `dummy`."))
}
header_params["dummy"] <- `dummy`
header_params["data_file"] <- `var_data_file`

View File

@ -640,6 +640,7 @@ PetApi <- R6::R6Class(
reason = "Missing required parameter `pet`."))
}
if (!missing(`pet`)) {
local_var_body <- `pet`$toJSONString()
} else {
@ -762,6 +763,8 @@ PetApi <- R6::R6Class(
reason = "Missing required parameter `pet_id`."))
}
header_params["api_key"] <- `api_key`
local_var_url_path <- "/pet/{petId}"
@ -867,6 +870,7 @@ PetApi <- R6::R6Class(
reason = "Missing required parameter `status`."))
}
query_params["status"] <- `status`
local_var_url_path <- "/pet/findByStatus"
@ -981,6 +985,7 @@ PetApi <- R6::R6Class(
reason = "Missing required parameter `tags`."))
}
query_params["tags"] <- `tags`
local_var_url_path <- "/pet/findByTags"
@ -1095,6 +1100,7 @@ PetApi <- R6::R6Class(
reason = "Missing required parameter `pet_id`."))
}
local_var_url_path <- "/pet/{petId}"
if (!missing(`pet_id`)) {
local_var_url_path <- gsub(paste0("\\{", "petId", "\\}"), URLencode(as.character(`pet_id`), reserved = TRUE), local_var_url_path)
@ -1217,6 +1223,7 @@ PetApi <- R6::R6Class(
reason = "Missing required parameter `pet_id`."))
}
local_var_url_path <- "/pet/{petId}?streaming"
if (!missing(`pet_id`)) {
local_var_url_path <- gsub(paste0("\\{", "petId", "\\}"), URLencode(as.character(`pet_id`), reserved = TRUE), local_var_url_path)
@ -1344,6 +1351,7 @@ PetApi <- R6::R6Class(
reason = "Missing required parameter `header_test_int`."))
}
header_params["header_test_int"] <- `header_test_int`
local_var_url_path <- "/pet_header_test"
@ -1463,6 +1471,7 @@ PetApi <- R6::R6Class(
reason = "Missing required parameter `pet`."))
}
if (!missing(`pet`)) {
local_var_body <- `pet`$toJSONString()
} else {
@ -1583,6 +1592,9 @@ PetApi <- R6::R6Class(
reason = "Missing required parameter `pet_id`."))
}
form_params["name"] <- `name`
form_params["status"] <- `status`
local_var_url_path <- "/pet/{petId}"
@ -1692,6 +1704,9 @@ PetApi <- R6::R6Class(
reason = "Missing required parameter `pet_id`."))
}
form_params["additionalMetadata"] <- `additional_metadata`
file_params["file"] <- curl::form_file(`file`)
local_var_url_path <- "/pet/{petId}/uploadImage"

View File

@ -284,6 +284,7 @@ StoreApi <- R6::R6Class(
reason = "Missing required parameter `order_id`."))
}
local_var_url_path <- "/store/order/{orderId}"
if (!missing(`order_id`)) {
local_var_url_path <- gsub(paste0("\\{", "orderId", "\\}"), URLencode(as.character(`order_id`), reserved = TRUE), local_var_url_path)
@ -486,6 +487,19 @@ StoreApi <- R6::R6Class(
reason = "Missing required parameter `order_id`."))
}
if (`order_id` > 5) {
rlang::abort(message = "Invalid value for `order_id` when calling StoreApi$get_order_by_id, must be smaller than or equal to 5.",
.subclass = "ApiException",
ApiException = ApiException$new(status = 0,
reason = "Invalid value for `order_id` when calling StoreApi$get_order_by_id, must be smaller than or equal to 5."))
}
if (`order_id` < 1) {
rlang::abort(message = "Invalid value for `order_id` when calling StoreApi$get_order_by_id, must be bigger than or equal to 1.",
.subclass = "ApiException",
ApiException = ApiException$new(status = 0,
reason = "Invalid value for `order_id` when calling StoreApi$get_order_by_id, must be bigger than or equal to 1."))
}
local_var_url_path <- "/store/order/{orderId}"
if (!missing(`order_id`)) {
local_var_url_path <- gsub(paste0("\\{", "orderId", "\\}"), URLencode(as.character(`order_id`), reserved = TRUE), local_var_url_path)
@ -598,6 +612,7 @@ StoreApi <- R6::R6Class(
reason = "Missing required parameter `order`."))
}
if (!missing(`order`)) {
local_var_body <- `order`$toJSONString()
} else {

View File

@ -470,6 +470,7 @@ UserApi <- R6::R6Class(
reason = "Missing required parameter `user`."))
}
if (!missing(`user`)) {
local_var_body <- `user`$toJSONString()
} else {
@ -573,6 +574,7 @@ UserApi <- R6::R6Class(
reason = "Missing required parameter `user`."))
}
if (!missing(`user`)) {
body.items <- paste(unlist(lapply(`user`, function(param) {
param$toJSONString()
@ -679,6 +681,7 @@ UserApi <- R6::R6Class(
reason = "Missing required parameter `user`."))
}
if (!missing(`user`)) {
body.items <- paste(unlist(lapply(`user`, function(param) {
param$toJSONString()
@ -785,6 +788,7 @@ UserApi <- R6::R6Class(
reason = "Missing required parameter `username`."))
}
local_var_url_path <- "/user/{username}"
if (!missing(`username`)) {
local_var_url_path <- gsub(paste0("\\{", "username", "\\}"), URLencode(as.character(`username`), reserved = TRUE), local_var_url_path)
@ -888,6 +892,7 @@ UserApi <- R6::R6Class(
reason = "Missing required parameter `username`."))
}
local_var_url_path <- "/user/{username}"
if (!missing(`username`)) {
local_var_url_path <- gsub(paste0("\\{", "username", "\\}"), URLencode(as.character(`username`), reserved = TRUE), local_var_url_path)
@ -1009,6 +1014,14 @@ UserApi <- R6::R6Class(
reason = "Missing required parameter `password`."))
}
if (!str_detect(`username`, "/^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$/")) {
rlang::abort(message = "Invalid value for `username` when calling UserApi$login_user, must conform to the pattern /^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$/.",
.subclass = "ApiException",
ApiException = ApiException$new(status = 0,
reason = "Invalid value for `username` when calling UserApi$login_user, must conform to the pattern /^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$/."))
}
query_params["username"] <- `username`
query_params["password"] <- `password`
@ -1216,6 +1229,8 @@ UserApi <- R6::R6Class(
reason = "Missing required parameter `user`."))
}
if (!missing(`user`)) {
local_var_body <- `user`$toJSONString()
} else {

View File

@ -136,6 +136,8 @@ FakeApi <- R6::R6Class(
reason = "Missing required parameter `dummy`."))
}
header_params["dummy"] <- `dummy`
header_params["data_file"] <- `var_data_file`

View File

@ -640,6 +640,7 @@ PetApi <- R6::R6Class(
reason = "Missing required parameter `pet`."))
}
if (!missing(`pet`)) {
local_var_body <- `pet`$toJSONString()
} else {
@ -762,6 +763,8 @@ PetApi <- R6::R6Class(
reason = "Missing required parameter `pet_id`."))
}
header_params["api_key"] <- `api_key`
local_var_url_path <- "/pet/{petId}"
@ -867,6 +870,7 @@ PetApi <- R6::R6Class(
reason = "Missing required parameter `status`."))
}
query_params["status"] <- `status`
local_var_url_path <- "/pet/findByStatus"
@ -981,6 +985,7 @@ PetApi <- R6::R6Class(
reason = "Missing required parameter `tags`."))
}
query_params["tags"] <- `tags`
local_var_url_path <- "/pet/findByTags"
@ -1095,6 +1100,7 @@ PetApi <- R6::R6Class(
reason = "Missing required parameter `pet_id`."))
}
local_var_url_path <- "/pet/{petId}"
if (!missing(`pet_id`)) {
local_var_url_path <- gsub(paste0("\\{", "petId", "\\}"), URLencode(as.character(`pet_id`), reserved = TRUE), local_var_url_path)
@ -1217,6 +1223,7 @@ PetApi <- R6::R6Class(
reason = "Missing required parameter `pet_id`."))
}
local_var_url_path <- "/pet/{petId}?streaming"
if (!missing(`pet_id`)) {
local_var_url_path <- gsub(paste0("\\{", "petId", "\\}"), URLencode(as.character(`pet_id`), reserved = TRUE), local_var_url_path)
@ -1344,6 +1351,7 @@ PetApi <- R6::R6Class(
reason = "Missing required parameter `header_test_int`."))
}
header_params["header_test_int"] <- `header_test_int`
local_var_url_path <- "/pet_header_test"
@ -1463,6 +1471,7 @@ PetApi <- R6::R6Class(
reason = "Missing required parameter `pet`."))
}
if (!missing(`pet`)) {
local_var_body <- `pet`$toJSONString()
} else {
@ -1583,6 +1592,9 @@ PetApi <- R6::R6Class(
reason = "Missing required parameter `pet_id`."))
}
form_params["name"] <- `name`
form_params["status"] <- `status`
local_var_url_path <- "/pet/{petId}"
@ -1692,6 +1704,9 @@ PetApi <- R6::R6Class(
reason = "Missing required parameter `pet_id`."))
}
form_params["additionalMetadata"] <- `additional_metadata`
file_params["file"] <- httr::upload_file(`file`)
local_var_url_path <- "/pet/{petId}/uploadImage"

View File

@ -284,6 +284,7 @@ StoreApi <- R6::R6Class(
reason = "Missing required parameter `order_id`."))
}
local_var_url_path <- "/store/order/{orderId}"
if (!missing(`order_id`)) {
local_var_url_path <- gsub(paste0("\\{", "orderId", "\\}"), URLencode(as.character(`order_id`), reserved = TRUE), local_var_url_path)
@ -486,6 +487,19 @@ StoreApi <- R6::R6Class(
reason = "Missing required parameter `order_id`."))
}
if (`order_id` > 5) {
rlang::abort(message = "Invalid value for `order_id` when calling StoreApi$GetOrderById, must be smaller than or equal to 5.",
.subclass = "ApiException",
ApiException = ApiException$new(status = 0,
reason = "Invalid value for `order_id` when calling StoreApi$GetOrderById, must be smaller than or equal to 5."))
}
if (`order_id` < 1) {
rlang::abort(message = "Invalid value for `order_id` when calling StoreApi$GetOrderById, must be bigger than or equal to 1.",
.subclass = "ApiException",
ApiException = ApiException$new(status = 0,
reason = "Invalid value for `order_id` when calling StoreApi$GetOrderById, must be bigger than or equal to 1."))
}
local_var_url_path <- "/store/order/{orderId}"
if (!missing(`order_id`)) {
local_var_url_path <- gsub(paste0("\\{", "orderId", "\\}"), URLencode(as.character(`order_id`), reserved = TRUE), local_var_url_path)
@ -598,6 +612,7 @@ StoreApi <- R6::R6Class(
reason = "Missing required parameter `order`."))
}
if (!missing(`order`)) {
local_var_body <- `order`$toJSONString()
} else {

View File

@ -470,6 +470,7 @@ UserApi <- R6::R6Class(
reason = "Missing required parameter `user`."))
}
if (!missing(`user`)) {
local_var_body <- `user`$toJSONString()
} else {
@ -573,6 +574,7 @@ UserApi <- R6::R6Class(
reason = "Missing required parameter `user`."))
}
if (!missing(`user`)) {
body.items <- paste(unlist(lapply(`user`, function(param) {
param$toJSONString()
@ -679,6 +681,7 @@ UserApi <- R6::R6Class(
reason = "Missing required parameter `user`."))
}
if (!missing(`user`)) {
body.items <- paste(unlist(lapply(`user`, function(param) {
param$toJSONString()
@ -785,6 +788,7 @@ UserApi <- R6::R6Class(
reason = "Missing required parameter `username`."))
}
local_var_url_path <- "/user/{username}"
if (!missing(`username`)) {
local_var_url_path <- gsub(paste0("\\{", "username", "\\}"), URLencode(as.character(`username`), reserved = TRUE), local_var_url_path)
@ -888,6 +892,7 @@ UserApi <- R6::R6Class(
reason = "Missing required parameter `username`."))
}
local_var_url_path <- "/user/{username}"
if (!missing(`username`)) {
local_var_url_path <- gsub(paste0("\\{", "username", "\\}"), URLencode(as.character(`username`), reserved = TRUE), local_var_url_path)
@ -1009,6 +1014,14 @@ UserApi <- R6::R6Class(
reason = "Missing required parameter `password`."))
}
if (!str_detect(`username`, "/^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$/")) {
rlang::abort(message = "Invalid value for `username` when calling UserApi$LoginUser, must conform to the pattern /^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$/.",
.subclass = "ApiException",
ApiException = ApiException$new(status = 0,
reason = "Invalid value for `username` when calling UserApi$LoginUser, must conform to the pattern /^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$/."))
}
query_params["username"] <- `username`
query_params["password"] <- `password`
@ -1216,6 +1229,8 @@ UserApi <- R6::R6Class(
reason = "Missing required parameter `user`."))
}
if (!missing(`user`)) {
local_var_body <- `user`$toJSONString()
} else {