Skip to content

Commit 8cbd7f5

Browse files
committed
Polish "Add support for initializing nested object when nothing bound"
This commit harmonizes the change made to @DefaultValue to the annotation processor. If such annotation is added to a scalar value with no value at all, no default value is produced. Closes gh-18917
1 parent 3065c88 commit 8cbd7f5

File tree

5 files changed

+65
-3
lines changed

5 files changed

+65
-3
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.junit.jupiter.api.Test;
2020

2121
import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata;
22+
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata;
2223
import org.springframework.boot.configurationprocessor.metadata.Metadata;
2324
import org.springframework.boot.configurationsample.recursive.RecursiveProperties;
2425
import org.springframework.boot.configurationsample.simple.ClassWithNestedProperties;
@@ -39,6 +40,7 @@
3940
import org.springframework.boot.configurationsample.specific.BuilderPojo;
4041
import org.springframework.boot.configurationsample.specific.DeprecatedUnrelatedMethodPojo;
4142
import org.springframework.boot.configurationsample.specific.DoubleRegistrationProperties;
43+
import org.springframework.boot.configurationsample.specific.EmptyDefaultValueProperties;
4244
import org.springframework.boot.configurationsample.specific.ExcludedTypesPojo;
4345
import org.springframework.boot.configurationsample.specific.InnerClassAnnotatedGetterConfig;
4446
import org.springframework.boot.configurationsample.specific.InnerClassHierarchicalProperties;
@@ -362,6 +364,15 @@ void constructorParameterPropertyWithInvalidDefaultValueOnCharacter() {
362364
.withMessageContaining("Compilation failed");
363365
}
364366

367+
@Test
368+
void constructorParameterPropertyWithEmptyDefaultValueOnProperty() {
369+
ConfigurationMetadata metadata = compile(EmptyDefaultValueProperties.class);
370+
assertThat(metadata).has(Metadata.withProperty("test.name"));
371+
ItemMetadata nameMetadata = metadata.getItems().stream().filter((item) -> item.getName().equals("test.name"))
372+
.findFirst().get();
373+
assertThat(nameMetadata.getDefaultValue()).isNull();
374+
}
375+
365376
@Test
366377
void recursivePropertiesDoNotCauseAStackOverflow() {
367378
compile(RecursiveProperties.class);

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.junit.jupiter.api.Test;
2020

2121
import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata;
22+
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata;
2223
import org.springframework.boot.configurationprocessor.metadata.Metadata;
2324
import org.springframework.boot.configurationsample.immutable.DeducedImmutableClassProperties;
2425

@@ -28,14 +29,21 @@
2829
* Metadata generation tests for immutable properties deduced because they're nested.
2930
*
3031
* @author Phillip Webb
32+
* @author Stephane Nicoll
3133
*/
3234
class DeducedImmutablePropertiesMetadataGenerationTests extends AbstractMetadataGenerationTests {
3335

3436
@Test
3537
void immutableSimpleProperties() {
3638
ConfigurationMetadata metadata = compile(DeducedImmutableClassProperties.class);
3739
assertThat(metadata).has(Metadata.withGroup("test").fromSource(DeducedImmutableClassProperties.class));
38-
assertThat(metadata).has(Metadata.withProperty("test.nested.name", String.class));
40+
assertThat(metadata).has(Metadata.withGroup("test.nested", DeducedImmutableClassProperties.Nested.class)
41+
.fromSource(DeducedImmutableClassProperties.class));
42+
assertThat(metadata).has(Metadata.withProperty("test.nested.name", String.class)
43+
.fromSource(DeducedImmutableClassProperties.Nested.class));
44+
ItemMetadata nestedMetadata = metadata.getItems().stream()
45+
.filter((item) -> item.getName().equals("test.nested")).findFirst().get();
46+
assertThat(nestedMetadata.getDefaultValue()).isNull();
3947
}
4048

4149
}

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/DefaultValue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@
3333
@Documented
3434
public @interface DefaultValue {
3535

36-
String[] value();
36+
String[] value() default {};
3737

3838
}

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/immutable/DeducedImmutableClassProperties.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.springframework.boot.configurationsample.ConfigurationProperties;
2020
import org.springframework.boot.configurationsample.ConstructorBinding;
21+
import org.springframework.boot.configurationsample.DefaultValue;
2122

2223
/**
2324
* Inner properties, in immutable format.
@@ -30,7 +31,7 @@ public class DeducedImmutableClassProperties {
3031

3132
private final Nested nested;
3233

33-
public DeducedImmutableClassProperties(Nested nested) {
34+
public DeducedImmutableClassProperties(@DefaultValue Nested nested) {
3435
this.nested = nested;
3536
}
3637

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2012-2020 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.specific;
18+
19+
import org.springframework.boot.configurationsample.ConfigurationProperties;
20+
import org.springframework.boot.configurationsample.ConstructorBinding;
21+
import org.springframework.boot.configurationsample.DefaultValue;
22+
23+
/**
24+
* Demonstrates that an empty default value on a property leads to no default value.
25+
*
26+
* @author Stephane Nicoll
27+
*/
28+
@ConfigurationProperties("test")
29+
public class EmptyDefaultValueProperties {
30+
31+
private final String name;
32+
33+
@ConstructorBinding
34+
public EmptyDefaultValueProperties(@DefaultValue String name) {
35+
this.name = name;
36+
}
37+
38+
public String getName() {
39+
return this.name;
40+
}
41+
42+
}

0 commit comments

Comments
 (0)