From 54248ac90d8cb40ed15ea1f1ff8e7b76539dd1b1 Mon Sep 17 00:00:00 2001 From: Alex Macrae Date: Wed, 11 May 2016 19:54:10 +0100 Subject: [PATCH] Fixed created property names being upper camel case. Added integration test with schema in propertiesIT. --- .../org/jsonschema2pojo/util/NameHelper.java | 5 ++++ .../integration/PropertiesIT.java | 25 +++++++++++++++++++ .../propertiesAreUpperCamelCase.json | 18 +++++++++++++ .../test/resources/schema/ref/httpRefs.json | 4 +-- 4 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 jsonschema2pojo-integration-tests/src/test/resources/schema/properties/propertiesAreUpperCamelCase.json diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/util/NameHelper.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/util/NameHelper.java index 79c1223cb..e1dbcd034 100644 --- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/util/NameHelper.java +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/util/NameHelper.java @@ -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. @@ -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; diff --git a/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/PropertiesIT.java b/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/PropertiesIT.java index 60ac12283..9d8453bf3 100644 --- a/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/PropertiesIT.java +++ b/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/PropertiesIT.java @@ -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)); + } } diff --git a/jsonschema2pojo-integration-tests/src/test/resources/schema/properties/propertiesAreUpperCamelCase.json b/jsonschema2pojo-integration-tests/src/test/resources/schema/properties/propertiesAreUpperCamelCase.json new file mode 100644 index 000000000..69681f19b --- /dev/null +++ b/jsonschema2pojo-integration-tests/src/test/resources/schema/properties/propertiesAreUpperCamelCase.json @@ -0,0 +1,18 @@ +{ + "type" : "object", + "javaType" : "com.example.UpperCase", + "properties" : { + "Property1" : { + "type" : "string" + }, + "PropertyTwo" : { + "type" : "integer" + }, + " PropertyThreeWithSpace" : { + "type" : "string" + }, + "propertyFour" : { + "type" : "string" + } + } +} \ No newline at end of file diff --git a/jsonschema2pojo-integration-tests/src/test/resources/schema/ref/httpRefs.json b/jsonschema2pojo-integration-tests/src/test/resources/schema/ref/httpRefs.json index b532a1105..45d24f44a 100644 --- a/jsonschema2pojo-integration-tests/src/test/resources/schema/ref/httpRefs.json +++ b/jsonschema2pojo-integration-tests/src/test/resources/schema/ref/httpRefs.json @@ -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" } } } \ No newline at end of file