You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
spring-boot-configuration-processor fails compilation due to @DefaultValue with a long value and generates invalid metadata for byte and short properties with out-of-range default values
#30020
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) {
The text was updated successfully, but these errors were encountered:
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.
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.
wilkinsona
changed the title
spring-boot-configuration-processor fails compilation due to @DefaultValue with a long value
spring-boot-configuration-processor fails compilation due to @DefaultValue with a long value and generates invalid metadata for byte and short properties with out-of-range default values
Mar 1, 2022
Using Spring Boot 2.6.4.
Error:
The text was updated successfully, but these errors were encountered: