Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[R][Client] allow to initialize enum classes without parameters #18183

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading