Skip to content

Commit f9c74d9

Browse files
committed
Merge branch '2.1.x'
Closes gh-16565
2 parents 84df37f + e99deb9 commit f9c74d9

File tree

5 files changed

+105
-23
lines changed

5 files changed

+105
-23
lines changed

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

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -108,7 +108,7 @@ private SimpleConfigurationMetadataRepository create(
108108
SimpleConfigurationMetadataRepository repository = new SimpleConfigurationMetadataRepository();
109109
repository.add(metadata.getSources());
110110
for (ConfigurationMetadataItem item : metadata.getItems()) {
111-
ConfigurationMetadataSource source = getSource(metadata, item);
111+
ConfigurationMetadataSource source = metadata.getSource(item);
112112
repository.add(item, source);
113113
}
114114
Map<String, ConfigurationMetadataProperty> allProperties = repository
@@ -146,14 +146,6 @@ private void addMapHints(ConfigurationMetadataProperty property,
146146
property.getHints().getKeyProviders().addAll(hint.getValueProviders());
147147
}
148148

149-
private ConfigurationMetadataSource getSource(RawConfigurationMetadata metadata,
150-
ConfigurationMetadataItem item) {
151-
if (item.getSourceType() != null) {
152-
return metadata.getSource(item.getSourceType());
153-
}
154-
return null;
155-
}
156-
157149
/**
158150
* Create a new builder instance using {@link StandardCharsets#UTF_8} as the default
159151
* charset and the specified json resource.

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

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.boot.configurationmetadata;
1818

1919
import java.util.ArrayList;
20+
import java.util.Comparator;
2021
import java.util.List;
2122

2223
/**
@@ -48,13 +49,16 @@ public List<ConfigurationMetadataSource> getSources() {
4849
return this.sources;
4950
}
5051

51-
public ConfigurationMetadataSource getSource(String type) {
52-
for (ConfigurationMetadataSource source : this.sources) {
53-
if (type.equals(source.getType())) {
54-
return source;
55-
}
52+
public ConfigurationMetadataSource getSource(ConfigurationMetadataItem item) {
53+
if (item.getSourceType() == null) {
54+
return null;
5655
}
57-
return null;
56+
return this.sources.stream()
57+
.filter((candidate) -> item.getSourceType().equals(candidate.getType())
58+
&& item.getId().startsWith(candidate.getGroupId()))
59+
.max(Comparator
60+
.comparingInt((candidate) -> candidate.getGroupId().length()))
61+
.orElse(null);
5862
}
5963

6064
public List<ConfigurationMetadataItem> getItems() {
@@ -72,10 +76,7 @@ public List<ConfigurationMetadataHint> getHints() {
7276
*/
7377
private void resolveName(ConfigurationMetadataItem item) {
7478
item.setName(item.getId()); // fallback
75-
if (item.getSourceType() == null) {
76-
return;
77-
}
78-
ConfigurationMetadataSource source = getSource(item.getSourceType());
79+
ConfigurationMetadataSource source = getSource(item);
7980
if (source != null) {
8081
String groupId = source.getGroupId();
8182
String dottedPrefix = groupId + ".";

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

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -129,6 +129,31 @@ public void emptyGroups() throws IOException {
129129
}
130130
}
131131

132+
@Test
133+
public void multiGroups() throws IOException {
134+
try (InputStream in = getInputStreamFor("multi-groups")) {
135+
ConfigurationMetadataRepository repo = ConfigurationMetadataRepositoryJsonBuilder
136+
.create(in).build();
137+
assertThat(repo.getAllGroups()).containsOnlyKeys("test.group.one.retry",
138+
"test.group.two.retry", "test.group.one.retry.specific");
139+
ConfigurationMetadataGroup one = repo.getAllGroups()
140+
.get("test.group.one.retry");
141+
assertThat(one.getSources()).containsOnlyKeys("com.example.Retry");
142+
assertThat(one.getProperties())
143+
.containsOnlyKeys("test.group.one.retry.enabled");
144+
ConfigurationMetadataGroup two = repo.getAllGroups()
145+
.get("test.group.two.retry");
146+
assertThat(two.getSources()).containsOnlyKeys("com.example.Retry");
147+
assertThat(two.getProperties())
148+
.containsOnlyKeys("test.group.two.retry.enabled");
149+
ConfigurationMetadataGroup oneSpecific = repo.getAllGroups()
150+
.get("test.group.one.retry.specific");
151+
assertThat(oneSpecific.getSources()).containsOnlyKeys("com.example.Retry");
152+
assertThat(oneSpecific.getProperties())
153+
.containsOnlyKeys("test.group.one.retry.specific.enabled");
154+
}
155+
}
156+
132157
@Test
133158
public void builderInstancesAreIsolated() throws IOException {
134159
try (InputStream foo = getInputStreamFor("foo");

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

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -199,6 +199,25 @@ public void deprecatedMetadata() throws IOException {
199199
.isEqualTo(Deprecation.Level.WARNING);
200200
}
201201

202+
@Test
203+
public void multiGroupsMetadata() throws IOException {
204+
RawConfigurationMetadata rawMetadata = readFor("multi-groups");
205+
List<ConfigurationMetadataItem> items = rawMetadata.getItems();
206+
assertThat(items).hasSize(3);
207+
208+
ConfigurationMetadataItem item = items.get(0);
209+
assertThat(item.getName()).isEqualTo("enabled");
210+
assertThat(item.getSourceType()).isEqualTo("com.example.Retry");
211+
212+
ConfigurationMetadataItem item2 = items.get(1);
213+
assertThat(item2.getName()).isEqualTo("enabled");
214+
assertThat(item2.getSourceType()).isEqualTo("com.example.Retry");
215+
216+
ConfigurationMetadataItem item3 = items.get(2);
217+
assertThat(item3.getName()).isEqualTo("enabled");
218+
assertThat(item3.getSourceType()).isEqualTo("com.example.Retry");
219+
}
220+
202221
RawConfigurationMetadata readFor(String path) throws IOException {
203222
return this.reader.read(getInputStreamFor(path), DEFAULT_CHARSET);
204223
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"groups": [
3+
{
4+
"name": "test.group.one.retry",
5+
"type": "com.example.Retry",
6+
"sourceType": "org.acme.config.TestApp",
7+
"sourceMethod": "one()"
8+
},
9+
{
10+
"name": "test.group.two.retry",
11+
"type": "com.example.Retry",
12+
"sourceType": "org.acme.config.TestApp",
13+
"sourceMethod": "two()"
14+
},
15+
{
16+
"name": "test.group.one.retry.specific",
17+
"type": "com.example.Retry",
18+
"sourceType": "org.acme.config.TestApp",
19+
"sourceMethod": "two()"
20+
}
21+
],
22+
"properties": [
23+
{
24+
"name": "test.group.one.retry.enabled",
25+
"type": "java.lang.Boolean",
26+
"description": "Whether publishing retries are enabled.",
27+
"sourceType": "com.example.Retry",
28+
"defaultValue": false
29+
},
30+
{
31+
"name": "test.group.two.retry.enabled",
32+
"type": "java.lang.Boolean",
33+
"description": "Whether publishing retries are enabled.",
34+
"sourceType": "com.example.Retry",
35+
"defaultValue": false
36+
},
37+
{
38+
"name": "test.group.one.retry.specific.enabled",
39+
"type": "java.lang.Boolean",
40+
"description": "Whether publishing retries are enabled.",
41+
"sourceType": "com.example.Retry",
42+
"defaultValue": false
43+
}
44+
]
45+
}

0 commit comments

Comments
 (0)