From 04abd337906e70df594daaf5beed811f397e6f72 Mon Sep 17 00:00:00 2001
From: William Cheng <wing328hk@gmail.com>
Date: Mon, 27 Jun 2022 16:06:40 +0800
Subject: [PATCH 1/2] fix empty variable name, fix post process model

---
 .../codegen/languages/RClientCodegen.java     | 24 +++++++++++--------
 .../src/test/resources/3_0/r/petstore.yaml    |  2 ++
 samples/client/petstore/R/R/special.R         | 24 ++++++++++++++++++-
 samples/client/petstore/R/docs/Special.md     |  1 +
 4 files changed, 40 insertions(+), 11 deletions(-)

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..f0d6d3f95d6 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;
     }
@@ -466,16 +471,15 @@ public class RClientCodegen extends DefaultCodegen implements CodegenConfig {
 
         // recursively add import for mapping one type to multiple imports
         List<Map<String, String>> recursiveImports = objs.getImports();
-        if (recursiveImports == null)
-            return objs;
-
-        ListIterator<Map<String, String>> 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<Map<String, String>> 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 2dd59b39e51..f0940dd9d66 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[['']] <-
+          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$``)) {
+        self$`empty_string` <- this_object$``
+      }
       self
     },
     #' To JSON string
@@ -122,6 +136,13 @@ Special <- R6::R6Class(
           "%s"
                 ',
         self$`item_super`
+        )},
+        if (!is.null(self$`empty_string`)) {
+        sprintf(
+        '"":
+          "%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] 
 
 
-- 
GitLab


From c0ab822415903bed0de3f0f0f5ea2671383cb0cc Mon Sep 17 00:00:00 2001
From: William Cheng <wing328hk@gmail.com>
Date: Mon, 27 Jun 2022 16:30:55 +0800
Subject: [PATCH 2/2] fix empty base name

---
 .../codegen/languages/RClientCodegen.java             | 11 +++++++++++
 samples/client/petstore/R/R/special.R                 |  8 ++++----
 2 files changed, 15 insertions(+), 4 deletions(-)

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 f0d6d3f95d6..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
@@ -459,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<Map<String, String>> imports = objs.getImports();
         final String prefix = modelPackage();
diff --git a/samples/client/petstore/R/R/special.R b/samples/client/petstore/R/R/special.R
index f0940dd9d66..f95632a5baa 100644
--- a/samples/client/petstore/R/R/special.R
+++ b/samples/client/petstore/R/R/special.R
@@ -77,7 +77,7 @@ Special <- R6::R6Class(
           self$`item_super`
       }
       if (!is.null(self$`empty_string`)) {
-        SpecialObject[['']] <-
+        SpecialObject[['empty_string']] <-
           self$`empty_string`
       }
 
@@ -102,8 +102,8 @@ Special <- R6::R6Class(
       if (!is.null(this_object$`super`)) {
         self$`item_super` <- this_object$`super`
       }
-      if (!is.null(this_object$``)) {
-        self$`empty_string` <- this_object$``
+      if (!is.null(this_object$`empty_string`)) {
+        self$`empty_string` <- this_object$`empty_string`
       }
       self
     },
@@ -139,7 +139,7 @@ Special <- R6::R6Class(
         )},
         if (!is.null(self$`empty_string`)) {
         sprintf(
-        '"":
+        '"empty_string":
           "%s"
                 ',
         self$`empty_string`
-- 
GitLab