Support NestedConfigurationProperty for record types

Add `ElementType.RECORD_COMPONENT` to `NestedConfigurationProperty` and implement `isMarkedAsNested` for `RecordParameterPropertyDescriptor`.

This will allow nested record to be properly harvested for their properties.
This commit is contained in:
Alexis Couvreur 2024-06-26 13:51:38 -04:00
parent 2dd6c1c3a0
commit 2315a72c3f
6 changed files with 61 additions and 3 deletions

View File

@ -50,7 +50,7 @@ class RecordParameterPropertyDescriptor extends ParameterPropertyDescriptor {
@Override
protected boolean isMarkedAsNested(MetadataGenerationEnvironment environment) {
return false;
return environment.getNestedConfigurationPropertyAnnotation(this.recordComponent) != null;
}
@Override

View File

@ -23,6 +23,7 @@ import org.springframework.boot.configurationprocessor.metadata.ItemMetadata;
import org.springframework.boot.configurationprocessor.metadata.Metadata;
import org.springframework.boot.configurationsample.deprecation.Dbcp2Configuration;
import org.springframework.boot.configurationsample.record.ExampleRecord;
import org.springframework.boot.configurationsample.record.NestedPropertiesRecord;
import org.springframework.boot.configurationsample.record.RecordWithGetter;
import org.springframework.boot.configurationsample.recursive.RecursiveProperties;
import org.springframework.boot.configurationsample.simple.ClassWithNestedProperties;
@ -510,6 +511,15 @@ class ConfigurationMetadataAnnotationProcessorTests extends AbstractMetadataGene
assertThat(metadata).doesNotHave(Metadata.withProperty("record-with-getter.bravo"));
}
@Test
void recordNested() {
ConfigurationMetadata metadata = compile(NestedPropertiesRecord.class);
assertThat(metadata).has(Metadata.withGroup("record-nested.nested"));
assertThat(metadata).has(Metadata.withProperty("record-nested.nested.my-nested-property"));
assertThat(metadata).has(Metadata.withGroup("record-nested.inner.nested"));
assertThat(metadata).has(Metadata.withProperty("record-nested.inner.nested.my-nested-property"));
}
@Test
void shouldNotMarkDbcp2UsernameOrPasswordAsDeprecated() {
ConfigurationMetadata metadata = compile(Dbcp2Configuration.class);

View File

@ -30,7 +30,7 @@ import java.lang.annotation.Target;
* @author Phillip Webb
* @since 1.2.0
*/
@Target(ElementType.FIELD)
@Target({ ElementType.FIELD, ElementType.RECORD_COMPONENT })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface NestedConfigurationProperty {

View File

@ -0,0 +1,28 @@
/*
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.record;
import org.springframework.boot.configurationsample.ConfigurationProperties;
import org.springframework.boot.configurationsample.NestedConfigurationProperty;
@ConfigurationProperties("record-nested")
public record NestedPropertiesRecord(String myProperty, @NestedConfigurationProperty NestedRecord nested,
InnerPropertiesRecord inner) {
public record InnerPropertiesRecord(String myInnerProperty, @NestedConfigurationProperty NestedRecord nested) {
}
}

View File

@ -0,0 +1,20 @@
/*
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.record;
public record NestedRecord(String myNestedProperty) {
}

View File

@ -38,7 +38,7 @@ import org.springframework.boot.context.properties.bind.Nested;
* @author Phillip Webb
* @since 1.2.0
*/
@Target(ElementType.FIELD)
@Target({ ElementType.FIELD, ElementType.RECORD_COMPONENT })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Nested