Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accepting null as default value for a property [SPR-14896] #19462

Closed
spring-projects-issues opened this issue Nov 10, 2016 · 4 comments
Closed
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: declined A suggestion or change that we don't feel we should currently apply type: enhancement A general enhancement

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Nov 10, 2016

Antonio Anzivino opened SPR-14896 and commented

Very silly issue.

I have a configuration mechanism that is based on both XML and property files and I use SpEL with a custom property resolver (the same as #18943).

Example XML

<bean id="oldValPasswordPolicy" class="com.acme.OldValPasswordPolicy">
    <property name="oldValues" value="${password.oldValues}" />
</bean>

Then in the property files, if I want not to use this component, I can set null to its value:

password.oldValues=3 #enables component
password.oldValues= #sets null, leaving it disabled

This requires a property password.oldValues to be defined in the property files.
Since I am maintaining an application already in production, I have been asked not to require additional properties. So I wanted to use null as default value (which is legal in my case) without adding a line to my property file. If I don't add the property, even empty, I get an exception

I have tried the following SpEL

${password.oldValues:} #with empty String

org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'lengthPasswordPolicy' defined in file [D:\Workspace\phoenix.web.3.0\build\classes\META-INF\context\phoenix-web-context.xml]: 1; nested exception is java.lang.ArrayIndexOutOfBoundsException: 1
	at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:223)
	at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.processProperties(PropertySourcesPlaceholderConfigurer.java:180)
	at it.phoenix.core.properties.EncrypedPropertyPlaceholderConfigurer.processProperties(EncrypedPropertyPlaceholderConfigurer.java:43)
	at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourcesPlaceholderConfigurer.java:152)
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:284)
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:166)
	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:681)
	at org.springframework.context.support.AbstractApplicationContext.__refresh(AbstractApplicationContext.java:523)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java)
	at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5099)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5615)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1571)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1561)
	at java.util.concurrent.FutureTask.run(FutureTask.java:262)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
	at it.phoenix.core.properties.EncryptedPropertyResolver.getPropertyAsRawString(EncryptedPropertyResolver.java:98)
	at org.springframework.core.env.AbstractPropertyResolver$1.resolvePlaceholder(AbstractPropertyResolver.java:222)
	at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:147)
	at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
	at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:219)
	at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:193)
	at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:172)
	at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveStringValue(BeanDefinitionVisitor.java:282)
	at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveValue(BeanDefinitionVisitor.java:204)
	at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitPropertyValues(BeanDefinitionVisitor.java:141)
	at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitBeanDefinition(BeanDefinitionVisitor.java:82)
	at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:220)
	... 20 more

${password.oldValues:null} #with explicit null

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'lengthPasswordPolicy' defined in file [D:\Workspace\phoenix.web.3.0\build\classes\META-INF\context\phoenix-web-context.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [java.lang.Integer] for property 'minLength'; nested exception is java.lang.NumberFormatException: For input string: "null"
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351)
	... 50 more
Caused by: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [java.lang.Integer] for property 'minLength'; nested exception is java.lang.NumberFormatException: For input string: "null"
	at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:596)
	at org.springframework.beans.AbstractNestablePropertyAccessor.convertForProperty(AbstractNestablePropertyAccessor.java:603)
	at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:216)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1532)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1491)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1231)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
	... 56 more
Caused by: java.lang.NumberFormatException: For input string: "null"
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.lang.Integer.parseInt(Integer.java:492)
	at java.lang.Integer.valueOf(Integer.java:582)
	at org.springframework.util.NumberUtils.parseNumber(NumberUtils.java:208)
	at org.springframework.beans.propertyeditors.CustomNumberEditor.setAsText(CustomNumberEditor.java:113)
	at org.springframework.beans.TypeConverterDelegate.doConvertTextValue(TypeConverterDelegate.java:468)
	at org.springframework.beans.TypeConverterDelegate.doConvertValue(TypeConverterDelegate.java:441)
	at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:199)
	at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:576)
	... 62 more

The following article reminds that Integer.valueOf does not accept null, so it shall be treated in advance
https://stackoverflow.com/questions/15115772/why-integer-doesnt-solve-null-string

As for the solution, I don't care whether empty string or "null" (actually, "null" is a legitimate String) should be the correct solution, but Spring should be smart enough to parse it as a null number

Point of fix should be

org.springframework.util.NumberUtils.parseNumber(NumberUtils.java:208)

Open to discussion. I currently don't know about any workaround


Affects: 4.3.3

Issue Links:

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

What does EncryptedPropertyResolver.getPropertyAsRawString do? Is this a custom class in your codebase? Does it make specific assumptions about the contents, leading to that ArrayIndexOutOfBoundsException in your first stacktrace?

Generally speaking, specifying an empty String as a default value works against number types works as long as they are number wrappers (i.e. Integer instead of int) where we can actually pass null in. CustomNumberEditor does such handling of empty values already, one level above NumberUtils.

If you'd like to make a special String such as "null" work, you could specify it as a custom nullValue on PropertySourcesPlaceholderConfigurer.

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Nov 11, 2016

Antonio Anzivino commented

Good point, if that is because of my custom property placeholder I will update my codebase accordingly.

The EncryptedPropertyResolver class decrypts properties wrapped between special markers #ENC(xxxxxxx)#.
My numbers in the beans are of class type (Integer, not int), that is why I want to set them to null :-)

Basically it queries all the configured PropertySources to obtain the plaintext property as encoded in the property file, then if it is wrapped in the special markers it will decrypt the ciphertext.

I still think this issue is not related to #18943 because I can't see my property resolver in the stack trace. While the property resolver is enabled, the exception still occurs before the property resolver is called. I can update my stack trace with full information. The error occurs when creating beans that do not use encrypted properties.

@spring-projects-issues
Copy link
Collaborator Author

Antonio Anzivino commented

Requesting to close as I needed to add the appropriate property to my EncrypedPropertyPlaceholderConfigurer.

As per documentation, by default the system PropertyPlaceholderConfigurer does not set a nullValue, and since my special resolver (aimed at decrypting properties if encrypted) is a PropertyPlaceholderConfigurer, the nullValue must be applied to it

<bean class="EncrypedPropertyPlaceholderConfigurer">
    <property name="ignoreResourceNotFound" value="true" />
    <property name="nullValue" value="null" />
    <property name="trimValues" value="true" />
</bean>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: declined A suggestion or change that we don't feel we should currently apply type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants