Fix source detection in case of multiple candidates

This commit improves the detection of a property source when more than
one group with the same type exist.

Closes gh-16549
pull/16606/head
Stephane Nicoll 6 years ago
parent bb0037f041
commit e99deb95f7

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -108,7 +108,7 @@ public final class ConfigurationMetadataRepositoryJsonBuilder {
SimpleConfigurationMetadataRepository repository = new SimpleConfigurationMetadataRepository(); SimpleConfigurationMetadataRepository repository = new SimpleConfigurationMetadataRepository();
repository.add(metadata.getSources()); repository.add(metadata.getSources());
for (ConfigurationMetadataItem item : metadata.getItems()) { for (ConfigurationMetadataItem item : metadata.getItems()) {
ConfigurationMetadataSource source = getSource(metadata, item); ConfigurationMetadataSource source = metadata.getSource(item);
repository.add(item, source); repository.add(item, source);
} }
Map<String, ConfigurationMetadataProperty> allProperties = repository Map<String, ConfigurationMetadataProperty> allProperties = repository
@ -146,14 +146,6 @@ public final class ConfigurationMetadataRepositoryJsonBuilder {
property.getHints().getKeyProviders().addAll(hint.getValueProviders()); property.getHints().getKeyProviders().addAll(hint.getValueProviders());
} }
private ConfigurationMetadataSource getSource(RawConfigurationMetadata metadata,
ConfigurationMetadataItem item) {
if (item.getSourceType() != null) {
return metadata.getSource(item.getSourceType());
}
return null;
}
/** /**
* Create a new builder instance using {@link StandardCharsets#UTF_8} as the default * Create a new builder instance using {@link StandardCharsets#UTF_8} as the default
* charset and the specified json resource. * charset and the specified json resource.

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,6 +17,7 @@
package org.springframework.boot.configurationmetadata; package org.springframework.boot.configurationmetadata;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator;
import java.util.List; import java.util.List;
/** /**
@ -48,13 +49,16 @@ class RawConfigurationMetadata {
return this.sources; return this.sources;
} }
public ConfigurationMetadataSource getSource(String type) { public ConfigurationMetadataSource getSource(ConfigurationMetadataItem item) {
for (ConfigurationMetadataSource source : this.sources) { if (item.getSourceType() == null) {
if (type.equals(source.getType())) { return null;
return source;
}
} }
return null; return this.sources.stream()
.filter((candidate) -> item.getSourceType().equals(candidate.getType())
&& item.getId().startsWith(candidate.getGroupId()))
.max(Comparator
.comparingInt((candidate) -> candidate.getGroupId().length()))
.orElse(null);
} }
public List<ConfigurationMetadataItem> getItems() { public List<ConfigurationMetadataItem> getItems() {
@ -72,10 +76,7 @@ class RawConfigurationMetadata {
*/ */
private void resolveName(ConfigurationMetadataItem item) { private void resolveName(ConfigurationMetadataItem item) {
item.setName(item.getId()); // fallback item.setName(item.getId()); // fallback
if (item.getSourceType() == null) { ConfigurationMetadataSource source = getSource(item);
return;
}
ConfigurationMetadataSource source = getSource(item.getSourceType());
if (source != null) { if (source != null) {
String groupId = source.getGroupId(); String groupId = source.getGroupId();
String dottedPrefix = groupId + "."; String dottedPrefix = groupId + ".";

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -129,6 +129,31 @@ public class ConfigurationMetadataRepositoryJsonBuilderTests
} }
} }
@Test
public void multiGroups() throws IOException {
try (InputStream in = getInputStreamFor("multi-groups")) {
ConfigurationMetadataRepository repo = ConfigurationMetadataRepositoryJsonBuilder
.create(in).build();
assertThat(repo.getAllGroups()).containsOnlyKeys("test.group.one.retry",
"test.group.two.retry", "test.group.one.retry.specific");
ConfigurationMetadataGroup one = repo.getAllGroups()
.get("test.group.one.retry");
assertThat(one.getSources()).containsOnlyKeys("com.example.Retry");
assertThat(one.getProperties())
.containsOnlyKeys("test.group.one.retry.enabled");
ConfigurationMetadataGroup two = repo.getAllGroups()
.get("test.group.two.retry");
assertThat(two.getSources()).containsOnlyKeys("com.example.Retry");
assertThat(two.getProperties())
.containsOnlyKeys("test.group.two.retry.enabled");
ConfigurationMetadataGroup oneSpecific = repo.getAllGroups()
.get("test.group.one.retry.specific");
assertThat(oneSpecific.getSources()).containsOnlyKeys("com.example.Retry");
assertThat(oneSpecific.getProperties())
.containsOnlyKeys("test.group.one.retry.specific.enabled");
}
}
@Test @Test
public void builderInstancesAreIsolated() throws IOException { public void builderInstancesAreIsolated() throws IOException {
try (InputStream foo = getInputStreamFor("foo"); try (InputStream foo = getInputStreamFor("foo");

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -199,6 +199,25 @@ public class JsonReaderTests extends AbstractConfigurationMetadataTests {
.isEqualTo(Deprecation.Level.WARNING); .isEqualTo(Deprecation.Level.WARNING);
} }
@Test
public void multiGroupsMetadata() throws IOException {
RawConfigurationMetadata rawMetadata = readFor("multi-groups");
List<ConfigurationMetadataItem> items = rawMetadata.getItems();
assertThat(items).hasSize(3);
ConfigurationMetadataItem item = items.get(0);
assertThat(item.getName()).isEqualTo("enabled");
assertThat(item.getSourceType()).isEqualTo("com.example.Retry");
ConfigurationMetadataItem item2 = items.get(1);
assertThat(item2.getName()).isEqualTo("enabled");
assertThat(item2.getSourceType()).isEqualTo("com.example.Retry");
ConfigurationMetadataItem item3 = items.get(2);
assertThat(item3.getName()).isEqualTo("enabled");
assertThat(item3.getSourceType()).isEqualTo("com.example.Retry");
}
RawConfigurationMetadata readFor(String path) throws IOException { RawConfigurationMetadata readFor(String path) throws IOException {
return this.reader.read(getInputStreamFor(path), DEFAULT_CHARSET); return this.reader.read(getInputStreamFor(path), DEFAULT_CHARSET);
} }

@ -0,0 +1,45 @@
{
"groups": [
{
"name": "test.group.one.retry",
"type": "com.example.Retry",
"sourceType": "org.acme.config.TestApp",
"sourceMethod": "one()"
},
{
"name": "test.group.two.retry",
"type": "com.example.Retry",
"sourceType": "org.acme.config.TestApp",
"sourceMethod": "two()"
},
{
"name": "test.group.one.retry.specific",
"type": "com.example.Retry",
"sourceType": "org.acme.config.TestApp",
"sourceMethod": "two()"
}
],
"properties": [
{
"name": "test.group.one.retry.enabled",
"type": "java.lang.Boolean",
"description": "Whether publishing retries are enabled.",
"sourceType": "com.example.Retry",
"defaultValue": false
},
{
"name": "test.group.two.retry.enabled",
"type": "java.lang.Boolean",
"description": "Whether publishing retries are enabled.",
"sourceType": "com.example.Retry",
"defaultValue": false
},
{
"name": "test.group.one.retry.specific.enabled",
"type": "java.lang.Boolean",
"description": "Whether publishing retries are enabled.",
"sourceType": "com.example.Retry",
"defaultValue": false
}
]
}
Loading…
Cancel
Save