Skip to content

Commit

Permalink
Allow NestedConfigurationProperty on getters
Browse files Browse the repository at this point in the history
Adds support for getters with NestedConfigurationProperty.

Closes spring-projectsgh-9109
  • Loading branch information
jaredtbates committed Dec 17, 2023
1 parent 92a4a11 commit 0b73c5d
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ protected boolean isNested(MetadataGenerationEnvironment environment) {
if (environment.getConfigurationPropertiesAnnotation(getGetter()) != null) {
return false;
}
if (environment.getNestedConfigurationPropertyAnnotation(getField()) != null) {
if (environment.getNestedConfigurationPropertyAnnotation(getField()) != null
|| environment.getNestedConfigurationPropertyAnnotation(getGetter()) != null) {
return true;
}
if (isCyclePresent(typeElement, getOwnerElement())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
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;
Expand Down Expand Up @@ -333,6 +334,9 @@ void innerClassProperties() {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* @author Phillip Webb
* @since 1.2.0
*/
@Target(ElementType.FIELD)
@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface NestedConfigurationProperty {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class InnerClassProperties {

private Fourth fourth;

private final DeprecatedSimplePojo fifth = new DeprecatedSimplePojo();

public Foo getFirst() {
return this.first;
}
Expand All @@ -60,6 +62,11 @@ public void setFourth(Fourth fourth) {
this.fourth = fourth;
}

@NestedConfigurationProperty
public DeprecatedSimplePojo getFifth() {
return this.fifth;
}

public static class Foo {

private String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* @author Phillip Webb
* @since 1.2.0
*/
@Target(ElementType.FIELD)
@Target({ ElementType.FIELD, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Nested
Expand Down

0 comments on commit 0b73c5d

Please sign in to comment.