Skip to content

Commit

Permalink
Merge pull request #565 from MungoRae/fix-lower-camel-case
Browse files Browse the repository at this point in the history
Class fields are now created to be lower camel case. Added interation test.
  • Loading branch information
joelittlejohn committed May 11, 2016
2 parents e782e6a + 54248ac commit 4a8a504
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ public String capitalizeTrailingWords(String name) {
return name;
}

private String makeLowerCamelCase(String name) {
return Character.toLowerCase(name.charAt(0)) + name.substring(1);
}

/**
* Convert jsonFieldName into the equivalent Java fieldname by replacing
* illegal characters and normalizing it.
Expand All @@ -80,6 +84,7 @@ public String getPropertyName(String jsonFieldName, JsonNode node) {

jsonFieldName = replaceIllegalCharacters(jsonFieldName);
jsonFieldName = normalizeName(jsonFieldName);
jsonFieldName = makeLowerCamelCase(jsonFieldName);

if (isKeyword(jsonFieldName)) {
jsonFieldName = "_" + jsonFieldName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,29 @@ public void propertyCalledClassCanBeSerialized() throws ClassNotFoundException,
assertThat(valueAsJsonNode.path("class").asText(), is("a"));

}

@Test
public void propertyNamesAreLowerCamelCase() throws Exception {
ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/schema/properties/propertiesAreUpperCamelCase.json", "com.example");
Class<?> generatedType = resultsClassLoader.loadClass("com.example.UpperCase");

Object instance = generatedType.newInstance();

new PropertyDescriptor("property1", generatedType).getWriteMethod().invoke(instance, "1");
new PropertyDescriptor("propertyTwo", generatedType).getWriteMethod().invoke(instance, 2);
new PropertyDescriptor("propertyThreeWithSpace", generatedType).getWriteMethod().invoke(instance, "3");
new PropertyDescriptor("propertyFour", generatedType).getWriteMethod().invoke(instance, "4");

JsonNode jsonified = mapper.valueToTree(instance);

assertNotNull(generatedType.getDeclaredField("property1"));
assertNotNull(generatedType.getDeclaredField("propertyTwo"));
assertNotNull(generatedType.getDeclaredField("propertyThreeWithSpace"));
assertNotNull(generatedType.getDeclaredField("propertyFour"));

assertThat(jsonified.has("Property1"), is(true));
assertThat(jsonified.has("PropertyTwo"), is(true));
assertThat(jsonified.has(" PropertyThreeWithSpace"), is(true));
assertThat(jsonified.has("propertyFour"), is(true));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"type" : "object",
"javaType" : "com.example.UpperCase",
"properties" : {
"Property1" : {
"type" : "string"
},
"PropertyTwo" : {
"type" : "integer"
},
" PropertyThreeWithSpace" : {
"type" : "string"
},
"propertyFour" : {
"type" : "string"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"type" : "object",
"properties" : {
"address" : {
"$ref" : "https://jsonschema2pojo.googlecode.com/git-history/jsonschema2pojo-0.3.4/jsonschema2pojo-integration-tests/src/test/resources/schema/ref/address.json"
"$ref" : "https://raw.githubusercontent.com/joelittlejohn/jsonschema2pojo/master/jsonschema2pojo-integration-tests/src/test/resources/schema/ref/address.json"
},
"refsToA" : {
"$ref" : "https://jsonschema2pojo.googlecode.com/git-history/jsonschema2pojo-0.3.4/jsonschema2pojo-integration-tests/src/test/resources/schema/ref/refsToA.json"
"$ref" : "https://raw.githubusercontent.com/joelittlejohn/jsonschema2pojo/master/jsonschema2pojo-integration-tests/src/test/resources/schema/ref/refsToA.json"
}
}
}

0 comments on commit 4a8a504

Please sign in to comment.