Feature Request: Support Arithmetic Expression Evaluation in spring-boot-configuration-processor for Default Values
Description
The spring-boot-configuration-processor
does not evaluate simple arithmetic expressions (e.g., 10 * 10
) when generating metadata for @ConfigurationProperties
fields, resulting in incorrect default values in spring-configuration-metadata.json
. This is a limitation for developers who use expressions to define default values.
Steps to Reproduce
- Create a Spring Boot project with
spring-boot-configuration-processor
dependency. - Define a
@ConfigurationProperties
class with a field initialized using an arithmetic expression:java @ConfigurationProperties(prefix = "foo") public class FooProperties { private int bar = 10 * 10; }
- Build the project to generate
spring-configuration-metadata.json
. - Check the generated metadata file in
target/classes/META-INF/spring-configuration-metadata.json
.
Expected Behavior
The metadata should reflect the computed default value:
{
"properties": [
{
"name": "foo.bar",
"type": "java.lang.Integer",
"defaultValue": 100
}
]
}
Actual Behavior
The metadata shows the Java default value for int
(0
) because the expression 10 * 10
is not evaluated:
{
"properties": [
{
"name": "foo.bar",
"type": "java.lang.Integer",
"defaultValue": 0
}
]
}
Environment
- Spring Boot Version: 3.3.4
- Operating System: Windows 11
- JDK Version: OpenJDK 17
- Build Tool: Maven 3.9.9
Additional Details
- The processor correctly handles static literals (e.g.,
private int bar = 100
) and certain factory methods (e.g.,Instant.parse("2019-02-12T11:49:22.455Z")
,Duration.ofDays(1)
) due to Spring’sConversionService
support. - Arithmetic expressions like
10 * 10
are deterministic but not evaluated, forcing developers to use workarounds like static literals. - This limitation affects IDE auto-completion and documentation, as the metadata does not reflect the intended default value.
- Proposed Enhancement: Add support for evaluating simple arithmetic expressions (e.g., addition, multiplication) in the configuration processor, possibly by integrating a lightweight expression evaluator or leveraging Spring Expression Language (SpEL) at compile time.
- Workaround: Using
private int bar = 100
works but loses the expressiveness of the arithmetic expression.
Comment From: snicoll
Workaround: Using private int bar = 100 works but loses the expressiveness of the arithmetic expression.
I don't thinks that's the only way to express this. You could define a constant that has whatever arithmetic you want and then use the constant as the default value.
Supporting arithmetic expression like that doesn't sound very realistic to me as we're at the level of structure parsing. Is there a problem defining a constant?
Comment From: spring-projects-issues
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.
Comment From: snicoll
If I've missed something with the proposal above, please comment. Regardless, we're not going to implement this. The next logical step would be to show 10 * 10
as the default value in your IDE because it's more expressive, and that's also something we can't do reliably.
Comment From: wilkinsona
We've opened #46509 so that the metadata will omit the default value in this situation rather than including an incorrect default value.