diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RClientCodegen.java index 087681bbe89..fd5d1a870e2 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RClientCodegen.java @@ -325,6 +325,11 @@ public class RClientCodegen extends DefaultCodegen implements CodegenConfig { return "item_" + name; } + if ("".equals(name)) { + LOGGER.warn("Empty item name `` (empty string) has been renamed to `empty_string` to avoid compilation errors."); + return "empty_string"; + } + // don't do anything as we'll put property name inside ` `, e.g. `date-time` return name; } @@ -454,6 +459,17 @@ public class RClientCodegen extends DefaultCodegen implements CodegenConfig { @Override public ModelsMap postProcessModels(ModelsMap objs) { + for (ModelMap mo : objs.getModels()) { + CodegenModel cm = mo.getModel(); + for (CodegenProperty var : cm.vars) { + // check to see if base name is an empty string + if ("".equals(var.baseName)) { + LOGGER.debug("Empty baseName `` (empty string) in the model `{}` has been renamed to `empty_string` to avoid compilation errors.", cm.classname); + var.baseName = "empty_string"; + } + } + } + // remove model imports to avoid error List> imports = objs.getImports(); final String prefix = modelPackage(); @@ -466,16 +482,15 @@ public class RClientCodegen extends DefaultCodegen implements CodegenConfig { // recursively add import for mapping one type to multiple imports List> recursiveImports = objs.getImports(); - if (recursiveImports == null) - return objs; - - ListIterator> listIterator = imports.listIterator(); - while (listIterator.hasNext()) { - String _import = listIterator.next().get("import"); - // if the import package happens to be found in the importMapping (key) - // add the corresponding import package to the list - if (importMapping.containsKey(_import)) { - listIterator.add(createMapping("import", importMapping.get(_import))); + if (recursiveImports != null) { + ListIterator> listIterator = imports.listIterator(); + while (listIterator.hasNext()) { + String _import = listIterator.next().get("import"); + // if the import package happens to be found in the importMapping (key) + // add the corresponding import package to the list + if (importMapping.containsKey(_import)) { + listIterator.add(createMapping("import", importMapping.get(_import))); + } } } diff --git a/modules/openapi-generator/src/test/resources/3_0/r/petstore.yaml b/modules/openapi-generator/src/test/resources/3_0/r/petstore.yaml index 03985dca6e4..22a80bb3148 100644 --- a/modules/openapi-generator/src/test/resources/3_0/r/petstore.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/r/petstore.yaml @@ -796,6 +796,8 @@ components: type: string super: type: string + "": + type: string Dog: allOf: - $ref: '#/components/schemas/Animal' diff --git a/samples/client/petstore/R/R/special.R b/samples/client/petstore/R/R/special.R index aa1db50f993..2f14118b02f 100644 --- a/samples/client/petstore/R/R/special.R +++ b/samples/client/petstore/R/R/special.R @@ -13,6 +13,7 @@ #' @field item_self integer [optional] #' @field item_private character [optional] #' @field item_super character [optional] +#' @field empty_string character [optional] #' @importFrom R6 R6Class #' @importFrom jsonlite fromJSON toJSON #' @export @@ -22,6 +23,7 @@ Special <- R6::R6Class( `item_self` = NULL, `item_private` = NULL, `item_super` = NULL, + `empty_string` = NULL, #' Initialize a new Special class. #' #' @description @@ -30,10 +32,11 @@ Special <- R6::R6Class( #' @param item_self item_self #' @param item_private item_private #' @param item_super item_super + #' @param empty_string empty_string #' @param ... Other optional arguments. #' @export initialize = function( - `item_self`=NULL, `item_private`=NULL, `item_super`=NULL, ... + `item_self`=NULL, `item_private`=NULL, `item_super`=NULL, `empty_string`=NULL, ... ) { if (!is.null(`item_self`)) { stopifnot(is.numeric(`item_self`), length(`item_self`) == 1) @@ -47,6 +50,10 @@ Special <- R6::R6Class( stopifnot(is.character(`item_super`), length(`item_super`) == 1) self$`item_super` <- `item_super` } + if (!is.null(`empty_string`)) { + stopifnot(is.character(`empty_string`), length(`empty_string`) == 1) + self$`empty_string` <- `empty_string` + } }, #' To JSON string #' @@ -69,6 +76,10 @@ Special <- R6::R6Class( SpecialObject[['super']] <- self$`item_super` } + if (!is.null(self$`empty_string`)) { + SpecialObject[['empty_string']] <- + self$`empty_string` + } SpecialObject }, @@ -91,6 +102,9 @@ Special <- R6::R6Class( if (!is.null(this_object$`super`)) { self$`item_super` <- this_object$`super` } + if (!is.null(this_object$`empty_string`)) { + self$`empty_string` <- this_object$`empty_string` + } self }, #' To JSON string @@ -122,6 +136,13 @@ Special <- R6::R6Class( "%s" ', self$`item_super` + )}, + if (!is.null(self$`empty_string`)) { + sprintf( + '"empty_string": + "%s" + ', + self$`empty_string` )} ) jsoncontent <- paste(jsoncontent, collapse = ",") @@ -140,6 +161,7 @@ Special <- R6::R6Class( self$`item_self` <- this_object$`item_self` self$`item_private` <- this_object$`item_private` self$`item_super` <- this_object$`item_super` + self$`empty_string` <- this_object$`empty_string` self }, #' Validate JSON input with respect to Special diff --git a/samples/client/petstore/R/docs/Special.md b/samples/client/petstore/R/docs/Special.md index d242562121f..9939ec7bde4 100644 --- a/samples/client/petstore/R/docs/Special.md +++ b/samples/client/petstore/R/docs/Special.md @@ -8,5 +8,6 @@ Name | Type | Description | Notes **item_self** | **integer** | | [optional] **item_private** | **character** | | [optional] **item_super** | **character** | | [optional] +**empty_string** | **character** | | [optional]