Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

Commit e146afb

Browse files
authored
better enum status validation (#13486)
1 parent 5f9910d commit e146afb

File tree

11 files changed

+113
-0
lines changed

11 files changed

+113
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@
5252
{{#requiredVars}}
5353
if (!missing(`{{name}}`)) {
5454
{{^isContainer}}
55+
{{#isEnum}}
56+
if (!(`{{name}}` %in% c({{#_enum}}"{{{.}}}"{{^-last}}, {{/-last}}{{/_enum}}))) {
57+
stop(paste("Error! \"", `{{name}}`, "\" cannot be assigned to `{{name}}`. Must be {{#_enum}}\"{{{.}}}\"{{^-last}}, {{/-last}}{{/_enum}}.", sep = ""))
58+
}
59+
{{/isEnum}}
5560
{{#isInteger}}
5661
stopifnot(is.numeric(`{{name}}`), length(`{{name}}`) == 1)
5762
{{/isInteger}}
@@ -97,6 +102,11 @@
97102
{{#optionalVars}}
98103
if (!is.null(`{{name}}`)) {
99104
{{^isContainer}}
105+
{{#isEnum}}
106+
if (!(`{{name}}` %in% c({{#_enum}}"{{{.}}}"{{^-last}}, {{/-last}}{{/_enum}}))) {
107+
stop(paste("Error! \"", `{{name}}`, "\" cannot be assigned to `{{name}}`. Must be {{#_enum}}\"{{{.}}}\"{{^-last}}, {{/-last}}{{/_enum}}.", sep = ""))
108+
}
109+
{{/isEnum}}
100110
{{#isInteger}}
101111
stopifnot(is.numeric(`{{name}}`), length(`{{name}}`) == 1)
102112
{{/isInteger}}
@@ -219,6 +229,11 @@
219229
{{/isContainer}}
220230
{{^isContainer}}
221231
{{#isPrimitiveType}}
232+
{{#isEnum}}
233+
if (!is.null(this_object$`{{baseName}}`) && !(this_object$`{{baseName}}` %in% c({{#_enum}}"{{{.}}}"{{^-last}}, {{/-last}}{{/_enum}}))) {
234+
stop(paste("Error! \"", this_object$`{{baseName}}`, "\" cannot be assigned to `{{baseName}}`. Must be {{#_enum}}\"{{{.}}}\"{{^-last}}, {{/-last}}{{/_enum}}.", sep = ""))
235+
}
236+
{{/isEnum}}
222237
self$`{{name}}` <- this_object$`{{baseName}}`
223238
{{/isPrimitiveType}}
224239
{{^isPrimitiveType}}
@@ -340,6 +355,11 @@
340355
{{/isContainer}}
341356
{{^isContainer}}
342357
{{#isPrimitiveType}}
358+
{{#isEnum}}
359+
if (!is.null(this_object$`{{baseName}}`) && !(this_object$`{{baseName}}` %in% c({{#_enum}}"{{{.}}}"{{^-last}}, {{/-last}}{{/_enum}}))) {
360+
stop(paste("Error! \"", this_object$`{{baseName}}`, "\" cannot be assigned to `{{baseName}}`. Must be {{#_enum}}\"{{{.}}}\"{{^-last}}, {{/-last}}{{/_enum}}.", sep = ""))
361+
}
362+
{{/isEnum}}
343363
self$`{{name}}` <- this_object$`{{name}}`
344364
{{/isPrimitiveType}}
345365
{{^isPrimitiveType}}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ Order <- R6::R6Class(
6363
self$`shipDate` <- `shipDate`
6464
}
6565
if (!is.null(`status`)) {
66+
if (!(`status` %in% c("placed", "approved", "delivered"))) {
67+
stop(paste("Error! \"", `status`, "\" cannot be assigned to `status`. Must be \"placed\", \"approved\", \"delivered\".", sep = ""))
68+
}
6669
stopifnot(is.character(`status`), length(`status`) == 1)
6770
self$`status` <- `status`
6871
}
@@ -138,6 +141,9 @@ Order <- R6::R6Class(
138141
self$`shipDate` <- this_object$`shipDate`
139142
}
140143
if (!is.null(this_object$`status`)) {
144+
if (!is.null(this_object$`status`) && !(this_object$`status` %in% c("placed", "approved", "delivered"))) {
145+
stop(paste("Error! \"", this_object$`status`, "\" cannot be assigned to `status`. Must be \"placed\", \"approved\", \"delivered\".", sep = ""))
146+
}
141147
self$`status` <- this_object$`status`
142148
}
143149
if (!is.null(this_object$`complete`)) {
@@ -232,6 +238,9 @@ Order <- R6::R6Class(
232238
self$`petId` <- this_object$`petId`
233239
self$`quantity` <- this_object$`quantity`
234240
self$`shipDate` <- this_object$`shipDate`
241+
if (!is.null(this_object$`status`) && !(this_object$`status` %in% c("placed", "approved", "delivered"))) {
242+
stop(paste("Error! \"", this_object$`status`, "\" cannot be assigned to `status`. Must be \"placed\", \"approved\", \"delivered\".", sep = ""))
243+
}
235244
self$`status` <- this_object$`status`
236245
self$`complete` <- this_object$`complete`
237246
# process additional properties/fields in the payload

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ Pet <- R6::R6Class(
6969
self$`tags` <- `tags`
7070
}
7171
if (!is.null(`status`)) {
72+
if (!(`status` %in% c("available", "pending", "sold"))) {
73+
stop(paste("Error! \"", `status`, "\" cannot be assigned to `status`. Must be \"available\", \"pending\", \"sold\".", sep = ""))
74+
}
7275
stopifnot(is.character(`status`), length(`status`) == 1)
7376
self$`status` <- `status`
7477
}
@@ -145,6 +148,9 @@ Pet <- R6::R6Class(
145148
self$`tags` <- ApiClient$new()$deserializeObj(this_object$`tags`, "array[Tag]", loadNamespace("petstore"))
146149
}
147150
if (!is.null(this_object$`status`)) {
151+
if (!is.null(this_object$`status`) && !(this_object$`status` %in% c("available", "pending", "sold"))) {
152+
stop(paste("Error! \"", this_object$`status`, "\" cannot be assigned to `status`. Must be \"available\", \"pending\", \"sold\".", sep = ""))
153+
}
148154
self$`status` <- this_object$`status`
149155
}
150156
# process additional properties/fields in the payload
@@ -237,6 +243,9 @@ Pet <- R6::R6Class(
237243
self$`name` <- this_object$`name`
238244
self$`photoUrls` <- ApiClient$new()$deserializeObj(this_object$`photoUrls`, "array[character]", loadNamespace("petstore"))
239245
self$`tags` <- ApiClient$new()$deserializeObj(this_object$`tags`, "array[Tag]", loadNamespace("petstore"))
246+
if (!is.null(this_object$`status`) && !(this_object$`status` %in% c("available", "pending", "sold"))) {
247+
stop(paste("Error! \"", this_object$`status`, "\" cannot be assigned to `status`. Must be \"available\", \"pending\", \"sold\".", sep = ""))
248+
}
240249
self$`status` <- this_object$`status`
241250
# process additional properties/fields in the payload
242251
for (key in names(this_object)) {

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ Zebra <- R6::R6Class(
3939
self$`className` <- `className`
4040
}
4141
if (!is.null(`type`)) {
42+
if (!(`type` %in% c("plains", "mountain", "grevys"))) {
43+
stop(paste("Error! \"", `type`, "\" cannot be assigned to `type`. Must be \"plains\", \"mountain\", \"grevys\".", sep = ""))
44+
}
4245
stopifnot(is.character(`type`), length(`type`) == 1)
4346
self$`type` <- `type`
4447
}
@@ -82,6 +85,9 @@ Zebra <- R6::R6Class(
8285
fromJSON = function(input_json) {
8386
this_object <- jsonlite::fromJSON(input_json)
8487
if (!is.null(this_object$`type`)) {
88+
if (!is.null(this_object$`type`) && !(this_object$`type` %in% c("plains", "mountain", "grevys"))) {
89+
stop(paste("Error! \"", this_object$`type`, "\" cannot be assigned to `type`. Must be \"plains\", \"mountain\", \"grevys\".", sep = ""))
90+
}
8591
self$`type` <- this_object$`type`
8692
}
8793
if (!is.null(this_object$`className`)) {
@@ -140,6 +146,9 @@ Zebra <- R6::R6Class(
140146
#' @export
141147
fromJSONString = function(input_json) {
142148
this_object <- jsonlite::fromJSON(input_json)
149+
if (!is.null(this_object$`type`) && !(this_object$`type` %in% c("plains", "mountain", "grevys"))) {
150+
stop(paste("Error! \"", this_object$`type`, "\" cannot be assigned to `type`. Must be \"plains\", \"mountain\", \"grevys\".", sep = ""))
151+
}
143152
self$`type` <- this_object$`type`
144153
self$`className` <- this_object$`className`
145154
# process additional properties/fields in the payload

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ pet_api$api_client$username <- "username123"
2121
pet_api$api_client$password <- "password123"
2222
result <- pet_api$add_pet(pet)
2323

24+
test_that("Invalid enum value test", {
25+
expect_error(Pet$new("name_test",
26+
photoUrls = list("photo_test", "second test"),
27+
category = Category$new(id = 450, name = "test_cat"),
28+
id = pet_id,
29+
tags = list(
30+
Tag$new(id = 123, name = "tag_test"), Tag$new(id = 456, name = "unknown")
31+
),
32+
status = "error_available"
33+
), "Error! \"error_available\" cannot be assigned to `status`. Must be \"available\", \"pending\", \"sold\".")
34+
})
35+
2436
test_that("Additional Properties test", {
2537
# test tag
2638
t <- Tag$new(id = 393, name = "something")

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ Order <- R6::R6Class(
5858
self$`shipDate` <- `shipDate`
5959
}
6060
if (!is.null(`status`)) {
61+
if (!(`status` %in% c("placed", "approved", "delivered"))) {
62+
stop(paste("Error! \"", `status`, "\" cannot be assigned to `status`. Must be \"placed\", \"approved\", \"delivered\".", sep = ""))
63+
}
6164
stopifnot(is.character(`status`), length(`status`) == 1)
6265
self$`status` <- `status`
6366
}
@@ -124,6 +127,9 @@ Order <- R6::R6Class(
124127
self$`shipDate` <- this_object$`shipDate`
125128
}
126129
if (!is.null(this_object$`status`)) {
130+
if (!is.null(this_object$`status`) && !(this_object$`status` %in% c("placed", "approved", "delivered"))) {
131+
stop(paste("Error! \"", this_object$`status`, "\" cannot be assigned to `status`. Must be \"placed\", \"approved\", \"delivered\".", sep = ""))
132+
}
127133
self$`status` <- this_object$`status`
128134
}
129135
if (!is.null(this_object$`complete`)) {
@@ -206,6 +212,9 @@ Order <- R6::R6Class(
206212
self$`petId` <- this_object$`petId`
207213
self$`quantity` <- this_object$`quantity`
208214
self$`shipDate` <- this_object$`shipDate`
215+
if (!is.null(this_object$`status`) && !(this_object$`status` %in% c("placed", "approved", "delivered"))) {
216+
stop(paste("Error! \"", this_object$`status`, "\" cannot be assigned to `status`. Must be \"placed\", \"approved\", \"delivered\".", sep = ""))
217+
}
209218
self$`status` <- this_object$`status`
210219
self$`complete` <- this_object$`complete`
211220
self

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ Pet <- R6::R6Class(
6464
self$`tags` <- `tags`
6565
}
6666
if (!is.null(`status`)) {
67+
if (!(`status` %in% c("available", "pending", "sold"))) {
68+
stop(paste("Error! \"", `status`, "\" cannot be assigned to `status`. Must be \"available\", \"pending\", \"sold\".", sep = ""))
69+
}
6770
stopifnot(is.character(`status`), length(`status`) == 1)
6871
self$`status` <- `status`
6972
}
@@ -131,6 +134,9 @@ Pet <- R6::R6Class(
131134
self$`tags` <- ApiClient$new()$deserializeObj(this_object$`tags`, "array[Tag]", loadNamespace("petstore"))
132135
}
133136
if (!is.null(this_object$`status`)) {
137+
if (!is.null(this_object$`status`) && !(this_object$`status` %in% c("available", "pending", "sold"))) {
138+
stop(paste("Error! \"", this_object$`status`, "\" cannot be assigned to `status`. Must be \"available\", \"pending\", \"sold\".", sep = ""))
139+
}
134140
self$`status` <- this_object$`status`
135141
}
136142
self
@@ -211,6 +217,9 @@ Pet <- R6::R6Class(
211217
self$`name` <- this_object$`name`
212218
self$`photoUrls` <- ApiClient$new()$deserializeObj(this_object$`photoUrls`, "array[character]", loadNamespace("petstore"))
213219
self$`tags` <- ApiClient$new()$deserializeObj(this_object$`tags`, "array[Tag]", loadNamespace("petstore"))
220+
if (!is.null(this_object$`status`) && !(this_object$`status` %in% c("available", "pending", "sold"))) {
221+
stop(paste("Error! \"", this_object$`status`, "\" cannot be assigned to `status`. Must be \"available\", \"pending\", \"sold\".", sep = ""))
222+
}
214223
self$`status` <- this_object$`status`
215224
self
216225
},

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ Zebra <- R6::R6Class(
3434
self$`className` <- `className`
3535
}
3636
if (!is.null(`type`)) {
37+
if (!(`type` %in% c("plains", "mountain", "grevys"))) {
38+
stop(paste("Error! \"", `type`, "\" cannot be assigned to `type`. Must be \"plains\", \"mountain\", \"grevys\".", sep = ""))
39+
}
3740
stopifnot(is.character(`type`), length(`type`) == 1)
3841
self$`type` <- `type`
3942
}
@@ -68,6 +71,9 @@ Zebra <- R6::R6Class(
6871
fromJSON = function(input_json) {
6972
this_object <- jsonlite::fromJSON(input_json)
7073
if (!is.null(this_object$`type`)) {
74+
if (!is.null(this_object$`type`) && !(this_object$`type` %in% c("plains", "mountain", "grevys"))) {
75+
stop(paste("Error! \"", this_object$`type`, "\" cannot be assigned to `type`. Must be \"plains\", \"mountain\", \"grevys\".", sep = ""))
76+
}
7177
self$`type` <- this_object$`type`
7278
}
7379
if (!is.null(this_object$`className`)) {
@@ -114,6 +120,9 @@ Zebra <- R6::R6Class(
114120
#' @export
115121
fromJSONString = function(input_json) {
116122
this_object <- jsonlite::fromJSON(input_json)
123+
if (!is.null(this_object$`type`) && !(this_object$`type` %in% c("plains", "mountain", "grevys"))) {
124+
stop(paste("Error! \"", this_object$`type`, "\" cannot be assigned to `type`. Must be \"plains\", \"mountain\", \"grevys\".", sep = ""))
125+
}
117126
self$`type` <- this_object$`type`
118127
self$`className` <- this_object$`className`
119128
self

samples/client/petstore/R/R/order.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ Order <- R6::R6Class(
6363
self$`shipDate` <- `shipDate`
6464
}
6565
if (!is.null(`status`)) {
66+
if (!(`status` %in% c("placed", "approved", "delivered"))) {
67+
stop(paste("Error! \"", `status`, "\" cannot be assigned to `status`. Must be \"placed\", \"approved\", \"delivered\".", sep = ""))
68+
}
6669
stopifnot(is.character(`status`), length(`status`) == 1)
6770
self$`status` <- `status`
6871
}
@@ -138,6 +141,9 @@ Order <- R6::R6Class(
138141
self$`shipDate` <- this_object$`shipDate`
139142
}
140143
if (!is.null(this_object$`status`)) {
144+
if (!is.null(this_object$`status`) && !(this_object$`status` %in% c("placed", "approved", "delivered"))) {
145+
stop(paste("Error! \"", this_object$`status`, "\" cannot be assigned to `status`. Must be \"placed\", \"approved\", \"delivered\".", sep = ""))
146+
}
141147
self$`status` <- this_object$`status`
142148
}
143149
if (!is.null(this_object$`complete`)) {
@@ -232,6 +238,9 @@ Order <- R6::R6Class(
232238
self$`petId` <- this_object$`petId`
233239
self$`quantity` <- this_object$`quantity`
234240
self$`shipDate` <- this_object$`shipDate`
241+
if (!is.null(this_object$`status`) && !(this_object$`status` %in% c("placed", "approved", "delivered"))) {
242+
stop(paste("Error! \"", this_object$`status`, "\" cannot be assigned to `status`. Must be \"placed\", \"approved\", \"delivered\".", sep = ""))
243+
}
235244
self$`status` <- this_object$`status`
236245
self$`complete` <- this_object$`complete`
237246
# process additional properties/fields in the payload

samples/client/petstore/R/R/pet.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ Pet <- R6::R6Class(
6969
self$`tags` <- `tags`
7070
}
7171
if (!is.null(`status`)) {
72+
if (!(`status` %in% c("available", "pending", "sold"))) {
73+
stop(paste("Error! \"", `status`, "\" cannot be assigned to `status`. Must be \"available\", \"pending\", \"sold\".", sep = ""))
74+
}
7275
stopifnot(is.character(`status`), length(`status`) == 1)
7376
self$`status` <- `status`
7477
}
@@ -145,6 +148,9 @@ Pet <- R6::R6Class(
145148
self$`tags` <- ApiClient$new()$deserializeObj(this_object$`tags`, "array[Tag]", loadNamespace("petstore"))
146149
}
147150
if (!is.null(this_object$`status`)) {
151+
if (!is.null(this_object$`status`) && !(this_object$`status` %in% c("available", "pending", "sold"))) {
152+
stop(paste("Error! \"", this_object$`status`, "\" cannot be assigned to `status`. Must be \"available\", \"pending\", \"sold\".", sep = ""))
153+
}
148154
self$`status` <- this_object$`status`
149155
}
150156
# process additional properties/fields in the payload
@@ -237,6 +243,9 @@ Pet <- R6::R6Class(
237243
self$`name` <- this_object$`name`
238244
self$`photoUrls` <- ApiClient$new()$deserializeObj(this_object$`photoUrls`, "array[character]", loadNamespace("petstore"))
239245
self$`tags` <- ApiClient$new()$deserializeObj(this_object$`tags`, "array[Tag]", loadNamespace("petstore"))
246+
if (!is.null(this_object$`status`) && !(this_object$`status` %in% c("available", "pending", "sold"))) {
247+
stop(paste("Error! \"", this_object$`status`, "\" cannot be assigned to `status`. Must be \"available\", \"pending\", \"sold\".", sep = ""))
248+
}
240249
self$`status` <- this_object$`status`
241250
# process additional properties/fields in the payload
242251
for (key in names(this_object)) {

0 commit comments

Comments
 (0)