jsonschema2pojo icon indicating copy to clipboard operation
jsonschema2pojo copied to clipboard

Accessor methods for properties starting with a lowercase letter and followed by an uppercase letter

Open rinat11011 opened this issue 3 years ago • 3 comments
trafficstars

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!

rinat11011 avatar Feb 23 '22 17:02 rinat11011

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.

joelittlejohn avatar Feb 23 '22 19:02 joelittlejohn

Thank you for the quick response :) In this case, is there a way to manually name the accessor methods?

rinat11011 avatar Feb 24 '22 09:02 rinat11011

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

  1. NameHelper would need to be subclassed with desired methods being overridden eg. getGetterName, getSetterName, ...
  2. custom RuleFactory would have to be created/used
  3. RuleFactory::setGenerationConfig would need to be overridden, such that it would redefine nameHelper field value to point at custom NameHelper
    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.

unkish avatar Feb 25 '22 12:02 unkish