Limit depth of example code generation in RClientCodegen (#9822)

This commit is contained in:
Kevyn Liebersbach
2021-06-23 13:29:48 +10:00
committed by GitHub
parent 969cea8ce1
commit 77f84ec811

View File

@@ -707,7 +707,7 @@ public class RClientCodegen extends DefaultCodegen implements CodegenConfig {
public String constructExampleCode(CodegenParameter codegenParameter, HashMap<String, CodegenModel> modelMaps) {
if (codegenParameter.isArray) { // array
return "list(" + constructExampleCode(codegenParameter.items, modelMaps) + ")";
return "list(" + constructExampleCode(codegenParameter.items, modelMaps, 0) + ")";
} else if (codegenParameter.isMap) { // TODO: map
return "TODO";
} else if (languageSpecificPrimitives.contains(codegenParameter.dataType)) { // primitive type
@@ -715,7 +715,7 @@ public class RClientCodegen extends DefaultCodegen implements CodegenConfig {
} else { // model
// look up the model
if (modelMaps.containsKey(codegenParameter.dataType)) {
return constructExampleCode(modelMaps.get(codegenParameter.dataType), modelMaps);
return constructExampleCode(modelMaps.get(codegenParameter.dataType), modelMaps, 0);
} else {
LOGGER.error("Error in constructing examples. Failed to look up the model {}", codegenParameter.dataType);
return "TODO";
@@ -723,9 +723,12 @@ public class RClientCodegen extends DefaultCodegen implements CodegenConfig {
}
}
public String constructExampleCode(CodegenProperty codegenProperty, HashMap<String, CodegenModel> modelMaps) {
public String constructExampleCode(CodegenProperty codegenProperty, HashMap<String, CodegenModel> modelMaps, int depth) {
if (depth > 10) return "...";
depth++;
if (codegenProperty.isArray) { // array
return "list(" + constructExampleCode(codegenProperty.items, modelMaps) + ")";
return "list(" + constructExampleCode(codegenProperty.items, modelMaps, depth) + ")";
} else if (codegenProperty.isMap) { // TODO: map
return "TODO";
} else if (languageSpecificPrimitives.contains(codegenProperty.dataType)) { // primitive type
@@ -749,7 +752,7 @@ public class RClientCodegen extends DefaultCodegen implements CodegenConfig {
} else {
// look up the model
if (modelMaps.containsKey(codegenProperty.dataType)) {
return constructExampleCode(modelMaps.get(codegenProperty.dataType), modelMaps);
return constructExampleCode(modelMaps.get(codegenProperty.dataType), modelMaps, depth);
} else {
LOGGER.error("Error in constructing examples. Failed to look up the model {}", codegenProperty.dataType);
return "TODO";
@@ -757,18 +760,21 @@ public class RClientCodegen extends DefaultCodegen implements CodegenConfig {
}
}
public String constructExampleCode(CodegenModel codegenModel, HashMap<String, CodegenModel> modelMaps) {
public String constructExampleCode(CodegenModel codegenModel, HashMap<String, CodegenModel> modelMaps, int depth) {
if (depth > 10) return "...";
depth++;
String example;
example = codegenModel.name + "$new(";
List<String> propertyExamples = new ArrayList<>();
// required properties first
for (CodegenProperty codegenProperty : codegenModel.requiredVars) {
propertyExamples.add(constructExampleCode(codegenProperty, modelMaps));
propertyExamples.add(constructExampleCode(codegenProperty, modelMaps, depth));
}
// optional properties second
for (CodegenProperty codegenProperty : codegenModel.optionalVars) {
propertyExamples.add(constructExampleCode(codegenProperty, modelMaps));
propertyExamples.add(constructExampleCode(codegenProperty, modelMaps, depth));
}
example += StringUtils.join(propertyExamples, ", ");