Using Spring Boot 2.6.4.
package test;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConstructorBinding;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.validation.annotation.Validated;
@Validated
@ConstructorBinding
@ConfigurationProperties(prefix = "myprefix")
public class Test {
private final long myLongValue;
public Test(@DefaultValue("3221225472") long myLongValue) {
this.myLongValue = myLongValue;
}
public long getMyLongValue() {
return myLongValue;
}
}
Error:
error: Invalid number representation '3221225472'
public Test(@DefaultValue("3221225472") long myLongValue) {
Comment From: wilkinsona
Thanks for the report. This looks like a bug to me as we're using Integer.parseInt(String)
to convert a long's default value. We seem to use Integer.parseInt
for all integral primitives. It might be worth some experimentation to see how that behaves if, for example, a short is configured with a default value that's larger than a short can contain.
Comment From: wilkinsona
If you provide a default value that is out of range, the metadata will be generated successfully, but the default value will be invalid.
Subsequently, when the @DefaultValue
is applied at runtime it fails:
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to bind properties under 'myprefix' to com.example.demo.Test:
Reason: failed to convert java.lang.String to @org.springframework.boot.context.properties.bind.DefaultValue short (caused by java.lang.NumberFormatException: Value out of range. Value:"12345678" Radix:10)
Action:
Update your application's configuration
I think we should catch this at compile time (as long as the annotation processor's in use) as this will ensure that the metadata isn't invalid.