Skip to content

Commit 8000754

Browse files
committed
Merge pull request #41251 from acouvreur
* pr/41251: Support NestedConfigurationProperty for record types Closes gh-41251
2 parents efd1bd1 + 47c8a85 commit 8000754

File tree

6 files changed

+61
-3
lines changed

6 files changed

+61
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ protected List<Element> getDeprecatableElements() {
5050

5151
@Override
5252
protected boolean isMarkedAsNested(MetadataGenerationEnvironment environment) {
53-
return false;
53+
return environment.getNestedConfigurationPropertyAnnotation(this.recordComponent) != null;
5454
}
5555

5656
@Override

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.boot.configurationprocessor.metadata.Metadata;
2424
import org.springframework.boot.configurationsample.deprecation.Dbcp2Configuration;
2525
import org.springframework.boot.configurationsample.record.ExampleRecord;
26+
import org.springframework.boot.configurationsample.record.NestedPropertiesRecord;
2627
import org.springframework.boot.configurationsample.record.RecordWithGetter;
2728
import org.springframework.boot.configurationsample.recursive.RecursiveProperties;
2829
import org.springframework.boot.configurationsample.simple.ClassWithNestedProperties;
@@ -510,6 +511,15 @@ void recordWithGetter() {
510511
assertThat(metadata).doesNotHave(Metadata.withProperty("record-with-getter.bravo"));
511512
}
512513

514+
@Test
515+
void recordNested() {
516+
ConfigurationMetadata metadata = compile(NestedPropertiesRecord.class);
517+
assertThat(metadata).has(Metadata.withGroup("record-nested.nested"));
518+
assertThat(metadata).has(Metadata.withProperty("record-nested.nested.my-nested-property"));
519+
assertThat(metadata).has(Metadata.withGroup("record-nested.inner.nested"));
520+
assertThat(metadata).has(Metadata.withProperty("record-nested.inner.nested.my-nested-property"));
521+
}
522+
513523
@Test
514524
void shouldNotMarkDbcp2UsernameOrPasswordAsDeprecated() {
515525
ConfigurationMetadata metadata = compile(Dbcp2Configuration.class);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* @author Phillip Webb
3131
* @since 1.2.0
3232
*/
33-
@Target(ElementType.FIELD)
33+
@Target({ ElementType.FIELD, ElementType.RECORD_COMPONENT })
3434
@Retention(RetentionPolicy.RUNTIME)
3535
@Documented
3636
public @interface NestedConfigurationProperty {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2012-2024 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.record;
18+
19+
import org.springframework.boot.configurationsample.ConfigurationProperties;
20+
import org.springframework.boot.configurationsample.NestedConfigurationProperty;
21+
22+
@ConfigurationProperties("record-nested")
23+
public record NestedPropertiesRecord(String myProperty, @NestedConfigurationProperty NestedRecord nested,
24+
InnerPropertiesRecord inner) {
25+
26+
public record InnerPropertiesRecord(String myInnerProperty, @NestedConfigurationProperty NestedRecord nested) {
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2012-2024 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.record;
18+
19+
public record NestedRecord(String myNestedProperty) {
20+
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/NestedConfigurationProperty.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
* @author Phillip Webb
3939
* @since 1.2.0
4040
*/
41-
@Target(ElementType.FIELD)
41+
@Target({ ElementType.FIELD, ElementType.RECORD_COMPONENT })
4242
@Retention(RetentionPolicy.RUNTIME)
4343
@Documented
4444
@Nested

0 commit comments

Comments
 (0)