Skip to content

Commit

Permalink
[R][Client] allow to initialize enum classes without parameters (#18183)
Browse files Browse the repository at this point in the history
* allow empty initialization of enum classes

* regenerate samples
  • Loading branch information
joXemMx committed Mar 21, 2024
1 parent ed8b7ec commit 2600eb9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
16 changes: 13 additions & 3 deletions modules/openapi-generator/src/main/resources/r/modelEnum.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,21 @@
val <- unlist(local.optional.var)
enumvec <- .parse_{{name}}()
stopifnot(length(val) == 1L)
if (length(val) == 0L) {
val = "DUMMY_ENUM"
} else {
stopifnot(length(val) == 1L)
}
if (!val %in% enumvec)
if (!val %in% enumvec) {
if (!(val=="DUMMY_ENUM")) {
stop("Use one of the valid values: ",
paste0(enumvec, collapse = ", "))
paste0(enumvec, collapse = ", "))
}
warning("Initializing {{{classname}}} with DUMMY_ENUM. Use one of the valid values: ",
paste0(enumvec, collapse = ", "),
". If you did not manually initialize {{{classname}}}, this may already be overwritten by an enum loaded from a JSON config.")
}
private$value <- val
},
#' To JSON string
Expand Down
16 changes: 13 additions & 3 deletions samples/client/echo_api/r/R/string_enum_ref.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,21 @@ StringEnumRef <- R6::R6Class(
val <- unlist(local.optional.var)
enumvec <- .parse_StringEnumRef()

stopifnot(length(val) == 1L)
if (length(val) == 0L) {
val = "DUMMY_ENUM"
} else {
stopifnot(length(val) == 1L)
}

if (!val %in% enumvec)
if (!val %in% enumvec) {
if (!(val=="DUMMY_ENUM")) {
stop("Use one of the valid values: ",
paste0(enumvec, collapse = ", "))
paste0(enumvec, collapse = ", "))
}
warning("Initializing StringEnumRef with DUMMY_ENUM. Use one of the valid values: ",
paste0(enumvec, collapse = ", "),
". If you did not manually initialize StringEnumRef, this may already be overwritten by an enum loaded from a JSON config.")
}
private$value <- val
},
#' To JSON string
Expand Down

0 comments on commit 2600eb9

Please sign in to comment.