Visitar URL original
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 · Issue #30020 · spring-projects/spring-boot · GitHub
Skip to content

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

Description

@roded

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) {

Activity

  1. added this to the 2.5.x milestone on Mar 1, 2022
  2. wilkinsona commented on Mar 1, 2022

    @wilkinsona
    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.

  3. wilkinsona commented on Mar 1, 2022

    @wilkinsona
    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.

  4. self-assigned this
    on Mar 1, 2022
  5. 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[/+] on Mar 1, 2022
  6. added a commit that references this issue on Mar 1, 2022
    a265f15
  7. modified the milestones: 2.5.x, 2.5.11 on Mar 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions