From 78f6ad01189f17b029828520c1826340bea61b4f Mon Sep 17 00:00:00 2001 From: Jonas Emmert <63006948+joXemMx@users.noreply.github.com> Date: Thu, 21 Mar 2024 04:21:41 +0100 Subject: [PATCH] [R][Client] allow to initialize enum classes without parameters (#18183) * allow empty initialization of enum classes * regenerate samples --- .../src/main/resources/r/modelEnum.mustache | 16 +++++++++++++--- samples/client/echo_api/r/R/string_enum_ref.R | 16 +++++++++++++--- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/r/modelEnum.mustache b/modules/openapi-generator/src/main/resources/r/modelEnum.mustache index 8471a94cad3c..266cd9f2a872 100644 --- a/modules/openapi-generator/src/main/resources/r/modelEnum.mustache +++ b/modules/openapi-generator/src/main/resources/r/modelEnum.mustache @@ -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 diff --git a/samples/client/echo_api/r/R/string_enum_ref.R b/samples/client/echo_api/r/R/string_enum_ref.R index d13f7fbf67aa..ea39a2a6e2c9 100644 --- a/samples/client/echo_api/r/R/string_enum_ref.R +++ b/samples/client/echo_api/r/R/string_enum_ref.R @@ -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