Skip to content

Commit

Permalink
[cpp] Sanitize identifier names
Browse files Browse the repository at this point in the history
  • Loading branch information
Jauler committed Jul 24, 2018
1 parent d42ff75 commit f2694d6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,33 @@ 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(Character.toUpperCase(type.charAt(0)) + type.substring(1) + "Api");
}

@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 sanitizeName(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 @@ -153,7 +153,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 @@ -191,6 +191,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 @@ -209,7 +210,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 @@ -272,12 +273,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 @@ -379,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 Map<String, Object> postProcessAllModels(final Map<String, Object> models) {

Expand Down

0 comments on commit f2694d6

Please sign in to comment.