Skip to content

Commit 5cb66a8

Browse files
authored
[R] Fix empty variable name, fix post process model (#12708)
* fix empty variable name, fix post process model * fix empty base name
1 parent 9522f6d commit 5cb66a8

4 files changed

Lines changed: 51 additions & 11 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RClientCodegen.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,11 @@ public String toVarName(String name) {
325325
return "item_" + name;
326326
}
327327

328+
if ("".equals(name)) {
329+
LOGGER.warn("Empty item name `` (empty string) has been renamed to `empty_string` to avoid compilation errors.");
330+
return "empty_string";
331+
}
332+
328333
// don't do anything as we'll put property name inside ` `, e.g. `date-time`
329334
return name;
330335
}
@@ -454,6 +459,17 @@ public String toOperationId(String operationId) {
454459

455460
@Override
456461
public ModelsMap postProcessModels(ModelsMap objs) {
462+
for (ModelMap mo : objs.getModels()) {
463+
CodegenModel cm = mo.getModel();
464+
for (CodegenProperty var : cm.vars) {
465+
// check to see if base name is an empty string
466+
if ("".equals(var.baseName)) {
467+
LOGGER.debug("Empty baseName `` (empty string) in the model `{}` has been renamed to `empty_string` to avoid compilation errors.", cm.classname);
468+
var.baseName = "empty_string";
469+
}
470+
}
471+
}
472+
457473
// remove model imports to avoid error
458474
List<Map<String, String>> imports = objs.getImports();
459475
final String prefix = modelPackage();
@@ -466,16 +482,15 @@ public ModelsMap postProcessModels(ModelsMap objs) {
466482

467483
// recursively add import for mapping one type to multiple imports
468484
List<Map<String, String>> recursiveImports = objs.getImports();
469-
if (recursiveImports == null)
470-
return objs;
471-
472-
ListIterator<Map<String, String>> listIterator = imports.listIterator();
473-
while (listIterator.hasNext()) {
474-
String _import = listIterator.next().get("import");
475-
// if the import package happens to be found in the importMapping (key)
476-
// add the corresponding import package to the list
477-
if (importMapping.containsKey(_import)) {
478-
listIterator.add(createMapping("import", importMapping.get(_import)));
485+
if (recursiveImports != null) {
486+
ListIterator<Map<String, String>> listIterator = imports.listIterator();
487+
while (listIterator.hasNext()) {
488+
String _import = listIterator.next().get("import");
489+
// if the import package happens to be found in the importMapping (key)
490+
// add the corresponding import package to the list
491+
if (importMapping.containsKey(_import)) {
492+
listIterator.add(createMapping("import", importMapping.get(_import)));
493+
}
479494
}
480495
}
481496

modules/openapi-generator/src/test/resources/3_0/r/petstore.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,8 @@ components:
796796
type: string
797797
super:
798798
type: string
799+
"":
800+
type: string
799801
Dog:
800802
allOf:
801803
- $ref: '#/components/schemas/Animal'

samples/client/petstore/R/R/special.R

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#' @field item_self integer [optional]
1414
#' @field item_private character [optional]
1515
#' @field item_super character [optional]
16+
#' @field empty_string character [optional]
1617
#' @importFrom R6 R6Class
1718
#' @importFrom jsonlite fromJSON toJSON
1819
#' @export
@@ -22,6 +23,7 @@ Special <- R6::R6Class(
2223
`item_self` = NULL,
2324
`item_private` = NULL,
2425
`item_super` = NULL,
26+
`empty_string` = NULL,
2527
#' Initialize a new Special class.
2628
#'
2729
#' @description
@@ -30,10 +32,11 @@ Special <- R6::R6Class(
3032
#' @param item_self item_self
3133
#' @param item_private item_private
3234
#' @param item_super item_super
35+
#' @param empty_string empty_string
3336
#' @param ... Other optional arguments.
3437
#' @export
3538
initialize = function(
36-
`item_self`=NULL, `item_private`=NULL, `item_super`=NULL, ...
39+
`item_self`=NULL, `item_private`=NULL, `item_super`=NULL, `empty_string`=NULL, ...
3740
) {
3841
if (!is.null(`item_self`)) {
3942
stopifnot(is.numeric(`item_self`), length(`item_self`) == 1)
@@ -47,6 +50,10 @@ Special <- R6::R6Class(
4750
stopifnot(is.character(`item_super`), length(`item_super`) == 1)
4851
self$`item_super` <- `item_super`
4952
}
53+
if (!is.null(`empty_string`)) {
54+
stopifnot(is.character(`empty_string`), length(`empty_string`) == 1)
55+
self$`empty_string` <- `empty_string`
56+
}
5057
},
5158
#' To JSON string
5259
#'
@@ -69,6 +76,10 @@ Special <- R6::R6Class(
6976
SpecialObject[['super']] <-
7077
self$`item_super`
7178
}
79+
if (!is.null(self$`empty_string`)) {
80+
SpecialObject[['empty_string']] <-
81+
self$`empty_string`
82+
}
7283

7384
SpecialObject
7485
},
@@ -91,6 +102,9 @@ Special <- R6::R6Class(
91102
if (!is.null(this_object$`super`)) {
92103
self$`item_super` <- this_object$`super`
93104
}
105+
if (!is.null(this_object$`empty_string`)) {
106+
self$`empty_string` <- this_object$`empty_string`
107+
}
94108
self
95109
},
96110
#' To JSON string
@@ -122,6 +136,13 @@ Special <- R6::R6Class(
122136
"%s"
123137
',
124138
self$`item_super`
139+
)},
140+
if (!is.null(self$`empty_string`)) {
141+
sprintf(
142+
'"empty_string":
143+
"%s"
144+
',
145+
self$`empty_string`
125146
)}
126147
)
127148
jsoncontent <- paste(jsoncontent, collapse = ",")
@@ -140,6 +161,7 @@ Special <- R6::R6Class(
140161
self$`item_self` <- this_object$`item_self`
141162
self$`item_private` <- this_object$`item_private`
142163
self$`item_super` <- this_object$`item_super`
164+
self$`empty_string` <- this_object$`empty_string`
143165
self
144166
},
145167
#' Validate JSON input with respect to Special

samples/client/petstore/R/docs/Special.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ Name | Type | Description | Notes
88
**item_self** | **integer** | | [optional]
99
**item_private** | **character** | | [optional]
1010
**item_super** | **character** | | [optional]
11+
**empty_string** | **character** | | [optional]
1112

1213

0 commit comments

Comments
 (0)