Commit 74e06e8e authored by Phillip Webb's avatar Phillip Webb

Refine MetadataCollector logic

Update `MetadataCollector` merge logic so that previous items are no
longer added if the current round contains a property of the same name.

Fixes gh-23916
parent 23e5fd79
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2020 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.
...@@ -98,7 +98,7 @@ public class MetadataCollector { ...@@ -98,7 +98,7 @@ public class MetadataCollector {
List<ItemMetadata> items = this.previousMetadata.getItems(); List<ItemMetadata> items = this.previousMetadata.getItems();
for (ItemMetadata item : items) { for (ItemMetadata item : items) {
if (shouldBeMerged(item)) { if (shouldBeMerged(item)) {
metadata.add(item); metadata.addIfMissing(item);
} }
} }
} }
......
...@@ -62,7 +62,15 @@ public class ConfigurationMetadata { ...@@ -62,7 +62,15 @@ public class ConfigurationMetadata {
* @param itemMetadata the meta-data to add * @param itemMetadata the meta-data to add
*/ */
public void add(ItemMetadata itemMetadata) { public void add(ItemMetadata itemMetadata) {
add(this.items, itemMetadata.getName(), itemMetadata); add(this.items, itemMetadata.getName(), itemMetadata, false);
}
/**
* Add item meta-data if it's not already present.
* @param itemMetadata the meta-data to add
*/
public void addIfMissing(ItemMetadata itemMetadata) {
add(this.items, itemMetadata.getName(), itemMetadata, true);
} }
/** /**
...@@ -70,7 +78,7 @@ public class ConfigurationMetadata { ...@@ -70,7 +78,7 @@ public class ConfigurationMetadata {
* @param itemHint the item hint to add * @param itemHint the item hint to add
*/ */
public void add(ItemHint itemHint) { public void add(ItemHint itemHint) {
add(this.hints, itemHint.getName(), itemHint); add(this.hints, itemHint.getName(), itemHint, false);
} }
/** /**
...@@ -131,13 +139,15 @@ public class ConfigurationMetadata { ...@@ -131,13 +139,15 @@ public class ConfigurationMetadata {
} }
} }
else { else {
add(this.items, metadata.getName(), metadata); add(this.items, metadata.getName(), metadata, false);
} }
} }
private <K, V> void add(Map<K, List<V>> map, K key, V value) { private <K, V> void add(Map<K, List<V>> map, K key, V value, boolean ifMissing) {
List<V> values = map.computeIfAbsent(key, (k) -> new ArrayList<>()); List<V> values = map.computeIfAbsent(key, (k) -> new ArrayList<>());
values.add(value); if (!ifMissing || values.isEmpty()) {
values.add(value);
}
} }
private ItemMetadata findMatchingItemMetadata(ItemMetadata metadata) { private ItemMetadata findMatchingItemMetadata(ItemMetadata metadata) {
......
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2020 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.
...@@ -25,6 +25,7 @@ import org.assertj.core.api.Condition; ...@@ -25,6 +25,7 @@ import org.assertj.core.api.Condition;
import org.hamcrest.collection.IsMapContaining; import org.hamcrest.collection.IsMapContaining;
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata.ItemType; import org.springframework.boot.configurationprocessor.metadata.ItemMetadata.ItemType;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
/** /**
...@@ -131,7 +132,7 @@ public final class Metadata { ...@@ -131,7 +132,7 @@ public final class Metadata {
@Override @Override
public boolean matches(ConfigurationMetadata value) { public boolean matches(ConfigurationMetadata value) {
ItemMetadata itemMetadata = getFirstItemWithName(value, this.name); ItemMetadata itemMetadata = getItemWithName(value, this.name);
if (itemMetadata == null) { if (itemMetadata == null) {
return false; return false;
} }
...@@ -207,13 +208,15 @@ public final class Metadata { ...@@ -207,13 +208,15 @@ public final class Metadata {
this.description, this.defaultValue, null); this.description, this.defaultValue, null);
} }
private ItemMetadata getFirstItemWithName(ConfigurationMetadata metadata, String name) { private ItemMetadata getItemWithName(ConfigurationMetadata metadata, String name) {
ItemMetadata result = null;
for (ItemMetadata item : metadata.getItems()) { for (ItemMetadata item : metadata.getItems()) {
if (item.isOfItemType(this.itemType) && name.equals(item.getName())) { if (item.isOfItemType(this.itemType) && name.equals(item.getName())) {
return item; Assert.state(result == null, () -> "Duplicate item found for " + name);
result = item;
} }
} }
return null; return result;
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment