Skip to content

Commit cf4bc6e

Browse files
brenuartsnicoll
authored andcommitted
Include properties in source merge algorithm
This commit improves SimpleConfigurationMetadataRepository to include properties that are contributed to an existing configuration metadata source. See gh-25507
1 parent c62367e commit cf4bc6e

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,17 @@ private <V> void putIfAbsent(Map<String, V> map, String key, V value) {
117117
}
118118
}
119119

120+
/*
121+
Uncomment this code to fix issue revealed by ConfigurationMetadataRepositoryJsonBuilderTests#severalRepositoriesIdenticalGroups3()
122+
123+
private void putIfAbsent(Map<String, ConfigurationMetadataSource> sources, String name,
124+
ConfigurationMetadataSource source) {
125+
ConfigurationMetadataSource existing = sources.get(name);
126+
if (existing == null) {
127+
sources.put(name, source);
128+
} else {
129+
source.getProperties().forEach((k, v) -> putIfAbsent(existing.getProperties(), k, v));
130+
}
131+
}
132+
*/
120133
}

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

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,102 @@ void severalRepositoriesIdenticalGroups() throws IOException {
113113
}
114114
}
115115

116+
117+
/*
118+
* A rewrite of severalRepositoriesIdenticalGroups() using "containsOnlyKeys" to show actual vs. expected when assert fails.
119+
*/
120+
@Test
121+
void severalRepositoriesIdenticalGroups_rewritten() throws IOException {
122+
try (InputStream foo = getInputStreamFor("foo")) {
123+
try (InputStream foo2 = getInputStreamFor("foo2")) {
124+
ConfigurationMetadataRepository repo = ConfigurationMetadataRepositoryJsonBuilder.create(foo, foo2)
125+
.build();
126+
127+
// assert all properties are found
128+
assertThat(repo.getAllProperties()).containsOnlyKeys(
129+
"spring.foo.name",
130+
"spring.foo.description",
131+
"spring.foo.counter",
132+
"spring.foo.enabled",
133+
"spring.foo.type");
134+
135+
// we have a single group containing all properties
136+
assertThat(repo.getAllGroups()).containsOnlyKeys("spring.foo");
137+
138+
ConfigurationMetadataGroup group = repo.getAllGroups().get("spring.foo");
139+
assertThat(group.getProperties()).containsOnlyKeys(
140+
"spring.foo.name",
141+
"spring.foo.description",
142+
"spring.foo.counter",
143+
"spring.foo.enabled",
144+
"spring.foo.type");
145+
146+
// the group contains 3 different sources
147+
assertThat(group.getSources()).containsOnlyKeys(
148+
"org.acme.Foo", "org.acme.Foo2", "org.springframework.boot.FooProperties");
149+
150+
assertThat(group.getSources().get("org.acme.Foo").getProperties()).containsOnlyKeys(
151+
"spring.foo.name",
152+
"spring.foo.description");
153+
154+
assertThat(group.getSources().get("org.acme.Foo2").getProperties()).containsOnlyKeys(
155+
"spring.foo.enabled",
156+
"spring.foo.type");
157+
158+
assertThat(group.getSources().get("org.springframework.boot.FooProperties").getProperties()).containsOnlyKeys(
159+
"spring.foo.name",
160+
"spring.foo.counter");
161+
}
162+
}
163+
}
164+
165+
/*
166+
* "foo3" contains the same properties as "foo2" except they refer to a group that already exists in
167+
* "foo1" (same NAME, same TYPE).
168+
*
169+
* This test shows that the union of properties collected from the sources is less than what the group actually
170+
* contains (some properties are missing).
171+
*/
172+
@Test
173+
void severalRepositoriesIdenticalGroups3() throws IOException {
174+
try (InputStream foo = getInputStreamFor("foo")) {
175+
try (InputStream foo3 = getInputStreamFor("foo3")) {
176+
ConfigurationMetadataRepository repo = ConfigurationMetadataRepositoryJsonBuilder.create(foo, foo3)
177+
.build();
178+
179+
assertThat(repo.getAllProperties()).containsOnlyKeys(
180+
"spring.foo.name",
181+
"spring.foo.description",
182+
"spring.foo.counter",
183+
"spring.foo.enabled",
184+
"spring.foo.type");
185+
186+
assertThat(repo.getAllGroups()).containsOnlyKeys("spring.foo");
187+
188+
ConfigurationMetadataGroup group = repo.getAllGroups().get("spring.foo");
189+
assertThat(group.getProperties()).containsOnlyKeys(
190+
"spring.foo.name",
191+
"spring.foo.description",
192+
"spring.foo.counter",
193+
"spring.foo.enabled",
194+
"spring.foo.type");
195+
196+
197+
assertThat(group.getSources()).containsOnlyKeys("org.acme.Foo", "org.springframework.boot.FooProperties");
198+
assertThat(group.getSources().get("org.acme.Foo").getProperties()).containsOnlyKeys(
199+
"spring.foo.name",
200+
"spring.foo.description",
201+
"spring.foo.enabled", // <-- missing although present in repo.getAllProperties()
202+
"spring.foo.type"); // <-- missing although present in repo.getAllProperties()
203+
204+
assertThat(group.getSources().get("org.springframework.boot.FooProperties").getProperties()).containsOnlyKeys(
205+
"spring.foo.name",
206+
"spring.foo.counter");
207+
}
208+
}
209+
}
210+
211+
116212
@Test
117213
void emptyGroups() throws IOException {
118214
try (InputStream in = getInputStreamFor("empty-groups")) {
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": "foo2()",
8+
"description": "This is Foo."
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)