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

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

Closed
roded opened this issue Mar 1, 2022 · 2 comments
Assignees
Labels
type: bug A general bug
Milestone

Comments

@roded
Copy link

roded commented Mar 1, 2022

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) {
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 1, 2022
@wilkinsona wilkinsona added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Mar 1, 2022
@wilkinsona wilkinsona added this to the 2.5.x milestone Mar 1, 2022
@wilkinsona
Copy link
Member

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.

@wilkinsona
Copy link
Member

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 wilkinsona self-assigned this Mar 1, 2022
@wilkinsona 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
@wilkinsona wilkinsona modified the milestones: 2.5.x, 2.5.11 Mar 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants