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
This commit is contained in:
Phillip Webb
2020-10-28 21:55:35 -07:00
parent 23e5fd798c
commit 74e06e8e6a
3 changed files with 25 additions and 12 deletions

View File

@@ -1,5 +1,5 @@
/*
* 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");
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@ import org.assertj.core.api.Condition;
import org.hamcrest.collection.IsMapContaining;
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata.ItemType;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
/**
@@ -131,7 +132,7 @@ public final class Metadata {
@Override
public boolean matches(ConfigurationMetadata value) {
ItemMetadata itemMetadata = getFirstItemWithName(value, this.name);
ItemMetadata itemMetadata = getItemWithName(value, this.name);
if (itemMetadata == null) {
return false;
}
@@ -207,13 +208,15 @@ public final class Metadata {
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()) {
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;
}
}