Skip to content

Commit ddf75f0

Browse files
committed
Merge branch '2.3.x' into 2.4.x
Closes gh-25515
2 parents 2f95c1e + 034e7d4 commit ddf75f0

File tree

3 files changed

+86
-11
lines changed

3 files changed

+86
-11
lines changed

spring-boot-project/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/SimpleConfigurationMetadataRepository.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void add(Collection<ConfigurationMetadataSource> sources) {
6161
}
6262
String sourceType = source.getType();
6363
if (sourceType != null) {
64-
putIfAbsent(group.getSources(), sourceType, source);
64+
addOrMergeSource(group.getSources(), sourceType, source);
6565
}
6666
}
6767
}
@@ -93,7 +93,7 @@ public void include(ConfigurationMetadataRepository repository) {
9393
// Merge properties
9494
group.getProperties().forEach((name, value) -> putIfAbsent(existingGroup.getProperties(), name, value));
9595
// Merge sources
96-
group.getSources().forEach((name, value) -> putIfAbsent(existingGroup.getSources(), name, value));
96+
group.getSources().forEach((name, value) -> addOrMergeSource(existingGroup.getSources(), name, value));
9797
}
9898
}
9999

@@ -111,6 +111,17 @@ private ConfigurationMetadataGroup getGroup(ConfigurationMetadataSource source)
111111
return this.allGroups.get(source.getGroupId());
112112
}
113113

114+
private void addOrMergeSource(Map<String, ConfigurationMetadataSource> sources, String name,
115+
ConfigurationMetadataSource source) {
116+
ConfigurationMetadataSource existingSource = sources.get(name);
117+
if (existingSource == null) {
118+
sources.put(name, source);
119+
}
120+
else {
121+
source.getProperties().forEach((k, v) -> putIfAbsent(existingSource.getProperties(), k, v));
122+
}
123+
}
124+
114125
private <V> void putIfAbsent(Map<String, V> map, String key, V value) {
115126
if (!map.containsKey(key)) {
116127
map.put(key, value);

spring-boot-project/spring-boot-tools/spring-boot-configuration-metadata/src/test/java/org/springframework/boot/configurationmetadata/ConfigurationMetadataRepositoryJsonBuilderTests.java

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.IOException;
2020
import java.io.InputStream;
21+
import java.util.Arrays;
2122
import java.util.Map;
2223

2324
import org.junit.jupiter.api.Test;
@@ -99,16 +100,56 @@ void severalRepositoriesIdenticalGroups() throws IOException {
99100
try (InputStream foo2 = getInputStreamFor("foo2")) {
100101
ConfigurationMetadataRepository repo = ConfigurationMetadataRepositoryJsonBuilder.create(foo, foo2)
101102
.build();
102-
assertThat(repo.getAllGroups()).hasSize(1);
103+
Iterable<String> allKeys = Arrays.asList("spring.foo.name", "spring.foo.description",
104+
"spring.foo.counter", "spring.foo.enabled", "spring.foo.type");
105+
assertThat(repo.getAllProperties()).containsOnlyKeys(allKeys);
106+
assertThat(repo.getAllGroups()).containsOnlyKeys("spring.foo");
103107
ConfigurationMetadataGroup group = repo.getAllGroups().get("spring.foo");
104-
contains(group.getSources(), "org.acme.Foo", "org.acme.Foo2", "org.springframework.boot.FooProperties");
105-
assertThat(group.getSources()).hasSize(3);
106-
contains(group.getProperties(), "spring.foo.name", "spring.foo.description", "spring.foo.counter",
107-
"spring.foo.enabled", "spring.foo.type");
108-
assertThat(group.getProperties()).hasSize(5);
109-
contains(repo.getAllProperties(), "spring.foo.name", "spring.foo.description", "spring.foo.counter",
110-
"spring.foo.enabled", "spring.foo.type");
111-
assertThat(repo.getAllProperties()).hasSize(5);
108+
assertThat(group.getProperties()).containsOnlyKeys(allKeys);
109+
assertThat(group.getSources()).containsOnlyKeys("org.acme.Foo", "org.acme.Foo2",
110+
"org.springframework.boot.FooProperties");
111+
assertThat(group.getSources().get("org.acme.Foo").getProperties()).containsOnlyKeys("spring.foo.name",
112+
"spring.foo.description");
113+
assertThat(group.getSources().get("org.acme.Foo2").getProperties())
114+
.containsOnlyKeys("spring.foo.enabled", "spring.foo.type");
115+
assertThat(group.getSources().get("org.springframework.boot.FooProperties").getProperties())
116+
.containsOnlyKeys("spring.foo.name", "spring.foo.counter");
117+
}
118+
}
119+
}
120+
121+
@Test
122+
void severalRepositoriesIdenticalGroupsWithSameType() throws IOException {
123+
try (InputStream foo = getInputStreamFor("foo")) {
124+
try (InputStream foo3 = getInputStreamFor("foo3")) {
125+
ConfigurationMetadataRepository repo = ConfigurationMetadataRepositoryJsonBuilder.create(foo, foo3)
126+
.build();
127+
Iterable<String> allKeys = Arrays.asList("spring.foo.name", "spring.foo.description",
128+
"spring.foo.counter", "spring.foo.enabled", "spring.foo.type");
129+
assertThat(repo.getAllProperties()).containsOnlyKeys(allKeys);
130+
assertThat(repo.getAllGroups()).containsOnlyKeys("spring.foo");
131+
ConfigurationMetadataGroup group = repo.getAllGroups().get("spring.foo");
132+
assertThat(group.getProperties()).containsOnlyKeys(allKeys);
133+
assertThat(group.getSources()).containsOnlyKeys("org.acme.Foo",
134+
"org.springframework.boot.FooProperties");
135+
assertThat(group.getSources().get("org.acme.Foo").getProperties()).containsOnlyKeys("spring.foo.name",
136+
"spring.foo.description", "spring.foo.enabled", "spring.foo.type");
137+
assertThat(group.getSources().get("org.springframework.boot.FooProperties").getProperties())
138+
.containsOnlyKeys("spring.foo.name", "spring.foo.counter");
139+
}
140+
}
141+
}
142+
143+
@Test
144+
void severalRepositoriesIdenticalGroupsWithSameTypeDoesNotOverrideSource() throws IOException {
145+
try (InputStream foo = getInputStreamFor("foo")) {
146+
try (InputStream foo3 = getInputStreamFor("foo3")) {
147+
ConfigurationMetadataRepository repo = ConfigurationMetadataRepositoryJsonBuilder.create(foo, foo3)
148+
.build();
149+
ConfigurationMetadataGroup group = repo.getAllGroups().get("spring.foo");
150+
ConfigurationMetadataSource fooSource = group.getSources().get("org.acme.Foo");
151+
assertThat(fooSource.getSourceMethod()).isEqualTo("foo()");
152+
assertThat(fooSource.getDescription()).isEqualTo("This is Foo.");
112153
}
113154
}
114155
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"groups": [
3+
{
4+
"name": "spring.foo",
5+
"type": "org.acme.Foo",
6+
"sourceType": "org.acme.config.FooApp",
7+
"sourceMethod": "foo3()",
8+
"description": "This is Foo3."
9+
}
10+
],
11+
"properties": [
12+
{
13+
"name": "spring.foo.enabled",
14+
"type": "java.lang.Boolean",
15+
"sourceType": "org.acme.Foo"
16+
},
17+
{
18+
"name": "spring.foo.type",
19+
"type": "java.lang.String",
20+
"sourceType": "org.acme.Foo"
21+
}
22+
]
23+
}

0 commit comments

Comments
 (0)