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
This commit is contained in:
Phillip Webb 2020-06-23 16:50:00 -07:00
parent fdc6e80163
commit 078e146983
3 changed files with 22 additions and 0 deletions

View File

@ -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) {

View File

@ -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();

View File

@ -0,0 +1,5 @@
---
---
---
---