forked from loafle/openapi-generator-original
Fix issue 5793 (#5794)
* cast character to string. * Check if variable name is reversed. * Guard against reservedWordsMappings.
This commit is contained in:
@@ -9,12 +9,14 @@ import io.swagger.models.Operation;
|
||||
import io.swagger.models.Swagger;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class HaskellServantCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
|
||||
// source folder where to write the files
|
||||
protected String sourceFolder = "src";
|
||||
protected String apiVersion = "0.0.1";
|
||||
private static final Pattern LEADING_UNDERSCORE = Pattern.compile("^_+");
|
||||
|
||||
/**
|
||||
* Configures the type of generator.
|
||||
@@ -468,10 +470,20 @@ public class HaskellServantCodegen extends DefaultCodegen implements CodegenConf
|
||||
|
||||
private String fixOperatorChars(String string) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (char c : string.toCharArray()) {
|
||||
if (specialCharReplacements.containsKey(c)) {
|
||||
String name = string;
|
||||
//Check if it is a reserved word, in which case the underscore is added when property name is generated.
|
||||
if (string.startsWith("_")) {
|
||||
if (reservedWords.contains(string.substring(1, string.length()))) {
|
||||
name = string.substring(1, string.length());
|
||||
} else if (reservedWordsMappings.containsValue(string)) {
|
||||
name = LEADING_UNDERSCORE.matcher(string).replaceFirst("");
|
||||
}
|
||||
}
|
||||
for (char c : name.toCharArray()) {
|
||||
String cString = String.valueOf(c);
|
||||
if (specialCharReplacements.containsKey(cString)) {
|
||||
sb.append("'");
|
||||
sb.append(specialCharReplacements.get(c));
|
||||
sb.append(specialCharReplacements.get(cString));
|
||||
} else {
|
||||
sb.append(c);
|
||||
}
|
||||
@@ -498,7 +510,7 @@ public class HaskellServantCodegen extends DefaultCodegen implements CodegenConf
|
||||
// From the model name, compute the prefix for the fields.
|
||||
String prefix = camelize(model.classname, true);
|
||||
for(CodegenProperty prop : model.vars) {
|
||||
prop.name = prefix + camelize(fixOperatorChars(prop.name));
|
||||
prop.name = toVarName(prefix + camelize(fixOperatorChars(prop.name)));
|
||||
}
|
||||
|
||||
// Create newtypes for things with non-object types
|
||||
|
||||
Reference in New Issue
Block a user