Skip to content

Commit

Permalink
[cpp] Sanitize identifier names (#631)
Browse files Browse the repository at this point in the history
* [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.
  • Loading branch information
Jauler authored and wing328 committed Jul 28, 2018
1 parent b33f1f9 commit 0b88889
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 160 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,38 @@ public AbstractCppCodegen() {
);
}

@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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,6 @@ public void processOpts() {
}
}

/**
* 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)) {
Expand Down Expand Up @@ -392,33 +380,6 @@ public String getSchemaType(Schema p) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,20 +251,6 @@ public String toModelImport(String name) {
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
Expand Down Expand Up @@ -378,25 +364,6 @@ public String getSchemaType(Schema p) {
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
Expand Down Expand Up @@ -424,22 +391,6 @@ public String toParamName(String name) {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public CppRestSdkClientCodegen() {
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>");
Expand Down Expand Up @@ -200,6 +200,7 @@ public void processOpts() {
* Location to write model files. You can use the modelPackage() as defined
* when the class is instantiated
*/
@Override
public String modelFileFolder() {
return outputFolder + "/model";
}
Expand All @@ -218,7 +219,7 @@ public String toModelImport(String name) {
if (importMapping.containsKey(name)) {
return importMapping.get(name);
} else {
return "#include \"" + name + ".h\"";
return "#include \"" + sanitizeName(name) + ".h\"";
}
}

Expand Down Expand Up @@ -281,12 +282,12 @@ protected boolean isFileSchema(CodegenProperty property) {

@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");
}

/**
Expand Down Expand Up @@ -388,33 +389,6 @@ public String getSchemaType(Schema p) {
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) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,6 @@ public void processOpts() {
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
Expand Down Expand Up @@ -361,33 +349,4 @@ public String getSchemaType(Schema p) {
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("/*", "/_*");
}


}

0 comments on commit 0b88889

Please sign in to comment.