[R] Fix empty variable name, fix post process model (#12708)

* fix empty variable name, fix post process model

* fix empty base name
This commit is contained in:
William Cheng 2022-06-27 19:26:51 +08:00 committed by GitHub
parent 9522f6d1e2
commit 5cb66a8ab0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 11 deletions

View File

@ -325,6 +325,11 @@ public class RClientCodegen extends DefaultCodegen implements CodegenConfig {
return "item_" + name; 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` // don't do anything as we'll put property name inside ` `, e.g. `date-time`
return name; return name;
} }
@ -454,6 +459,17 @@ public class RClientCodegen extends DefaultCodegen implements CodegenConfig {
@Override @Override
public ModelsMap postProcessModels(ModelsMap objs) { 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 // remove model imports to avoid error
List<Map<String, String>> imports = objs.getImports(); List<Map<String, String>> imports = objs.getImports();
final String prefix = modelPackage(); 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 // recursively add import for mapping one type to multiple imports
List<Map<String, String>> recursiveImports = objs.getImports(); List<Map<String, String>> recursiveImports = objs.getImports();
if (recursiveImports == null) if (recursiveImports != null) {
return objs; ListIterator<Map<String, String>> listIterator = imports.listIterator();
while (listIterator.hasNext()) {
ListIterator<Map<String, String>> listIterator = imports.listIterator(); String _import = listIterator.next().get("import");
while (listIterator.hasNext()) { // if the import package happens to be found in the importMapping (key)
String _import = listIterator.next().get("import"); // add the corresponding import package to the list
// if the import package happens to be found in the importMapping (key) if (importMapping.containsKey(_import)) {
// add the corresponding import package to the list listIterator.add(createMapping("import", importMapping.get(_import)));
if (importMapping.containsKey(_import)) { }
listIterator.add(createMapping("import", importMapping.get(_import)));
} }
} }

View File

@ -796,6 +796,8 @@ components:
type: string type: string
super: super:
type: string type: string
"":
type: string
Dog: Dog:
allOf: allOf:
- $ref: '#/components/schemas/Animal' - $ref: '#/components/schemas/Animal'

View File

@ -13,6 +13,7 @@
#' @field item_self integer [optional] #' @field item_self integer [optional]
#' @field item_private character [optional] #' @field item_private character [optional]
#' @field item_super character [optional] #' @field item_super character [optional]
#' @field empty_string character [optional]
#' @importFrom R6 R6Class #' @importFrom R6 R6Class
#' @importFrom jsonlite fromJSON toJSON #' @importFrom jsonlite fromJSON toJSON
#' @export #' @export
@ -22,6 +23,7 @@ Special <- R6::R6Class(
`item_self` = NULL, `item_self` = NULL,
`item_private` = NULL, `item_private` = NULL,
`item_super` = NULL, `item_super` = NULL,
`empty_string` = NULL,
#' Initialize a new Special class. #' Initialize a new Special class.
#' #'
#' @description #' @description
@ -30,10 +32,11 @@ Special <- R6::R6Class(
#' @param item_self item_self #' @param item_self item_self
#' @param item_private item_private #' @param item_private item_private
#' @param item_super item_super #' @param item_super item_super
#' @param empty_string empty_string
#' @param ... Other optional arguments. #' @param ... Other optional arguments.
#' @export #' @export
initialize = function( 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`)) { if (!is.null(`item_self`)) {
stopifnot(is.numeric(`item_self`), length(`item_self`) == 1) 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) stopifnot(is.character(`item_super`), length(`item_super`) == 1)
self$`item_super` <- `item_super` 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 #' To JSON string
#' #'
@ -69,6 +76,10 @@ Special <- R6::R6Class(
SpecialObject[['super']] <- SpecialObject[['super']] <-
self$`item_super` self$`item_super`
} }
if (!is.null(self$`empty_string`)) {
SpecialObject[['empty_string']] <-
self$`empty_string`
}
SpecialObject SpecialObject
}, },
@ -91,6 +102,9 @@ Special <- R6::R6Class(
if (!is.null(this_object$`super`)) { if (!is.null(this_object$`super`)) {
self$`item_super` <- this_object$`super` self$`item_super` <- this_object$`super`
} }
if (!is.null(this_object$`empty_string`)) {
self$`empty_string` <- this_object$`empty_string`
}
self self
}, },
#' To JSON string #' To JSON string
@ -122,6 +136,13 @@ Special <- R6::R6Class(
"%s" "%s"
', ',
self$`item_super` self$`item_super`
)},
if (!is.null(self$`empty_string`)) {
sprintf(
'"empty_string":
"%s"
',
self$`empty_string`
)} )}
) )
jsoncontent <- paste(jsoncontent, collapse = ",") jsoncontent <- paste(jsoncontent, collapse = ",")
@ -140,6 +161,7 @@ Special <- R6::R6Class(
self$`item_self` <- this_object$`item_self` self$`item_self` <- this_object$`item_self`
self$`item_private` <- this_object$`item_private` self$`item_private` <- this_object$`item_private`
self$`item_super` <- this_object$`item_super` self$`item_super` <- this_object$`item_super`
self$`empty_string` <- this_object$`empty_string`
self self
}, },
#' Validate JSON input with respect to Special #' Validate JSON input with respect to Special

View File

@ -8,5 +8,6 @@ Name | Type | Description | Notes
**item_self** | **integer** | | [optional] **item_self** | **integer** | | [optional]
**item_private** | **character** | | [optional] **item_private** | **character** | | [optional]
**item_super** | **character** | | [optional] **item_super** | **character** | | [optional]
**empty_string** | **character** | | [optional]