Skip to content

Commit 74e06e8

Browse files
committed
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
1 parent 23e5fd7 commit 74e06e8

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/MetadataCollector.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 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.
@@ -98,7 +98,7 @@ public ConfigurationMetadata getMetadata() {
9898
List<ItemMetadata> items = this.previousMetadata.getItems();
9999
for (ItemMetadata item : items) {
100100
if (shouldBeMerged(item)) {
101-
metadata.add(item);
101+
metadata.addIfMissing(item);
102102
}
103103
}
104104
}

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/ConfigurationMetadata.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,23 @@ public ConfigurationMetadata(ConfigurationMetadata metadata) {
6262
* @param itemMetadata the meta-data to add
6363
*/
6464
public void add(ItemMetadata itemMetadata) {
65-
add(this.items, itemMetadata.getName(), itemMetadata);
65+
add(this.items, itemMetadata.getName(), itemMetadata, false);
66+
}
67+
68+
/**
69+
* Add item meta-data if it's not already present.
70+
* @param itemMetadata the meta-data to add
71+
*/
72+
public void addIfMissing(ItemMetadata itemMetadata) {
73+
add(this.items, itemMetadata.getName(), itemMetadata, true);
6674
}
6775

6876
/**
6977
* Add item hint.
7078
* @param itemHint the item hint to add
7179
*/
7280
public void add(ItemHint itemHint) {
73-
add(this.hints, itemHint.getName(), itemHint);
81+
add(this.hints, itemHint.getName(), itemHint, false);
7482
}
7583

7684
/**
@@ -131,13 +139,15 @@ protected void mergeItemMetadata(ItemMetadata metadata) {
131139
}
132140
}
133141
else {
134-
add(this.items, metadata.getName(), metadata);
142+
add(this.items, metadata.getName(), metadata, false);
135143
}
136144
}
137145

138-
private <K, V> void add(Map<K, List<V>> map, K key, V value) {
146+
private <K, V> void add(Map<K, List<V>> map, K key, V value, boolean ifMissing) {
139147
List<V> values = map.computeIfAbsent(key, (k) -> new ArrayList<>());
140-
values.add(value);
148+
if (!ifMissing || values.isEmpty()) {
149+
values.add(value);
150+
}
141151
}
142152

143153
private ItemMetadata findMatchingItemMetadata(ItemMetadata metadata) {

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/metadata/Metadata.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 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.
@@ -25,6 +25,7 @@
2525
import org.hamcrest.collection.IsMapContaining;
2626

2727
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata.ItemType;
28+
import org.springframework.util.Assert;
2829
import org.springframework.util.ObjectUtils;
2930

3031
/**
@@ -131,7 +132,7 @@ private String createDescription() {
131132

132133
@Override
133134
public boolean matches(ConfigurationMetadata value) {
134-
ItemMetadata itemMetadata = getFirstItemWithName(value, this.name);
135+
ItemMetadata itemMetadata = getItemWithName(value, this.name);
135136
if (itemMetadata == null) {
136137
return false;
137138
}
@@ -207,13 +208,15 @@ public MetadataItemCondition withNoDeprecation() {
207208
this.description, this.defaultValue, null);
208209
}
209210

210-
private ItemMetadata getFirstItemWithName(ConfigurationMetadata metadata, String name) {
211+
private ItemMetadata getItemWithName(ConfigurationMetadata metadata, String name) {
212+
ItemMetadata result = null;
211213
for (ItemMetadata item : metadata.getItems()) {
212214
if (item.isOfItemType(this.itemType) && name.equals(item.getName())) {
213-
return item;
215+
Assert.state(result == null, () -> "Duplicate item found for " + name);
216+
result = item;
214217
}
215218
}
216-
return null;
219+
return result;
217220
}
218221

219222
}

0 commit comments

Comments
 (0)