Fix bug where using a YAML with anchors couldn't be loaded

Closes gh-33404
This commit is contained in:
Moritz Halbritter 2022-11-29 11:21:24 +01:00
parent 2ceee789ce
commit b3878e84ae
3 changed files with 19 additions and 2 deletions

View File

@ -79,7 +79,7 @@ class OriginTrackedYamlLoader extends YamlProcessor {
}
List<Map<String, Object>> load() {
final List<Map<String, Object>> result = new ArrayList<>();
List<Map<String, Object>> result = new ArrayList<>();
process((properties, map) -> result.add(getFlattenedMap(map)));
return result;
}
@ -119,7 +119,9 @@ class OriginTrackedYamlLoader extends YamlProcessor {
}
private void replaceMappingNodeKeys(MappingNode node) {
node.setValue(node.getValue().stream().map(KeyScalarNode::get).toList());
List<NodeTuple> newValue = new ArrayList<>();
node.getValue().stream().map(KeyScalarNode::get).forEach(newValue::add);
node.setValue(newValue);
}
private Object constructTrackedObject(Node node, Object value) {

View File

@ -173,6 +173,15 @@ class OriginTrackedYamlLoaderTests {
assertThat(loaded.get("test.b.boot")).hasToString("b");
}
@Test
void loadWhenUsingAnchors() {
Resource resource = new ClassPathResource("anchors.yml", getClass());
this.loader = new OriginTrackedYamlLoader(resource);
Map<String, Object> loaded = this.loader.load().get(0);
assertThat(loaded.get("some.path.config.key")).hasToString("value");
assertThat(loaded.get("some.anotherpath.config.key")).hasToString("value");
}
private OriginTrackedValue getValue(String name) {
if (this.result == null) {
this.result = this.loader.load();

View File

@ -0,0 +1,6 @@
some:
path: &anchor
config:
key: value
anotherpath:
<<: *anchor