Skip to content

Commit

Permalink
Fix edge case of getter/setter naming when second char is uppercase
Browse files Browse the repository at this point in the history
A little known part of the been spec, since a getter/setter with a
capital letter in second position indicates that the first char is also
a capital. If the first char is in fact not a capital, then it needs to
be lowercase in the getter/setter also.

See also:

* https://stackoverflow.com/questions/8969112/confused-about-naming-of-javabean-properties-with-respect-to-getters-and-setter
* FasterXML/jackson-databind#653
* https://bugs.eclipse.org/bugs/show_bug.cgi?id=322223

Fixes #756
  • Loading branch information
joelittlejohn committed Feb 4, 2018
1 parent da3e8e2 commit e86db10
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,16 @@ public String getPropertyName(String jsonFieldName, JsonNode node) {
* @return
*/
public String getSetterName(String propertyName, JsonNode node) {
propertyName = getFieldName(propertyName, node);

propertyName = replaceIllegalCharacters(propertyName);
String setterName = "set" + capitalize(capitalizeTrailingWords(propertyName));
propertyName = getPropertyNameForAccessor(propertyName, node);

String prefix = "set";

String setterName;
if (propertyName.length() > 1 && Character.isUpperCase(propertyName.charAt(1))) {
setterName = prefix + propertyName;
} else {
setterName = prefix + capitalize(propertyName);
}

if (setterName.equals("setClass")) {
setterName = "setClass_";
Expand Down Expand Up @@ -140,16 +146,28 @@ public String getFieldName(String propertyName, JsonNode node) {
* @return
*/
public String getGetterName(String propertyName, JType type, JsonNode node) {
propertyName = getFieldName(propertyName, node);

propertyName = getPropertyNameForAccessor(propertyName, node);
String prefix = type.equals(type.owner()._ref(boolean.class)) ? "is" : "get";
propertyName = replaceIllegalCharacters(propertyName);
String getterName = prefix + capitalize(capitalizeTrailingWords(propertyName));

String getterName;
if (propertyName.length() > 1 && Character.isUpperCase(propertyName.charAt(1))) {
getterName = prefix + propertyName;
} else {
getterName = prefix + capitalize(propertyName);
}

if (getterName.equals("getClass")) {
getterName = "getClass_";
}

return getterName;
}

private String getPropertyNameForAccessor(String jsonPropertyName, JsonNode node) {
jsonPropertyName = getFieldName(jsonPropertyName, node);
jsonPropertyName = replaceIllegalCharacters(jsonPropertyName);
jsonPropertyName = capitalizeTrailingWords(jsonPropertyName);
return jsonPropertyName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Copyright © 2010-2017 Nokia
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jsonschema2pojo.util;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

import org.jsonschema2pojo.DefaultGenerationConfig;
import org.junit.Test;

import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.sun.codemodel.JCodeModel;

public class NameHelperTest {

private static final ObjectNode NODE = JsonNodeFactory.instance.objectNode();

private final NameHelper nameHelper = new NameHelper(new DefaultGenerationConfig());

@Test
public void testGetterNamedCorrectly() {
assertThat(nameHelper.getGetterName("foo", new JCodeModel().BOOLEAN, NODE), is("isFoo"));
assertThat(nameHelper.getGetterName("foo", new JCodeModel().INT, NODE), is("getFoo"));
assertThat(nameHelper.getGetterName("oAuth2State", new JCodeModel().INT, NODE), is("getoAuth2State"));
assertThat(nameHelper.getGetterName("URL", new JCodeModel().INT, NODE), is("getURL"));
}

@Test
public void testSetterNamedCorrectly() {
assertThat(nameHelper.getSetterName("foo", NODE), is("setFoo"));
assertThat(nameHelper.getSetterName("oAuth2State", NODE), is("setoAuth2State"));
assertThat(nameHelper.getSetterName("URL", NODE), is("setURL"));
}

}

0 comments on commit e86db10

Please sign in to comment.