Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private void collectProperties(String prefix, SkipPropertyMapping defaultSkip, M
}
}
String name = getName(prefix, attributeMapping, attribute);
putProperties(name, value.get(), properties);
putProperties(name, skip, value.get(), properties);
}

private String getName(String prefix, MergedAnnotation<?> attributeMapping, Method attribute) {
Expand Down Expand Up @@ -118,11 +118,17 @@ private String dotAppend(String prefix, String postfix) {
return postfix;
}

private void putProperties(String name, Object value, Map<String, Object> properties) {
private void putProperties(String name, SkipPropertyMapping defaultSkip, Object value,
Map<String, Object> properties) {
if (ObjectUtils.isArray(value)) {
Object[] array = ObjectUtils.toObjectArray(value);
for (int i = 0; i < array.length; i++) {
properties.put(name + "[" + i + "]", array[i]);
putProperties(name + "[" + i + "]", defaultSkip, array[i], properties);
}
}
else if (value instanceof MergedAnnotation<?>) {
for (Method attribute : ((MergedAnnotation<?>) value).getType().getDeclaredMethods()) {
collectProperties(name, defaultSkip, (MergedAnnotation<?>) value, attribute, properties);
}
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@

import org.junit.jupiter.api.Test;

import org.springframework.boot.test.autoconfigure.properties.AnnotationsPropertySourceTests.DeeplyNestedAnnotations.Level1;
import org.springframework.boot.test.autoconfigure.properties.AnnotationsPropertySourceTests.DeeplyNestedAnnotations.Level2;
import org.springframework.boot.test.autoconfigure.properties.AnnotationsPropertySourceTests.NestedAnnotations.Entry;
import org.springframework.core.annotation.AliasFor;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -172,6 +175,26 @@ void enumValueNotMapped() {
assertThat(source.containsProperty("testenum.value")).isFalse();
}

@Test
void nestedAnnotationsMapped() {
AnnotationsPropertySource source = new AnnotationsPropertySource(PropertyMappedWithNestedAnnotations.class);
assertThat(source.getProperty("testnested")).isNull();
assertThat(source.getProperty("testnested.entries[0]")).isNull();
assertThat(source.getProperty("testnested.entries[0].value")).isEqualTo("one");
assertThat(source.getProperty("testnested.entries[1]")).isNull();
assertThat(source.getProperty("testnested.entries[1].value")).isEqualTo("two");
}

@Test
void deeplyNestedAnnotationsMapped() {
AnnotationsPropertySource source = new AnnotationsPropertySource(
PropertyMappedWithDeeplyNestedAnnotations.class);
assertThat(source.getProperty("testdeeplynested")).isNull();
assertThat(source.getProperty("testdeeplynested.level1")).isNull();
assertThat(source.getProperty("testdeeplynested.level1.level2")).isNull();
assertThat(source.getProperty("testdeeplynested.level1.level2.value")).isEqualTo("level2");
}

static class NoAnnotation {

}
Expand Down Expand Up @@ -396,4 +419,51 @@ enum EnumItem {

}

@Retention(RetentionPolicy.RUNTIME)
@PropertyMapping("testnested")
@interface NestedAnnotations {

Entry[] entries();

@Retention(RetentionPolicy.RUNTIME)
@interface Entry {

String value();

}

}

@NestedAnnotations(entries = { @Entry("one"), @Entry("two") })
static class PropertyMappedWithNestedAnnotations {

}

@Retention(RetentionPolicy.RUNTIME)
@PropertyMapping("testdeeplynested")
@interface DeeplyNestedAnnotations {

Level1 level1();

@Retention(RetentionPolicy.RUNTIME)
@interface Level1 {

Level2 level2();

}

@Retention(RetentionPolicy.RUNTIME)
@interface Level2 {

String value();

}

}

@DeeplyNestedAnnotations(level1 = @Level1(level2 = @Level2("level2")))
static class PropertyMappedWithDeeplyNestedAnnotations {

}

}