[cpp] Sanitize identifier names (#631)

* [cpp] Sanitize identifier names

* Remove duplicated methods in cpp code generator subclasses.

* Fix unintended codegen differences in cpp tizen caused by it not extending AbstractCppCodegen class.
This commit is contained in:
Rytis Karpuška
2018-07-28 10:23:35 +03:00
committed by William Cheng
parent b33f1f945b
commit 0b88889cdf
5 changed files with 37 additions and 160 deletions

View File

@@ -128,6 +128,38 @@ abstract public class AbstractCppCodegen extends DefaultCodegen implements Codeg
);
}
@Override
public String escapeQuotationMark(String input) {
// remove " to avoid code injection
return input.replace("\"", "");
}
@Override
public String escapeUnsafeCharacters(String input) {
return input.replace("*/", "*_/").replace("/*", "/_*");
}
@Override
public String toApiName(String type) {
return sanitizeName(modelNamePrefix + Character.toUpperCase(type.charAt(0)) + type.substring(1) + "Api");
}
@Override
public String toModelName(String type) {
if (type == null) {
LOGGER.warn("Model name can't be null. Default to 'UnknownModel'.");
type = "UnknownModel";
}
if (typeMapping.keySet().contains(type) || typeMapping.values().contains(type)
|| importMapping.values().contains(type) || defaultIncludes.contains(type)
|| languageSpecificPrimitives.contains(type)) {
return type;
} else {
return sanitizeName(modelNamePrefix + Character.toUpperCase(type.charAt(0)) + type.substring(1));
}
}
@Override
public String toVarName(String name) {
if (typeMapping.keySet().contains(name) || typeMapping.values().contains(name)

View File

@@ -129,18 +129,6 @@ public class CppPistacheServerCodegen extends AbstractCppCodegen {
}
}
/**
* Escapes a reserved word as defined in the `reservedWords` array. Handle
* escaping those terms here. This logic is only called if a variable
* matches the reserved words
*
* @return the escaped term
*/
@Override
public String escapeReservedWord(String name) {
return "_" + name; // add an underscore to the name
}
@Override
public String toModelImport(String name) {
if (importMapping.containsKey(name)) {
@@ -392,33 +380,6 @@ public class CppPistacheServerCodegen extends AbstractCppCodegen {
return toModelName(type);
}
@Override
public String toModelName(String type) {
if (typeMapping.keySet().contains(type) || typeMapping.values().contains(type)
|| importMapping.values().contains(type) || defaultIncludes.contains(type)
|| languageSpecificPrimitives.contains(type)) {
return type;
} else {
return Character.toUpperCase(type.charAt(0)) + type.substring(1);
}
}
@Override
public String toApiName(String type) {
return Character.toUpperCase(type.charAt(0)) + type.substring(1) + "Api";
}
@Override
public String escapeQuotationMark(String input) {
// remove " to avoid code injection
return input.replace("\"", "");
}
@Override
public String escapeUnsafeCharacters(String input) {
return input.replace("*/", "*_/").replace("/*", "/_*");
}
@Override
public String getTypeDeclaration(String str) {
return toModelName(str);

View File

@@ -251,20 +251,6 @@ public class CppQt5ClientCodegen extends AbstractCppCodegen implements CodegenCo
return "#include \"" + folder + toModelName(name) + ".h\"";
}
/**
* Escapes a reserved word as defined in the `reservedWords` array. Handle escaping
* those terms here. This logic is only called if a variable matches the reserved words
*
* @return the escaped term
*/
@Override
public String escapeReservedWord(String name) {
if (this.reservedWordsMappings().containsKey(name)) {
return this.reservedWordsMappings().get(name);
}
return "_" + name;
}
/**
* Location to write model files. You can use the modelPackage() as defined when the class is
* instantiated
@@ -378,25 +364,6 @@ public class CppQt5ClientCodegen extends AbstractCppCodegen implements CodegenCo
return toModelName(type);
}
@Override
public String toModelName(String type) {
if (type == null) {
LOGGER.warn("Model name can't be null. Default to 'UnknownModel'.");
type = "UnknownModel";
}
if (typeMapping.keySet().contains(type) ||
typeMapping.values().contains(type) ||
importMapping.values().contains(type) ||
defaultIncludes.contains(type) ||
languageSpecificPrimitives.contains(type)) {
return type;
} else {
String typeName = sanitizeName(type);
return modelNamePrefix + Character.toUpperCase(typeName.charAt(0)) + typeName.substring(1);
}
}
@Override
public String toVarName(String name) {
// sanitize name
@@ -424,22 +391,6 @@ public class CppQt5ClientCodegen extends AbstractCppCodegen implements CodegenCo
return toVarName(name);
}
@Override
public String toApiName(String type) {
return modelNamePrefix + Character.toUpperCase(type.charAt(0)) + type.substring(1) + "Api";
}
@Override
public String escapeQuotationMark(String input) {
// remove " to avoid code injection
return input.replace("\"", "");
}
@Override
public String escapeUnsafeCharacters(String input) {
return input.replace("*/", "*_/").replace("/*", "/_*");
}
public void setOptionalProjectFileFlag(boolean flag) {
this.optionalProjectFileFlag = flag;
}

View File

@@ -157,7 +157,7 @@ public class CppRestSdkClientCodegen extends AbstractCppCodegen {
typeMapping.put("binary", "std::string");
typeMapping.put("number", "double");
typeMapping.put("UUID", "utility::string_t");
typeMapping.put("ByteArray", "utility::string_t");
typeMapping.put("ByteArray", "utility::string_t");
super.importMapping = new HashMap<String, String>();
importMapping.put("std::vector", "#include <vector>");
@@ -200,6 +200,7 @@ public class CppRestSdkClientCodegen extends AbstractCppCodegen {
* Location to write model files. You can use the modelPackage() as defined
* when the class is instantiated
*/
@Override
public String modelFileFolder() {
return outputFolder + "/model";
}
@@ -218,7 +219,7 @@ public class CppRestSdkClientCodegen extends AbstractCppCodegen {
if (importMapping.containsKey(name)) {
return importMapping.get(name);
} else {
return "#include \"" + name + ".h\"";
return "#include \"" + sanitizeName(name) + ".h\"";
}
}
@@ -281,12 +282,12 @@ public class CppRestSdkClientCodegen extends AbstractCppCodegen {
@Override
public String toModelFilename(String name) {
return initialCaps(name);
return sanitizeName(initialCaps(name));
}
@Override
public String toApiFilename(String name) {
return initialCaps(name) + "Api";
return sanitizeName(initialCaps(name) + "Api");
}
/**
@@ -388,33 +389,6 @@ public class CppRestSdkClientCodegen extends AbstractCppCodegen {
return toModelName(type);
}
@Override
public String toModelName(String type) {
if (typeMapping.keySet().contains(type) || typeMapping.values().contains(type)
|| importMapping.values().contains(type) || defaultIncludes.contains(type)
|| languageSpecificPrimitives.contains(type)) {
return type;
} else {
return Character.toUpperCase(type.charAt(0)) + type.substring(1);
}
}
@Override
public String toApiName(String type) {
return Character.toUpperCase(type.charAt(0)) + type.substring(1) + "Api";
}
@Override
public String escapeQuotationMark(String input) {
// remove " to avoid code injection
return input.replace("\"", "");
}
@Override
public String escapeUnsafeCharacters(String input) {
return input.replace("*/", "*_/").replace("/*", "/_*");
}
@Override
public Map<String, Object> postProcessAllModels(final Map<String, Object> models) {

View File

@@ -148,18 +148,6 @@ public class CppRestbedServerCodegen extends AbstractCppCodegen {
additionalProperties.put("defaultInclude", defaultInclude);
}
/**
* Escapes a reserved word as defined in the `reservedWords` array. Handle
* escaping those terms here. This logic is only called if a variable
* matches the reserved words
*
* @return the escaped term
*/
@Override
public String escapeReservedWord(String name) {
return "_" + name; // add an underscore to the name
}
/**
* Location to write model files. You can use the modelPackage() as defined
* when the class is instantiated
@@ -361,33 +349,4 @@ public class CppRestbedServerCodegen extends AbstractCppCodegen {
type = openAPIType;
return toModelName(type);
}
@Override
public String toModelName(String type) {
if (typeMapping.keySet().contains(type) || typeMapping.values().contains(type)
|| importMapping.values().contains(type) || defaultIncludes.contains(type)
|| languageSpecificPrimitives.contains(type)) {
return type;
} else {
return Character.toUpperCase(type.charAt(0)) + type.substring(1);
}
}
@Override
public String toApiName(String type) {
return Character.toUpperCase(type.charAt(0)) + type.substring(1) + "Api";
}
@Override
public String escapeQuotationMark(String input) {
// remove " to avoid code injection
return input.replace("\"", "");
}
@Override
public String escapeUnsafeCharacters(String input) {
return input.replace("*/", "*_/").replace("/*", "/_*");
}
}