Skip to content

Commit

Permalink
Filter empty YAML documents
Browse files Browse the repository at this point in the history
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
  • Loading branch information
philwebb committed Jul 23, 2020
1 parent fdc6e80 commit 078e146
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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;

Expand Down Expand Up @@ -82,6 +83,15 @@ List<Map<String, Object>> load() {
*/
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ void unsupportedType() throws Exception {
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();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
---

---
---

0 comments on commit 078e146

Please sign in to comment.