42BV/beanmapper-spring-boot-starter

Add support for LogicSecuredCheck as Spring Component.

marcus-talbot42 opened this issue · 0 comments

Currently, it is not possible to let BeanMapper auto-config LogicSecuredCheck-instances if they do not have a unparameterised constructor. This is due to the following method, located in BeanMapperAutoConfig:

private void addLogicSecuredChecks(BeanMapperBuilder builder, String basePackage) {
    collectionHandlerAppScanner.findLogicSecuredCheckClasses(basePackage).forEach(cls -> {
        LogicSecuredCheck<?, ?> logicSecuredCheck = instantiateClassAppContextOptional(cls, "logic secured check");
        if (logicSecuredCheck != null) {
            builder.addLogicSecuredCheck(logicSecuredCheck);
        }
    });
}

The solution could look something like the following:

private void addLogicSecuredChecks(BeanMapperBuilder builder, String basePackage) {
    collectionHandlerAppScanner.findLogicSecuredCheckClasses(basePackage).forEach(cls -> {
        LogicSecuredCheck<?, ?> logicSecuredCheck = instantiateClassAppContextOptional(cls, "logic secured check");
        if (logicSecuredCheck == null) {
            try {
                logicSecuredCheck = this.applicationContext.getBean(cls);
            } catch (BeansException e) {
                log.error("Could not instantiate class [{}] as Spring Bean.", cls.getName());
                return;
            }
        }
        builder.addLogicSecuredCheck(logicSecuredCheck);
    });
}

The proposed solution has been off-handedly tested in the project where this issue was encountered, however, more rigorous tests should be written, and the proposed solution should be tested in existing projects, before release.

It should be noted that making LogicSecuredCheck-classes into Spring Components, or writing a bean definition for them, may lead to circular dependencies, if used improperly. That would be a user-error, not a bug.