[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;
}
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<Map<String, String>> 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<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)));
}
}
}

View File

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

View File

@ -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

View File

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