jsonschema2pojo
jsonschema2pojo copied to clipboard
Accessor methods for properties starting with a lowercase letter and followed by an uppercase letter
We have updated the jsonschema2pojo in our project from version 0.5 to 1.1.1, and we ran into an issue regarding the accessor methods naming.
Upon properties starting with a lowercase letter and followed by an uppercase letter, for example -> eDealerRef, the generated getter and setter in the current version are -> geteDealerRef() and seteDealerRef(), while in the previous version they were getEDealerRef() and setEDealerRef().
Since we have data relaying on the previous accessor methods naming, is there a quick fix we can use?
Thank you!
No quick fix I'm afraid. The accessor names you see in 1.1.1 are actually compliant with the Java bean spec. They're the correct names (because two capitals would indicate a property that starts with a capital, if I remember correctly).
Knowing that this would be a potential breaking change, it was enacted in the 1.0 release.
Thank you for the quick response :) In this case, is there a way to manually name the accessor methods?
I'm afraid there's none, as name is derived from property see for example NameHelper::getSetterName. It would be possible to "override" getter/setter names generated as one wishes, thought that won't be without effort. A somewhat "quick & dirty" example of such possibility based on CustomRuleFactoryIT
NameHelperwould need to be subclassed with desired methods being overridden eg.getGetterName,getSetterName, ...- custom
RuleFactorywould have to be created/used RuleFactory::setGenerationConfigwould need to be overridden, such that it would redefinenameHelperfield value to point at customNameHelper
public static class CustomNameHelper extends NameHelper {
public CustomNameHelper(GenerationConfig generationConfig) {
super(generationConfig);
}
@Override
public String getGetterName(String propertyName, JType type, JsonNode node) {
// Logic to "calculate" getter name goes here
return CALCULATED_GETTER_NAME_AS_STRING;
}
}
public static class TestRuleFactory extends RuleFactory {
@Override
public void setGenerationConfig(GenerationConfig generationConfig) {
super.setGenerationConfig(generationConfig);
try {
Field nameHelper = getClass().getSuperclass().getDeclaredField("nameHelper");
nameHelper.setAccessible(true);
nameHelper.set(this, new CustomNameHelper(generationConfig));
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
}
NB! Keep in mind that such "workarounds" are not guaranteed to work in any subsequent releases or work in all cases.