Skip to content

Commit

Permalink
Fix name normalization function
Browse files Browse the repository at this point in the history
Name normalization should differentiate between class names and property
names to avoid name collisions and resulted compilation errors (issue
joelittlejohn#129)
  • Loading branch information
khitrenovich committed Oct 22, 2013
1 parent a473700 commit 3ff60db
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ private void addGeneratedAnnotation(JDefinedClass jclass) {
}

private String getEnumName(String nodeName) {
String className = ruleFactory.getNameHelper().replaceIllegalCharacters(capitalize(nodeName));
return ruleFactory.getNameHelper().normalizeName(className);
String className = ruleFactory.getNameHelper().replaceIllegalCharacters(nodeName);
return ruleFactory.getNameHelper().normalizeName(className, true);
}

private String getConstantName(String nodeName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,19 @@ public String replaceIllegalCharacters(String name) {
return name.replaceAll(ILLEGAL_CHARACTER_REGEX, "_");
}

public String normalizeName(String name) {
public String normalizeName(String name, boolean isClass) {
name = capitalizeTrailingWords(name);

if (isDigit(name.charAt(0))) {
name = "_" + name;
}

// Class names should start with uppercase letters,
// while property names should start with lovercase (see issue #129)
name =
(isClass ? toUpperCase(name.charAt(0)) : toLowerCase(name.charAt(0)))
+ (name.length() > 1 ? name.substring(1) : "");

return name;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ private void addInterfaces(JDefinedClass jclass, JsonNode javaInterfaces) {
}

private String getClassName(String nodeName, JPackage _package) {
String className = ruleFactory.getNameHelper().replaceIllegalCharacters(capitalize(nodeName));
String normalizedName = ruleFactory.getNameHelper().normalizeName(className);
String className = ruleFactory.getNameHelper().replaceIllegalCharacters(nodeName);
String normalizedName = ruleFactory.getNameHelper().normalizeName(className, true);
return makeUnique(normalizedName, _package);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private JMethod addBuilder(JDefinedClass c, JFieldVar field) {

private String getPropertyName(String nodeName) {
nodeName = ruleFactory.getNameHelper().replaceIllegalCharacters(nodeName);
nodeName = ruleFactory.getNameHelper().normalizeName(nodeName);
nodeName = ruleFactory.getNameHelper().normalizeName(nodeName, false);

if (isKeyword(nodeName)) {
nodeName = "_" + nodeName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,15 @@ public void enumWithNullValue() throws ClassNotFoundException, NoSuchMethodExcep

}

@Test
public void enumWithUppercaseProperty() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {

ClassLoader resultsClassLoader = generateAndCompile("/schema/enum/enumWithUppercaseProperty.json", "com.example");

resultsClassLoader.loadClass("com.example.EnumWithUppercaseProperty");
resultsClassLoader.loadClass("com.example.EnumWithUppercaseProperty$TimeFormat");
}

@Test
@SuppressWarnings("unchecked")
public void jacksonCanMarshalEnums() throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException, IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type" : "object",
"properties" : {
"TimeFormat" : {
"enum" : ["12h", "24h"]
}
}
}

0 comments on commit 3ff60db

Please sign in to comment.