From f4a01884bda3559288630a84ac97253d84d557ee Mon Sep 17 00:00:00 2001 From: Anthony Stirling <77850077+Frooodle@users.noreply.github.com> Date: Sat, 7 Oct 2023 23:23:56 +0100 Subject: [PATCH] test empty = null for settings --- .../software/SPDF/config/AppConfig.java | 13 ++++++ .../SPDF/config/CustomEditorConfigurer.java | 42 +++++++++++++++++++ .../SPDF/config/EmptyStringAsNullEditor.java | 13 ++++++ 3 files changed, 68 insertions(+) create mode 100644 src/main/java/stirling/software/SPDF/config/CustomEditorConfigurer.java create mode 100644 src/main/java/stirling/software/SPDF/config/EmptyStringAsNullEditor.java diff --git a/src/main/java/stirling/software/SPDF/config/AppConfig.java b/src/main/java/stirling/software/SPDF/config/AppConfig.java index 309437b68..602102d0d 100644 --- a/src/main/java/stirling/software/SPDF/config/AppConfig.java +++ b/src/main/java/stirling/software/SPDF/config/AppConfig.java @@ -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; diff --git a/src/main/java/stirling/software/SPDF/config/CustomEditorConfigurer.java b/src/main/java/stirling/software/SPDF/config/CustomEditorConfigurer.java new file mode 100644 index 000000000..6a0ac986c --- /dev/null +++ b/src/main/java/stirling/software/SPDF/config/CustomEditorConfigurer.java @@ -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; + } +} diff --git a/src/main/java/stirling/software/SPDF/config/EmptyStringAsNullEditor.java b/src/main/java/stirling/software/SPDF/config/EmptyStringAsNullEditor.java new file mode 100644 index 000000000..fd8de476a --- /dev/null +++ b/src/main/java/stirling/software/SPDF/config/EmptyStringAsNullEditor.java @@ -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); + } + } +}