Commit 078e1469 authored by Phillip Webb's avatar Phillip Webb

Filter empty YAML documents

Update `OriginTrackedYamlLoader` so that empty documents are filtered
from the result. Prior to this commit, our origin wrapper would confuse
the YAML processor and cause empty documents to be included in the Map
with a key of "document" and no value.

Closes gh-22493
parent fdc6e801
......@@ -19,6 +19,7 @@ package org.springframework.boot.env;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
......@@ -82,6 +83,15 @@ class OriginTrackedYamlLoader extends YamlProcessor {
*/
private class OriginTrackingConstructor extends SafeConstructor {
@Override
public Object getData() throws NoSuchElementException {
Object data = super.getData();
if (data instanceof CharSequence && ((CharSequence) data).length() == 0) {
return null;
}
return data;
}
@Override
protected Object constructObject(Node node) {
if (node instanceof ScalarNode) {
......
......@@ -128,6 +128,13 @@ class OriginTrackedYamlLoaderTests {
assertThatExceptionOfType(ConstructorException.class).isThrownBy(this.loader::load);
}
@Test
void emptyDocumentes() {
this.loader = new OriginTrackedYamlLoader(new ClassPathResource("test-empty-yaml.yml", getClass()));
List<Map<String, Object>> loaded = this.loader.load();
assertThat(loaded).isEmpty();
}
private OriginTrackedValue getValue(String name) {
if (this.result == null) {
this.result = this.loader.load();
......
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