test empty = null for settings

This commit is contained in:
Anthony Stirling 2023-10-07 23:23:56 +01:00
parent 105d7f12ac
commit f4a01884bd
3 changed files with 68 additions and 0 deletions

View file

@ -1,5 +1,6 @@
package stirling.software.SPDF.config;
import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -8,6 +9,18 @@ import stirling.software.SPDF.model.ApplicationProperties;
@Configuration
public class AppConfig {
@Bean
public CustomEditorConfigurer customEditorConfigurer() {
return new CustomEditorConfigurer();
}
@Bean
public PropertyEditorRegistrar propertyEditorRegistrar() {
return registry -> {
registry.registerCustomEditor(String.class, new EmptyStringAsNullEditor());
};
}
@Autowired
ApplicationProperties applicationProperties;

View file

@ -0,0 +1,42 @@
package stirling.software.SPDF.config;
import java.beans.PropertyEditor;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.validation.DataBinder;
public class CustomEditorConfigurer implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof PropertyEditorRegistrar) {
((PropertyEditorRegistrar) bean).registerCustomEditors(new PropertyEditorRegistry() {
@Override
public void registerCustomEditor(Class<?> requiredType, String propertyPath, java.beans.PropertyEditor propertyEditor) {
DataBinder dataBinder = new DataBinder(bean);
dataBinder.registerCustomEditor(requiredType, propertyPath, propertyEditor);
}
@Override
public void registerCustomEditor(Class<?> requiredType, java.beans.PropertyEditor propertyEditor) {
DataBinder dataBinder = new DataBinder(bean);
dataBinder.registerCustomEditor(requiredType, propertyEditor);
}
@Override
public PropertyEditor findCustomEditor(Class<?> requiredType, String propertyPath) {
return null;
}
});
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}

View file

@ -0,0 +1,13 @@
package stirling.software.SPDF.config;
import java.beans.PropertyEditorSupport;
public class EmptyStringAsNullEditor extends PropertyEditorSupport {
@Override
public void setAsText(String text) {
if (text != null && text.trim().isEmpty()) {
setValue(null);
} else {
setValue(text);
}
}
}