Skip to content

Commit

Permalink
better error handling in the r client (#13800)
Browse files Browse the repository at this point in the history
  • Loading branch information
wing328 authored Oct 23, 2022
1 parent 6650ba6 commit d74c49b
Show file tree
Hide file tree
Showing 62 changed files with 827 additions and 301 deletions.
102 changes: 71 additions & 31 deletions modules/openapi-generator/src/main/resources/r/modelGeneric.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -60,37 +60,49 @@
}
{{/isEnum}}
{{#isInteger}}
stopifnot(is.numeric(`{{name}}`), length(`{{name}}`) == 1)
if (!(is.numeric(`{{name}}`) && length(`{{name}}`) == 1)) {
stop(paste("Error! Invalid data for `{{name}}`. Must be an integer:", `{{name}}`))
}
{{/isInteger}}
{{#isLong}}
stopifnot(is.numeric(`{{name}}`), length(`{{name}}`) == 1)
if (!(is.numeric(`{{name}}`) && length(`{{name}}`) == 1)) {
stop(paste("Error! Invalid data for `{{name}}`. Must be an integer:", `{{name}}`))
}
{{/isLong}}
{{#isFloat}}
stopifnot(is.numeric(`{{name}}`), length(`{{name}}`) == 1)
if (!(is.numeric(`{{name}}`) && length(`{{name}}`) == 1)) {
stop(paste("Error! Invalid data for `{{name}}`. Must be a number:", `{{name}}`))
}
{{/isFloat}}
{{#isDouble}}
stopifnot(is.numeric(`{{name}}`), length(`{{name}}`) == 1)
if (!(is.numeric(`{{name}}`) && length(`{{name}}`) == 1)) {
stop(paste("Error! Invalid data for `{{name}}`. Must be a number:", `{{name}}`))
}
{{/isDouble}}
{{#isString}}
stopifnot(is.character(`{{name}}`), length(`{{name}}`) == 1)
if (!(is.character(`{{name}}`) && length(`{{name}}`) == 1)) {
stop(paste("Error! Invalid data for `{{name}}`. Must be a string:", `{{name}}`))
}
{{/isString}}
{{#isBoolean}}
stopifnot(is.logical(`{{name}}`), length(`{{name}}`) == 1)
if (!(is.logical(`{{name}}`) && length(`{{name}}`) == 1)) {
stop(paste("Error! Invalid data for `{{name}}`. Must be a boolean:", `{{name}}`))
}
{{/isBoolean}}
{{#isDate}}
if (!is.character(`{{name}}`)) {
stop(paste("Error! Invalid Date. Must be a string:", `{{name}}`))
if (!(is.character(`{{name}}`) && length(`{{name}}`) == 1)) {
stop(paste("Error! Invalid data for `{{name}}`. Must be a string:", `{{name}}`))
}
{{/isDate}}
{{#isDateTime}}
if (!is.character(`{{name}}`)) {
stop(paste("Error! Invalid DateTime. Must be a string:", `{{name}}`))
if (!(is.character(`{{name}}`) && length(`{{name}}`) == 1)) {
stop(paste("Error! Invalid data for `{{name}}`. Must be a string:", `{{name}}`))
}
{{/isDateTime}}
{{#isUri}}
# to validate URL. ref: https://stackoverflow.com/questions/73952024/url-validation-in-r
if (!stringr::str_detect(`{{name}}`, "(https?|ftp)://[^ /$.?#].[^\\s]*")) {
stop(paste("Error! Invalid URL:", `{{name}}`))
stop(paste("Error! Invalid data for `{{name}}`. Must be a URL:", `{{name}}`))
}
{{/isUri}}
{{^isPrimitiveType}}
Expand Down Expand Up @@ -120,37 +132,49 @@
}
{{/isEnum}}
{{#isInteger}}
stopifnot(is.numeric(`{{name}}`), length(`{{name}}`) == 1)
if (!(is.numeric(`{{name}}`) && length(`{{name}}`) == 1)) {
stop(paste("Error! Invalid data for `{{name}}`. Must be an integer:", `{{name}}`))
}
{{/isInteger}}
{{#isLong}}
stopifnot(is.numeric(`{{name}}`), length(`{{name}}`) == 1)
if (!(is.numeric(`{{name}}`) && length(`{{name}}`) == 1)) {
stop(paste("Error! Invalid data for `{{name}}`. Must be an integer:", `{{name}}`))
}
{{/isLong}}
{{#isFloat}}
stopifnot(is.numeric(`{{name}}`), length(`{{name}}`) == 1)
if (!(is.numeric(`{{name}}`) && length(`{{name}}`) == 1)) {
stop(paste("Error! Invalid data for `{{name}}`. Must be a number:", `{{name}}`))
}
{{/isFloat}}
{{#isDouble}}
stopifnot(is.numeric(`{{name}}`), length(`{{name}}`) == 1)
if (!(is.numeric(`{{name}}`) && length(`{{name}}`) == 1)) {
stop(paste("Error! Invalid data for `{{name}}`. Must be a number:", `{{name}}`))
}
{{/isDouble}}
{{#isString}}
stopifnot(is.character(`{{name}}`), length(`{{name}}`) == 1)
if (!(is.character(`{{name}}`) && length(`{{name}}`) == 1)) {
stop(paste("Error! Invalid data for `{{name}}`. Must be a string:", `{{name}}`))
}
{{/isString}}
{{#isBoolean}}
stopifnot(is.logical(`{{name}}`), length(`{{name}}`) == 1)
if (!(is.logical(`{{name}}`) && length(`{{name}}`) == 1)) {
stop(paste("Error! Invalid data for `{{name}}`. Must be a boolean:", `{{name}}`))
}
{{/isBoolean}}
{{#isDate}}
if (!is.character(`{{name}}`)) {
stop(paste("Error! Invalid Date. Must be a string:", `{{name}}`))
stop(paste("Error! Invalid data for `{{name}}`. Must be a string:", `{{name}}`))
}
{{/isDate}}
{{#isDateTime}}
if (!is.character(`{{name}}`)) {
stop(paste("Error! Invalid DateTime. Must be a string:", `{{name}}`))
stop(paste("Error! Invalid data for `{{name}}`. Must be a string:", `{{name}}`))
}
{{/isDateTime}}
{{#isUri}}
# to validate URL. ref: https://stackoverflow.com/questions/73952024/url-validation-in-r
if (!stringr::str_detect(`{{name}}`, "(https?|ftp)://[^ /$.?#].[^\\s]*")) {
stop(paste("Error! Invalid URL:", `{{name}}`))
stop(paste("Error! Invalid data for `{{name}}`. Must be a URL:", `{{name}}`))
}
{{/isUri}}
{{^isPrimitiveType}}
Expand Down Expand Up @@ -259,7 +283,7 @@
{{#isUri}}
# to validate URL. ref: https://stackoverflow.com/questions/73952024/url-validation-in-r
if (!stringr::str_detect(this_object$`{{baseName}}`, "(https?|ftp)://[^ /$.?#].[^\\s]*")) {
stop(paste("Error! Invalid URL:", this_object$`{{baseName}}`))
stop(paste("Error! Invalid data for `{{baseName}}`. Must be a URL:", this_object$`{{baseName}}`))
}
{{/isUri}}
self$`{{name}}` <- this_object$`{{baseName}}`
Expand Down Expand Up @@ -391,7 +415,7 @@
{{#isUri}}
# to validate URL. ref: https://stackoverflow.com/questions/73952024/url-validation-in-r
if (!stringr::str_detect(this_object$`{{name}}`, "(https?|ftp)://[^ /$.?#].[^\\s]*")) {
stop(paste("Error! Invalid URL:", this_object$`{{name}}`))
stop(paste("Error! Invalid data for `{{name}}`. Must be a URL:", this_object$`{{name}}`))
}
{{/isUri}}
self$`{{name}}` <- this_object$`{{name}}`
Expand Down Expand Up @@ -426,33 +450,49 @@
if (!is.null(input_json$`{{name}}`)) {
{{^isContainer}}
{{#isInteger}}
stopifnot(is.numeric(input_json$`{{name}}`), length(input_json$`{{name}}`) == 1)
if (!(is.numeric(input_json$`{{name}}`) && length(input_json$`{{name}}`) == 1)) {
stop(paste("Error! Invalid data for `{{name}}`. Must be an integer:", input_json$`{{name}}`))
}
{{/isInteger}}
{{#isLong}}
stopifnot(is.numeric(input_json$`{{name}}`), length(input_json$`{{name}}`) == 1)
if (!(is.numeric(input_json$`{{name}}`) && length(input_json$`{{name}}`) == 1)) {
stop(paste("Error! Invalid data for `{{name}}`. Must be an integer:", input_json$`{{name}}`))
}
{{/isLong}}
{{#isFloat}}
stopifnot(is.numeric(input_json$`{{name}}`), length(input_json$`{{name}}`) == 1)
if (!(is.numeric(input_json$`{{name}}`) && length(input_json$`{{name}}`) == 1)) {
stop(paste("Error! Invalid data for `{{name}}`. Must be a number:", input_json$`{{name}}`))
}
{{/isFloat}}
{{#isDouble}}
stopifnot(is.numeric(input_json$`{{name}}`), length(input_json$`{{name}}`) == 1)
if (!(is.numeric(input_json$`{{name}}`) && length(input_json$`{{name}}`) == 1)) {
stop(paste("Error! Invalid data for `{{name}}`. Must be a number:", input_json$`{{name}}`))
}
{{/isDouble}}
{{#isString}}
stopifnot(is.character(input_json$`{{name}}`), length(input_json$`{{name}}`) == 1)
if (!(is.character(input_json$`{{name}}`) && length(input_json$`{{name}}`) == 1)) {
stop(paste("Error! Invalid data for `{{name}}`. Must be a string:", input_json$`{{name}}`))
}
{{/isString}}
{{#isBoolean}}
stopifnot(is.logical(input_json$`{{name}}`), length(input_json$`{{name}}`) == 1)
if (!(is.logical(input_json$`{{name}}`) && length(input_json$`{{name}}`) == 1)) {
stop(paste("Error! Invalid data for `{{name}}`. Must be a boolean:", input_json$`{{name}}`))
}
{{/isBoolean}}
{{#isDate}}
stopifnot(is.character(input_json$`{{name}}`), length(input_json$`{{name}}`) == 1)
if (!(is.character(input_json$`{{name}}`) && length(input_json$`{{name}}`) == 1)) {
stop(paste("Error! Invalid data for `{{name}}`. Must be a string:", input_json$`{{name}}`))
}
{{/isDate}}
{{#isDateTime}}
stopifnot(is.character(input_json$`{{name}}`), length(input_json$`{{name}}`) == 1)
if (!(is.character(input_json$`{{name}}`) && length(input_json$`{{name}}`) == 1)) {
stop(paste("Error! Invalid data for `{{name}}`. Must be a string:", input_json$`{{name}}`))
}
{{/isDateTime}}
{{#isUri}}
# to validate URL. ref: https://stackoverflow.com/questions/73952024/url-validation-in-r
if (!stringr::str_detect(input_json$`{{name}}`, "(https?|ftp)://[^ /$.?#].[^\\s]*")) {
stop(paste("Error! Invalid URL:", input_json$`{{name}}`))
stop(paste("Error! Invalid data for `{{name}}`. Must be a URL:", input_json$`{{name}}`))
}
{{/isUri}}
{{^isPrimitiveType}}
Expand Down
20 changes: 15 additions & 5 deletions samples/client/petstore/R-httr2-wrapper/R/allof_tag_api_response.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,33 @@ AllofTagApiResponse <- R6::R6Class(
#' @export
initialize = function(`id` = NULL, `name` = NULL, `code` = NULL, `type` = NULL, `message` = NULL, additional_properties = NULL, ...) {
if (!is.null(`id`)) {
stopifnot(is.numeric(`id`), length(`id`) == 1)
if (!(is.numeric(`id`) && length(`id`) == 1)) {
stop(paste("Error! Invalid data for `id`. Must be an integer:", `id`))
}
self$`id` <- `id`
}
if (!is.null(`name`)) {
stopifnot(is.character(`name`), length(`name`) == 1)
if (!(is.character(`name`) && length(`name`) == 1)) {
stop(paste("Error! Invalid data for `name`. Must be a string:", `name`))
}
self$`name` <- `name`
}
if (!is.null(`code`)) {
stopifnot(is.numeric(`code`), length(`code`) == 1)
if (!(is.numeric(`code`) && length(`code`) == 1)) {
stop(paste("Error! Invalid data for `code`. Must be an integer:", `code`))
}
self$`code` <- `code`
}
if (!is.null(`type`)) {
stopifnot(is.character(`type`), length(`type`) == 1)
if (!(is.character(`type`) && length(`type`) == 1)) {
stop(paste("Error! Invalid data for `type`. Must be a string:", `type`))
}
self$`type` <- `type`
}
if (!is.null(`message`)) {
stopifnot(is.character(`message`), length(`message`) == 1)
if (!(is.character(`message`) && length(`message`) == 1)) {
stop(paste("Error! Invalid data for `message`. Must be a string:", `message`))
}
self$`message` <- `message`
}
if (!is.null(additional_properties)) {
Expand Down
12 changes: 9 additions & 3 deletions samples/client/petstore/R-httr2-wrapper/R/animal.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@ Animal <- R6::R6Class(
#' @export
initialize = function(`className`, `color` = "red", additional_properties = NULL, ...) {
if (!missing(`className`)) {
stopifnot(is.character(`className`), length(`className`) == 1)
if (!(is.character(`className`) && length(`className`) == 1)) {
stop(paste("Error! Invalid data for `className`. Must be a string:", `className`))
}
self$`className` <- `className`
}
if (!is.null(`color`)) {
stopifnot(is.character(`color`), length(`color`) == 1)
if (!(is.character(`color`) && length(`color`) == 1)) {
stop(paste("Error! Invalid data for `color`. Must be a string:", `color`))
}
self$`color` <- `color`
}
if (!is.null(additional_properties)) {
Expand Down Expand Up @@ -162,7 +166,9 @@ Animal <- R6::R6Class(
input_json <- jsonlite::fromJSON(input)
# check the required field `className`
if (!is.null(input_json$`className`)) {
stopifnot(is.character(input_json$`className`), length(input_json$`className`) == 1)
if (!(is.character(input_json$`className`) && length(input_json$`className`) == 1)) {
stop(paste("Error! Invalid data for `className`. Must be a string:", input_json$`className`))
}
} else {
stop(paste("The JSON input `", input, "` is invalid for Animal: the required field `className` is missing."))
}
Expand Down
16 changes: 12 additions & 4 deletions samples/client/petstore/R-httr2-wrapper/R/basque_pig.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ BasquePig <- R6::R6Class(
#' @export
initialize = function(`className`, `color`, additional_properties = NULL, ...) {
if (!missing(`className`)) {
stopifnot(is.character(`className`), length(`className`) == 1)
if (!(is.character(`className`) && length(`className`) == 1)) {
stop(paste("Error! Invalid data for `className`. Must be a string:", `className`))
}
self$`className` <- `className`
}
if (!missing(`color`)) {
stopifnot(is.character(`color`), length(`color`) == 1)
if (!(is.character(`color`) && length(`color`) == 1)) {
stop(paste("Error! Invalid data for `color`. Must be a string:", `color`))
}
self$`color` <- `color`
}
if (!is.null(additional_properties)) {
Expand Down Expand Up @@ -160,13 +164,17 @@ BasquePig <- R6::R6Class(
input_json <- jsonlite::fromJSON(input)
# check the required field `className`
if (!is.null(input_json$`className`)) {
stopifnot(is.character(input_json$`className`), length(input_json$`className`) == 1)
if (!(is.character(input_json$`className`) && length(input_json$`className`) == 1)) {
stop(paste("Error! Invalid data for `className`. Must be a string:", input_json$`className`))
}
} else {
stop(paste("The JSON input `", input, "` is invalid for BasquePig: the required field `className` is missing."))
}
# check the required field `color`
if (!is.null(input_json$`color`)) {
stopifnot(is.character(input_json$`color`), length(input_json$`color`) == 1)
if (!(is.character(input_json$`color`) && length(input_json$`color`) == 1)) {
stop(paste("Error! Invalid data for `color`. Must be a string:", input_json$`color`))
}
} else {
stop(paste("The JSON input `", input, "` is invalid for BasquePig: the required field `color` is missing."))
}
Expand Down
16 changes: 12 additions & 4 deletions samples/client/petstore/R-httr2-wrapper/R/cat.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,21 @@ Cat <- R6::R6Class(
#' @export
initialize = function(`className`, `color` = "red", `declawed` = NULL, additional_properties = NULL, ...) {
if (!missing(`className`)) {
stopifnot(is.character(`className`), length(`className`) == 1)
if (!(is.character(`className`) && length(`className`) == 1)) {
stop(paste("Error! Invalid data for `className`. Must be a string:", `className`))
}
self$`className` <- `className`
}
if (!is.null(`color`)) {
stopifnot(is.character(`color`), length(`color`) == 1)
if (!(is.character(`color`) && length(`color`) == 1)) {
stop(paste("Error! Invalid data for `color`. Must be a string:", `color`))
}
self$`color` <- `color`
}
if (!is.null(`declawed`)) {
stopifnot(is.logical(`declawed`), length(`declawed`) == 1)
if (!(is.logical(`declawed`) && length(`declawed`) == 1)) {
stop(paste("Error! Invalid data for `declawed`. Must be a boolean:", `declawed`))
}
self$`declawed` <- `declawed`
}
if (!is.null(additional_properties)) {
Expand Down Expand Up @@ -184,7 +190,9 @@ Cat <- R6::R6Class(
input_json <- jsonlite::fromJSON(input)
# check the required field `className`
if (!is.null(input_json$`className`)) {
stopifnot(is.character(input_json$`className`), length(input_json$`className`) == 1)
if (!(is.character(input_json$`className`) && length(input_json$`className`) == 1)) {
stop(paste("Error! Invalid data for `className`. Must be a string:", input_json$`className`))
}
} else {
stop(paste("The JSON input `", input, "` is invalid for Cat: the required field `className` is missing."))
}
Expand Down
4 changes: 3 additions & 1 deletion samples/client/petstore/R-httr2-wrapper/R/cat_all_of.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ CatAllOf <- R6::R6Class(
#' @export
initialize = function(`declawed` = NULL, additional_properties = NULL, ...) {
if (!is.null(`declawed`)) {
stopifnot(is.logical(`declawed`), length(`declawed`) == 1)
if (!(is.logical(`declawed`) && length(`declawed`) == 1)) {
stop(paste("Error! Invalid data for `declawed`. Must be a boolean:", `declawed`))
}
self$`declawed` <- `declawed`
}
if (!is.null(additional_properties)) {
Expand Down
8 changes: 6 additions & 2 deletions samples/client/petstore/R-httr2-wrapper/R/category.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ Category <- R6::R6Class(
#' @export
initialize = function(`id` = NULL, `name` = NULL, additional_properties = NULL, ...) {
if (!is.null(`id`)) {
stopifnot(is.numeric(`id`), length(`id`) == 1)
if (!(is.numeric(`id`) && length(`id`) == 1)) {
stop(paste("Error! Invalid data for `id`. Must be an integer:", `id`))
}
self$`id` <- `id`
}
if (!is.null(`name`)) {
stopifnot(is.character(`name`), length(`name`) == 1)
if (!(is.character(`name`) && length(`name`) == 1)) {
stop(paste("Error! Invalid data for `name`. Must be a string:", `name`))
}
self$`name` <- `name`
}
if (!is.null(additional_properties)) {
Expand Down
Loading

0 comments on commit d74c49b

Please sign in to comment.