From e146afbea1d029e5936364e66a812170211cbccc Mon Sep 17 00:00:00 2001 From: William Cheng Date: Thu, 22 Sep 2022 10:04:03 +0800 Subject: [PATCH] better enum status validation (#13486) --- .../main/resources/r/modelGeneric.mustache | 20 +++++++++++++++++++ .../client/petstore/R-httr2-wrapper/R/order.R | 9 +++++++++ .../client/petstore/R-httr2-wrapper/R/pet.R | 9 +++++++++ .../client/petstore/R-httr2-wrapper/R/zebra.R | 9 +++++++++ .../tests/testthat/test_petstore.R | 12 +++++++++++ samples/client/petstore/R-httr2/R/order.R | 9 +++++++++ samples/client/petstore/R-httr2/R/pet.R | 9 +++++++++ samples/client/petstore/R-httr2/R/zebra.R | 9 +++++++++ samples/client/petstore/R/R/order.R | 9 +++++++++ samples/client/petstore/R/R/pet.R | 9 +++++++++ samples/client/petstore/R/R/zebra.R | 9 +++++++++ 11 files changed, 113 insertions(+) diff --git a/modules/openapi-generator/src/main/resources/r/modelGeneric.mustache b/modules/openapi-generator/src/main/resources/r/modelGeneric.mustache index 55a3f395046..b0ab6658048 100644 --- a/modules/openapi-generator/src/main/resources/r/modelGeneric.mustache +++ b/modules/openapi-generator/src/main/resources/r/modelGeneric.mustache @@ -52,6 +52,11 @@ {{#requiredVars}} if (!missing(`{{name}}`)) { {{^isContainer}} + {{#isEnum}} + if (!(`{{name}}` %in% c({{#_enum}}"{{{.}}}"{{^-last}}, {{/-last}}{{/_enum}}))) { + stop(paste("Error! \"", `{{name}}`, "\" cannot be assigned to `{{name}}`. Must be {{#_enum}}\"{{{.}}}\"{{^-last}}, {{/-last}}{{/_enum}}.", sep = "")) + } + {{/isEnum}} {{#isInteger}} stopifnot(is.numeric(`{{name}}`), length(`{{name}}`) == 1) {{/isInteger}} @@ -97,6 +102,11 @@ {{#optionalVars}} if (!is.null(`{{name}}`)) { {{^isContainer}} + {{#isEnum}} + if (!(`{{name}}` %in% c({{#_enum}}"{{{.}}}"{{^-last}}, {{/-last}}{{/_enum}}))) { + stop(paste("Error! \"", `{{name}}`, "\" cannot be assigned to `{{name}}`. Must be {{#_enum}}\"{{{.}}}\"{{^-last}}, {{/-last}}{{/_enum}}.", sep = "")) + } + {{/isEnum}} {{#isInteger}} stopifnot(is.numeric(`{{name}}`), length(`{{name}}`) == 1) {{/isInteger}} @@ -219,6 +229,11 @@ {{/isContainer}} {{^isContainer}} {{#isPrimitiveType}} + {{#isEnum}} + if (!is.null(this_object$`{{baseName}}`) && !(this_object$`{{baseName}}` %in% c({{#_enum}}"{{{.}}}"{{^-last}}, {{/-last}}{{/_enum}}))) { + stop(paste("Error! \"", this_object$`{{baseName}}`, "\" cannot be assigned to `{{baseName}}`. Must be {{#_enum}}\"{{{.}}}\"{{^-last}}, {{/-last}}{{/_enum}}.", sep = "")) + } + {{/isEnum}} self$`{{name}}` <- this_object$`{{baseName}}` {{/isPrimitiveType}} {{^isPrimitiveType}} @@ -340,6 +355,11 @@ {{/isContainer}} {{^isContainer}} {{#isPrimitiveType}} + {{#isEnum}} + if (!is.null(this_object$`{{baseName}}`) && !(this_object$`{{baseName}}` %in% c({{#_enum}}"{{{.}}}"{{^-last}}, {{/-last}}{{/_enum}}))) { + stop(paste("Error! \"", this_object$`{{baseName}}`, "\" cannot be assigned to `{{baseName}}`. Must be {{#_enum}}\"{{{.}}}\"{{^-last}}, {{/-last}}{{/_enum}}.", sep = "")) + } + {{/isEnum}} self$`{{name}}` <- this_object$`{{name}}` {{/isPrimitiveType}} {{^isPrimitiveType}} diff --git a/samples/client/petstore/R-httr2-wrapper/R/order.R b/samples/client/petstore/R-httr2-wrapper/R/order.R index d8b98bbacc7..77d5790216a 100644 --- a/samples/client/petstore/R-httr2-wrapper/R/order.R +++ b/samples/client/petstore/R-httr2-wrapper/R/order.R @@ -63,6 +63,9 @@ Order <- R6::R6Class( self$`shipDate` <- `shipDate` } if (!is.null(`status`)) { + if (!(`status` %in% c("placed", "approved", "delivered"))) { + stop(paste("Error! \"", `status`, "\" cannot be assigned to `status`. Must be \"placed\", \"approved\", \"delivered\".", sep = "")) + } stopifnot(is.character(`status`), length(`status`) == 1) self$`status` <- `status` } @@ -138,6 +141,9 @@ Order <- R6::R6Class( self$`shipDate` <- this_object$`shipDate` } if (!is.null(this_object$`status`)) { + if (!is.null(this_object$`status`) && !(this_object$`status` %in% c("placed", "approved", "delivered"))) { + stop(paste("Error! \"", this_object$`status`, "\" cannot be assigned to `status`. Must be \"placed\", \"approved\", \"delivered\".", sep = "")) + } self$`status` <- this_object$`status` } if (!is.null(this_object$`complete`)) { @@ -232,6 +238,9 @@ Order <- R6::R6Class( self$`petId` <- this_object$`petId` self$`quantity` <- this_object$`quantity` self$`shipDate` <- this_object$`shipDate` + if (!is.null(this_object$`status`) && !(this_object$`status` %in% c("placed", "approved", "delivered"))) { + stop(paste("Error! \"", this_object$`status`, "\" cannot be assigned to `status`. Must be \"placed\", \"approved\", \"delivered\".", sep = "")) + } self$`status` <- this_object$`status` self$`complete` <- this_object$`complete` # process additional properties/fields in the payload diff --git a/samples/client/petstore/R-httr2-wrapper/R/pet.R b/samples/client/petstore/R-httr2-wrapper/R/pet.R index ce54a2dcd6f..47291dfbdbf 100644 --- a/samples/client/petstore/R-httr2-wrapper/R/pet.R +++ b/samples/client/petstore/R-httr2-wrapper/R/pet.R @@ -69,6 +69,9 @@ Pet <- R6::R6Class( self$`tags` <- `tags` } if (!is.null(`status`)) { + if (!(`status` %in% c("available", "pending", "sold"))) { + stop(paste("Error! \"", `status`, "\" cannot be assigned to `status`. Must be \"available\", \"pending\", \"sold\".", sep = "")) + } stopifnot(is.character(`status`), length(`status`) == 1) self$`status` <- `status` } @@ -145,6 +148,9 @@ Pet <- R6::R6Class( self$`tags` <- ApiClient$new()$deserializeObj(this_object$`tags`, "array[Tag]", loadNamespace("petstore")) } if (!is.null(this_object$`status`)) { + if (!is.null(this_object$`status`) && !(this_object$`status` %in% c("available", "pending", "sold"))) { + stop(paste("Error! \"", this_object$`status`, "\" cannot be assigned to `status`. Must be \"available\", \"pending\", \"sold\".", sep = "")) + } self$`status` <- this_object$`status` } # process additional properties/fields in the payload @@ -237,6 +243,9 @@ Pet <- R6::R6Class( self$`name` <- this_object$`name` self$`photoUrls` <- ApiClient$new()$deserializeObj(this_object$`photoUrls`, "array[character]", loadNamespace("petstore")) self$`tags` <- ApiClient$new()$deserializeObj(this_object$`tags`, "array[Tag]", loadNamespace("petstore")) + if (!is.null(this_object$`status`) && !(this_object$`status` %in% c("available", "pending", "sold"))) { + stop(paste("Error! \"", this_object$`status`, "\" cannot be assigned to `status`. Must be \"available\", \"pending\", \"sold\".", sep = "")) + } self$`status` <- this_object$`status` # process additional properties/fields in the payload for (key in names(this_object)) { diff --git a/samples/client/petstore/R-httr2-wrapper/R/zebra.R b/samples/client/petstore/R-httr2-wrapper/R/zebra.R index 177f5e48fb2..939ca24f2dd 100644 --- a/samples/client/petstore/R-httr2-wrapper/R/zebra.R +++ b/samples/client/petstore/R-httr2-wrapper/R/zebra.R @@ -39,6 +39,9 @@ Zebra <- R6::R6Class( self$`className` <- `className` } if (!is.null(`type`)) { + if (!(`type` %in% c("plains", "mountain", "grevys"))) { + stop(paste("Error! \"", `type`, "\" cannot be assigned to `type`. Must be \"plains\", \"mountain\", \"grevys\".", sep = "")) + } stopifnot(is.character(`type`), length(`type`) == 1) self$`type` <- `type` } @@ -82,6 +85,9 @@ Zebra <- R6::R6Class( fromJSON = function(input_json) { this_object <- jsonlite::fromJSON(input_json) if (!is.null(this_object$`type`)) { + if (!is.null(this_object$`type`) && !(this_object$`type` %in% c("plains", "mountain", "grevys"))) { + stop(paste("Error! \"", this_object$`type`, "\" cannot be assigned to `type`. Must be \"plains\", \"mountain\", \"grevys\".", sep = "")) + } self$`type` <- this_object$`type` } if (!is.null(this_object$`className`)) { @@ -140,6 +146,9 @@ Zebra <- R6::R6Class( #' @export fromJSONString = function(input_json) { this_object <- jsonlite::fromJSON(input_json) + if (!is.null(this_object$`type`) && !(this_object$`type` %in% c("plains", "mountain", "grevys"))) { + stop(paste("Error! \"", this_object$`type`, "\" cannot be assigned to `type`. Must be \"plains\", \"mountain\", \"grevys\".", sep = "")) + } self$`type` <- this_object$`type` self$`className` <- this_object$`className` # process additional properties/fields in the payload 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 1fb5860e8c0..d3010938e50 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 @@ -21,6 +21,18 @@ pet_api$api_client$username <- "username123" pet_api$api_client$password <- "password123" result <- pet_api$add_pet(pet) +test_that("Invalid enum value test", { + expect_error(Pet$new("name_test", + photoUrls = list("photo_test", "second test"), + category = Category$new(id = 450, name = "test_cat"), + id = pet_id, + tags = list( + Tag$new(id = 123, name = "tag_test"), Tag$new(id = 456, name = "unknown") + ), + status = "error_available" + ), "Error! \"error_available\" cannot be assigned to `status`. Must be \"available\", \"pending\", \"sold\".") +}) + test_that("Additional Properties test", { # test tag t <- Tag$new(id = 393, name = "something") diff --git a/samples/client/petstore/R-httr2/R/order.R b/samples/client/petstore/R-httr2/R/order.R index 84954f95e86..c45f20acecf 100644 --- a/samples/client/petstore/R-httr2/R/order.R +++ b/samples/client/petstore/R-httr2/R/order.R @@ -58,6 +58,9 @@ Order <- R6::R6Class( self$`shipDate` <- `shipDate` } if (!is.null(`status`)) { + if (!(`status` %in% c("placed", "approved", "delivered"))) { + stop(paste("Error! \"", `status`, "\" cannot be assigned to `status`. Must be \"placed\", \"approved\", \"delivered\".", sep = "")) + } stopifnot(is.character(`status`), length(`status`) == 1) self$`status` <- `status` } @@ -124,6 +127,9 @@ Order <- R6::R6Class( self$`shipDate` <- this_object$`shipDate` } if (!is.null(this_object$`status`)) { + if (!is.null(this_object$`status`) && !(this_object$`status` %in% c("placed", "approved", "delivered"))) { + stop(paste("Error! \"", this_object$`status`, "\" cannot be assigned to `status`. Must be \"placed\", \"approved\", \"delivered\".", sep = "")) + } self$`status` <- this_object$`status` } if (!is.null(this_object$`complete`)) { @@ -206,6 +212,9 @@ Order <- R6::R6Class( self$`petId` <- this_object$`petId` self$`quantity` <- this_object$`quantity` self$`shipDate` <- this_object$`shipDate` + if (!is.null(this_object$`status`) && !(this_object$`status` %in% c("placed", "approved", "delivered"))) { + stop(paste("Error! \"", this_object$`status`, "\" cannot be assigned to `status`. Must be \"placed\", \"approved\", \"delivered\".", sep = "")) + } self$`status` <- this_object$`status` self$`complete` <- this_object$`complete` self diff --git a/samples/client/petstore/R-httr2/R/pet.R b/samples/client/petstore/R-httr2/R/pet.R index f75deb15930..f452d75f071 100644 --- a/samples/client/petstore/R-httr2/R/pet.R +++ b/samples/client/petstore/R-httr2/R/pet.R @@ -64,6 +64,9 @@ Pet <- R6::R6Class( self$`tags` <- `tags` } if (!is.null(`status`)) { + if (!(`status` %in% c("available", "pending", "sold"))) { + stop(paste("Error! \"", `status`, "\" cannot be assigned to `status`. Must be \"available\", \"pending\", \"sold\".", sep = "")) + } stopifnot(is.character(`status`), length(`status`) == 1) self$`status` <- `status` } @@ -131,6 +134,9 @@ Pet <- R6::R6Class( self$`tags` <- ApiClient$new()$deserializeObj(this_object$`tags`, "array[Tag]", loadNamespace("petstore")) } if (!is.null(this_object$`status`)) { + if (!is.null(this_object$`status`) && !(this_object$`status` %in% c("available", "pending", "sold"))) { + stop(paste("Error! \"", this_object$`status`, "\" cannot be assigned to `status`. Must be \"available\", \"pending\", \"sold\".", sep = "")) + } self$`status` <- this_object$`status` } self @@ -211,6 +217,9 @@ Pet <- R6::R6Class( self$`name` <- this_object$`name` self$`photoUrls` <- ApiClient$new()$deserializeObj(this_object$`photoUrls`, "array[character]", loadNamespace("petstore")) self$`tags` <- ApiClient$new()$deserializeObj(this_object$`tags`, "array[Tag]", loadNamespace("petstore")) + if (!is.null(this_object$`status`) && !(this_object$`status` %in% c("available", "pending", "sold"))) { + stop(paste("Error! \"", this_object$`status`, "\" cannot be assigned to `status`. Must be \"available\", \"pending\", \"sold\".", sep = "")) + } self$`status` <- this_object$`status` self }, diff --git a/samples/client/petstore/R-httr2/R/zebra.R b/samples/client/petstore/R-httr2/R/zebra.R index e8a2896ae4b..d53e727ac01 100644 --- a/samples/client/petstore/R-httr2/R/zebra.R +++ b/samples/client/petstore/R-httr2/R/zebra.R @@ -34,6 +34,9 @@ Zebra <- R6::R6Class( self$`className` <- `className` } if (!is.null(`type`)) { + if (!(`type` %in% c("plains", "mountain", "grevys"))) { + stop(paste("Error! \"", `type`, "\" cannot be assigned to `type`. Must be \"plains\", \"mountain\", \"grevys\".", sep = "")) + } stopifnot(is.character(`type`), length(`type`) == 1) self$`type` <- `type` } @@ -68,6 +71,9 @@ Zebra <- R6::R6Class( fromJSON = function(input_json) { this_object <- jsonlite::fromJSON(input_json) if (!is.null(this_object$`type`)) { + if (!is.null(this_object$`type`) && !(this_object$`type` %in% c("plains", "mountain", "grevys"))) { + stop(paste("Error! \"", this_object$`type`, "\" cannot be assigned to `type`. Must be \"plains\", \"mountain\", \"grevys\".", sep = "")) + } self$`type` <- this_object$`type` } if (!is.null(this_object$`className`)) { @@ -114,6 +120,9 @@ Zebra <- R6::R6Class( #' @export fromJSONString = function(input_json) { this_object <- jsonlite::fromJSON(input_json) + if (!is.null(this_object$`type`) && !(this_object$`type` %in% c("plains", "mountain", "grevys"))) { + stop(paste("Error! \"", this_object$`type`, "\" cannot be assigned to `type`. Must be \"plains\", \"mountain\", \"grevys\".", sep = "")) + } self$`type` <- this_object$`type` self$`className` <- this_object$`className` self diff --git a/samples/client/petstore/R/R/order.R b/samples/client/petstore/R/R/order.R index d8b98bbacc7..77d5790216a 100644 --- a/samples/client/petstore/R/R/order.R +++ b/samples/client/petstore/R/R/order.R @@ -63,6 +63,9 @@ Order <- R6::R6Class( self$`shipDate` <- `shipDate` } if (!is.null(`status`)) { + if (!(`status` %in% c("placed", "approved", "delivered"))) { + stop(paste("Error! \"", `status`, "\" cannot be assigned to `status`. Must be \"placed\", \"approved\", \"delivered\".", sep = "")) + } stopifnot(is.character(`status`), length(`status`) == 1) self$`status` <- `status` } @@ -138,6 +141,9 @@ Order <- R6::R6Class( self$`shipDate` <- this_object$`shipDate` } if (!is.null(this_object$`status`)) { + if (!is.null(this_object$`status`) && !(this_object$`status` %in% c("placed", "approved", "delivered"))) { + stop(paste("Error! \"", this_object$`status`, "\" cannot be assigned to `status`. Must be \"placed\", \"approved\", \"delivered\".", sep = "")) + } self$`status` <- this_object$`status` } if (!is.null(this_object$`complete`)) { @@ -232,6 +238,9 @@ Order <- R6::R6Class( self$`petId` <- this_object$`petId` self$`quantity` <- this_object$`quantity` self$`shipDate` <- this_object$`shipDate` + if (!is.null(this_object$`status`) && !(this_object$`status` %in% c("placed", "approved", "delivered"))) { + stop(paste("Error! \"", this_object$`status`, "\" cannot be assigned to `status`. Must be \"placed\", \"approved\", \"delivered\".", sep = "")) + } self$`status` <- this_object$`status` self$`complete` <- this_object$`complete` # process additional properties/fields in the payload diff --git a/samples/client/petstore/R/R/pet.R b/samples/client/petstore/R/R/pet.R index ce54a2dcd6f..47291dfbdbf 100644 --- a/samples/client/petstore/R/R/pet.R +++ b/samples/client/petstore/R/R/pet.R @@ -69,6 +69,9 @@ Pet <- R6::R6Class( self$`tags` <- `tags` } if (!is.null(`status`)) { + if (!(`status` %in% c("available", "pending", "sold"))) { + stop(paste("Error! \"", `status`, "\" cannot be assigned to `status`. Must be \"available\", \"pending\", \"sold\".", sep = "")) + } stopifnot(is.character(`status`), length(`status`) == 1) self$`status` <- `status` } @@ -145,6 +148,9 @@ Pet <- R6::R6Class( self$`tags` <- ApiClient$new()$deserializeObj(this_object$`tags`, "array[Tag]", loadNamespace("petstore")) } if (!is.null(this_object$`status`)) { + if (!is.null(this_object$`status`) && !(this_object$`status` %in% c("available", "pending", "sold"))) { + stop(paste("Error! \"", this_object$`status`, "\" cannot be assigned to `status`. Must be \"available\", \"pending\", \"sold\".", sep = "")) + } self$`status` <- this_object$`status` } # process additional properties/fields in the payload @@ -237,6 +243,9 @@ Pet <- R6::R6Class( self$`name` <- this_object$`name` self$`photoUrls` <- ApiClient$new()$deserializeObj(this_object$`photoUrls`, "array[character]", loadNamespace("petstore")) self$`tags` <- ApiClient$new()$deserializeObj(this_object$`tags`, "array[Tag]", loadNamespace("petstore")) + if (!is.null(this_object$`status`) && !(this_object$`status` %in% c("available", "pending", "sold"))) { + stop(paste("Error! \"", this_object$`status`, "\" cannot be assigned to `status`. Must be \"available\", \"pending\", \"sold\".", sep = "")) + } self$`status` <- this_object$`status` # process additional properties/fields in the payload for (key in names(this_object)) { diff --git a/samples/client/petstore/R/R/zebra.R b/samples/client/petstore/R/R/zebra.R index 177f5e48fb2..939ca24f2dd 100644 --- a/samples/client/petstore/R/R/zebra.R +++ b/samples/client/petstore/R/R/zebra.R @@ -39,6 +39,9 @@ Zebra <- R6::R6Class( self$`className` <- `className` } if (!is.null(`type`)) { + if (!(`type` %in% c("plains", "mountain", "grevys"))) { + stop(paste("Error! \"", `type`, "\" cannot be assigned to `type`. Must be \"plains\", \"mountain\", \"grevys\".", sep = "")) + } stopifnot(is.character(`type`), length(`type`) == 1) self$`type` <- `type` } @@ -82,6 +85,9 @@ Zebra <- R6::R6Class( fromJSON = function(input_json) { this_object <- jsonlite::fromJSON(input_json) if (!is.null(this_object$`type`)) { + if (!is.null(this_object$`type`) && !(this_object$`type` %in% c("plains", "mountain", "grevys"))) { + stop(paste("Error! \"", this_object$`type`, "\" cannot be assigned to `type`. Must be \"plains\", \"mountain\", \"grevys\".", sep = "")) + } self$`type` <- this_object$`type` } if (!is.null(this_object$`className`)) { @@ -140,6 +146,9 @@ Zebra <- R6::R6Class( #' @export fromJSONString = function(input_json) { this_object <- jsonlite::fromJSON(input_json) + if (!is.null(this_object$`type`) && !(this_object$`type` %in% c("plains", "mountain", "grevys"))) { + stop(paste("Error! \"", this_object$`type`, "\" cannot be assigned to `type`. Must be \"plains\", \"mountain\", \"grevys\".", sep = "")) + } self$`type` <- this_object$`type` self$`className` <- this_object$`className` # process additional properties/fields in the payload