diff --git a/src/main/java/com/floragunn/codova/documents/DocReader.java b/src/main/java/com/floragunn/codova/documents/DocReader.java index 11681e588684443dfd96d40a1ff81e2267874266..d2072072e2984e636a859c29842bad547c038902 100644 --- a/src/main/java/com/floragunn/codova/documents/DocReader.java +++ b/src/main/java/com/floragunn/codova/documents/DocReader.java @@ -69,22 +69,25 @@ public class DocReader { private final JsonParser parser; private final Format format; private final int handleDottedAttributeNamesAsPathsFromDepth; + private final Object emptyDocumentFallback; private Deque<Object> nodeStack = new ArrayDeque<>(); private Object currentNode; private Object topNode; private String currentAttributeName = null; - public DocReader(Format format, JsonParser parser, int handleDottedAttributeNamesAsPathsFromDepth) { + public DocReader(Format format, JsonParser parser, int handleDottedAttributeNamesAsPathsFromDepth, Object emptyDocumentFallback) { this.format = format; this.parser = parser; this.handleDottedAttributeNamesAsPathsFromDepth = handleDottedAttributeNamesAsPathsFromDepth; + this.emptyDocumentFallback = emptyDocumentFallback; } public DocReader(Format format, JsonParser parser) { this.format = format; this.parser = parser; this.handleDottedAttributeNamesAsPathsFromDepth = -1; + this.emptyDocumentFallback = null; } public Object read() throws IOException, DocumentParseException { @@ -167,7 +170,11 @@ public class DocReader { parser.clearCurrentToken(); if (tokenCount == 0) { - throw new DocumentParseException(new ValidationError(null, "The document is empty").expected(format.getName() + " document")); + if (emptyDocumentFallback == null) { + throw new DocumentParseException(new ValidationError(null, "The document is empty").expected(format.getName() + " document")); + } else { + return emptyDocumentFallback; + } } return topNode; @@ -252,6 +259,7 @@ public class DocReader { private JsonFactory jsonFactory; private Format format; private int handleDottedAttributeNamesAsPathsFromDepth = -1; + private Object emptyDocumentFallback; DocReaderBuilder(Format format, JsonFactory jsonFactory) { this.format = format; @@ -260,13 +268,13 @@ public class DocReader { public Object read(Reader in) throws DocumentParseException, IOException { try (JsonParser parser = jsonFactory.createParser(in)) { - return new DocReader(format, parser, handleDottedAttributeNamesAsPathsFromDepth).read(); + return new DocReader(format, parser, handleDottedAttributeNamesAsPathsFromDepth, emptyDocumentFallback).read(); } } public Object read(String string) throws DocumentParseException { try (JsonParser parser = jsonFactory.createParser(string)) { - return new DocReader(format, parser, handleDottedAttributeNamesAsPathsFromDepth).read(); + return new DocReader(format, parser, handleDottedAttributeNamesAsPathsFromDepth, emptyDocumentFallback).read(); } catch (IOException e) { throw new RuntimeException(e); } @@ -286,7 +294,7 @@ public class DocReader { public Object read(InputStream in) throws DocumentParseException, IOException { try (JsonParser parser = jsonFactory.createParser(in)) { - return new DocReader(format, parser, handleDottedAttributeNamesAsPathsFromDepth).read(); + return new DocReader(format, parser, handleDottedAttributeNamesAsPathsFromDepth, emptyDocumentFallback).read(); } } @@ -333,6 +341,10 @@ public class DocReader { return this; } + public DocReaderBuilder fallbackForEmptyDocuments(Object emptyDocumentFallback) { + this.emptyDocumentFallback = emptyDocumentFallback; + return this; + } } } diff --git a/src/test/java/com/floragunn/codova/documents/BasicDocReaderTest.java b/src/test/java/com/floragunn/codova/documents/BasicDocReaderTest.java index 9380076bf61e0d41db3a75b71bcf101f0c851935..128ff4d5950dc34f07f092d0bae20dbf166985b2 100644 --- a/src/test/java/com/floragunn/codova/documents/BasicDocReaderTest.java +++ b/src/test/java/com/floragunn/codova/documents/BasicDocReaderTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 floragunn GmbH + * Copyright 2021-2022 floragunn GmbH * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,6 +21,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import java.util.Arrays; +import java.util.Collections; import org.junit.Assert; import org.junit.Test; @@ -117,4 +118,21 @@ public class BasicDocReaderTest { } } + @Test + public void emptyDocument() { + String yaml = "# Empty"; + + try { + DocReader.yaml().read(yaml); + Assert.fail("Parsing should fail with 'The document is empty; expected: YAML document' message"); + } catch (Exception e) { + Assert.assertTrue(e.getMessage(), e.getMessage().contains("The document is empty")); + } + } + + @Test + public void emptyDocument_fallback() throws Exception { + String yaml = "# Empty"; + assertEquals(Collections.emptyMap(), DocReader.yaml().fallbackForEmptyDocuments(Collections.emptyMap()).read(yaml)); + } }