Allow NestedConfigurationProperty on getters

This commit adds support adding `@NestedConfigurationProperty` on a
getter.

See gh-38844
This commit is contained in:
Jared Bates
2023-12-16 17:56:41 -06:00
committed by Stéphane Nicoll
parent 5d4a777d27
commit 6c8987e0d8
6 changed files with 52 additions and 3 deletions

View File

@@ -53,7 +53,8 @@ class JavaBeanPropertyDescriptor extends PropertyDescriptor {
@Override
protected boolean isMarkedAsNested(MetadataGenerationEnvironment environment) {
return environment.getNestedConfigurationPropertyAnnotation(this.field) != null;
return environment.getNestedConfigurationPropertyAnnotation(this.field) != null
|| environment.getNestedConfigurationPropertyAnnotation(getGetter()) != null;
}
@Override

View File

@@ -45,6 +45,7 @@ import org.springframework.boot.configurationsample.specific.AnnotatedGetter;
import org.springframework.boot.configurationsample.specific.BoxingPojo;
import org.springframework.boot.configurationsample.specific.BuilderPojo;
import org.springframework.boot.configurationsample.specific.DeprecatedLessPreciseTypePojo;
import org.springframework.boot.configurationsample.specific.DeprecatedSimplePojo;
import org.springframework.boot.configurationsample.specific.DeprecatedUnrelatedMethodPojo;
import org.springframework.boot.configurationsample.specific.DoubleRegistrationProperties;
import org.springframework.boot.configurationsample.specific.EmptyDefaultValueProperties;
@@ -336,6 +337,9 @@ class ConfigurationMetadataAnnotationProcessorTests extends AbstractMetadataGene
assertThat(metadata).has(Metadata.withProperty("config.third.value"));
assertThat(metadata).has(Metadata.withProperty("config.fourth"));
assertThat(metadata).isNotEqualTo(Metadata.withGroup("config.fourth"));
assertThat(metadata)
.has(Metadata.withGroup("config.fifth").ofType(DeprecatedSimplePojo.class).fromSource(InnerClassProperties.class));
assertThat(metadata).has(Metadata.withProperty("config.fifth.value").withDeprecation());
}
@Test

View File

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

View File

@@ -0,0 +1,37 @@
/*
* Copyright 2012-2019 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.specific;
/**
* POJO for use with samples needing a deprecated value.
*
* @author Jared Bates
*/
public class DeprecatedSimplePojo {
private int value;
@Deprecated
public int getValue() {
return this.value;
}
public void setValue(int value) {
this.value = value;
}
}

View File

@@ -36,6 +36,8 @@ public class InnerClassProperties {
private Fourth fourth;
private final DeprecatedSimplePojo fifth = new DeprecatedSimplePojo();
public Foo getFirst() {
return this.first;
}
@@ -60,6 +62,11 @@ public class InnerClassProperties {
this.fourth = fourth;
}
@NestedConfigurationProperty
public DeprecatedSimplePojo getFifth() {
return this.fifth;
}
public static class Foo {
private String name;

View File

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