Skip to content

Commit 5cf373a

Browse files
Kguswosnicoll
authored andcommitted
Do not set primitive default value for unsupported expressions
See gh-46551 Signed-off-by: Now <[email protected]>
1 parent b34d120 commit 5cf373a

File tree

4 files changed

+110
-1
lines changed

4 files changed

+110
-1
lines changed

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/fieldvalues/javac/JavaCompilerFieldValuesParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ private Object getValue(String variableType, ExpressionTree expression, Object d
209209
}
210210
return null;
211211
}
212-
return defaultValue;
212+
return null;
213213
}
214214

215215
private Object getFactoryValue(ExpressionTree expression, Object factoryValue) {

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/JsonConverter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ private void putHintValue(JSONObject jsonObject, Object value) throws Exception
145145
}
146146

147147
private void putDefaultValue(JSONObject jsonObject, Object value) throws Exception {
148+
if (value == null) {
149+
return;
150+
}
148151
Object defaultValue = extractItemValue(value);
149152
jsonObject.put("defaultValue", defaultValue);
150153
}

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessorTests.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@
1818

1919
import java.time.temporal.ChronoField;
2020
import java.time.temporal.ChronoUnit;
21+
import java.util.Arrays;
2122

2223
import org.junit.jupiter.api.Test;
2324

2425
import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata;
2526
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata;
2627
import org.springframework.boot.configurationprocessor.metadata.Metadata;
28+
import org.springframework.boot.configurationprocessor.test.CompiledMetadataReader;
29+
import org.springframework.boot.configurationprocessor.test.TestConfigurationMetadataAnnotationProcessor;
2730
import org.springframework.boot.configurationsample.deprecation.Dbcp2Configuration;
31+
import org.springframework.boot.configurationsample.fieldvalues.ArithmeticExpressionProperties;
2832
import org.springframework.boot.configurationsample.method.NestedPropertiesMethod;
2933
import org.springframework.boot.configurationsample.record.ExampleRecord;
3034
import org.springframework.boot.configurationsample.record.NestedPropertiesRecord;
@@ -67,6 +71,9 @@
6771
import org.springframework.boot.configurationsample.specific.SimplePojo;
6872
import org.springframework.boot.configurationsample.specific.StaticAccessor;
6973
import org.springframework.core.test.tools.CompilationException;
74+
import org.springframework.core.test.tools.ResourceFile;
75+
import org.springframework.core.test.tools.SourceFile;
76+
import org.springframework.core.test.tools.TestCompiler;
7077

7178
import static org.assertj.core.api.Assertions.assertThat;
7279
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -82,6 +89,7 @@
8289
* @author Pavel Anisimov
8390
* @author Scott Frederick
8491
* @author Moritz Halbritter
92+
* @author Hyeon Jae Kim
8593
*/
8694
class ConfigurationMetadataAnnotationProcessorTests extends AbstractMetadataGenerationTests {
8795

@@ -570,4 +578,19 @@ void recordPropertiesWithDescriptions() {
570578
.withDescription("last description in Javadoc"));
571579
}
572580

581+
@Test
582+
void arithmeticExpressionPropertiesShouldOmitUnknownDefaultValues(){
583+
ConfigurationMetadata metadata = compile(ArithmeticExpressionProperties.class);
584+
assertThat(metadata).has(Metadata.withProperty("arithmetic.calculated", Integer.class)
585+
.fromSource(ArithmeticExpressionProperties.class));
586+
assertThat(metadata).has(Metadata.withProperty("arithmetic.literal", Integer.class)
587+
.fromSource(ArithmeticExpressionProperties.class)
588+
.withDefaultValue(100));
589+
assertThat(metadata).has(Metadata.withProperty("arithmetic.simple-flag", Boolean.class)
590+
.fromSource(ArithmeticExpressionProperties.class)
591+
.withDefaultValue(true));
592+
assertThat(metadata).has(Metadata.withProperty("arithmetic.complex-flag", Boolean.class)
593+
.fromSource(ArithmeticExpressionProperties.class));
594+
}
595+
573596
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2012-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.configurationsample.fieldvalues;
18+
19+
import org.springframework.boot.configurationsample.ConfigurationProperties;
20+
21+
/**
22+
* Configuration properties with arithmetic expressions to test metadata generation for
23+
* values that cannot be inferred at compile time. Used to verify that primitive types
24+
* with unknown default values omit the defaultValue field in the generated metadata.
25+
*
26+
* @author Hyeon Jae Kim
27+
*/
28+
@ConfigurationProperties(prefix = "arithmetic")
29+
public class ArithmeticExpressionProperties {
30+
31+
/**
32+
* A value calculated using arithmetic expression that cannot be inferred.
33+
*/
34+
private int calculated = 10 * 10;
35+
36+
/**
37+
* A literal value that can be inferred.
38+
*/
39+
private int literal = 100;
40+
41+
/**
42+
* A boolean expression that cannot be inferred.
43+
*/
44+
private boolean complexFlag = !false;
45+
46+
/**
47+
* A simple boolean literal.
48+
*/
49+
private boolean simpleFlag = true;
50+
51+
public int getCalculated() {
52+
return this.calculated;
53+
}
54+
55+
public void setCalculated(int calculated) {
56+
this.calculated = calculated;
57+
}
58+
59+
public int getLiteral() {
60+
return this.literal;
61+
}
62+
63+
public void setLiteral(int literal) {
64+
this.literal = literal;
65+
}
66+
67+
public boolean isComplexFlag() {
68+
return this.complexFlag;
69+
}
70+
71+
public void setComplexFlag(boolean complexFlag) {
72+
this.complexFlag = complexFlag;
73+
}
74+
75+
public boolean isSimpleFlag() {
76+
return this.simpleFlag;
77+
}
78+
79+
public void setSimpleFlag(boolean simpleFlag) {
80+
this.simpleFlag = simpleFlag;
81+
}
82+
83+
}

0 commit comments

Comments
 (0)