diff --git a/src/main/java/com/amihaiemil/eoyaml/RtYamlInput.java b/src/main/java/com/amihaiemil/eoyaml/RtYamlInput.java index 8bd2c84b..7fa6a383 100644 --- a/src/main/java/com/amihaiemil/eoyaml/RtYamlInput.java +++ b/src/main/java/com/amihaiemil/eoyaml/RtYamlInput.java @@ -152,7 +152,7 @@ private AllYamlLines readInput() throws IOException { int number = 0; while ((line = reader.readLine()) != null) { - if (this.mappingStartsAtDash(line)) { + if (this.mappingOrSequenceStartsAtDash(line)) { // if line starts with a sequence ("-") and the first // key:value is unescaped and on the same line with the @@ -206,11 +206,13 @@ private AllYamlLines readInput() throws IOException { * @param line Line. * @return Boolean. */ - private boolean mappingStartsAtDash(final String line){ + private boolean mappingOrSequenceStartsAtDash(final String line){ //line without indentation. final String trimmed = line.trim(); final boolean escapedScalar = trimmed.matches("^\\s*-\\s*\".*\"$") || trimmed.matches("^\\s*-\\s*'.*'$"); - return trimmed.matches("^\\s*-.+:\\s.*$") && !escapedScalar; + return (trimmed.matches("^\\s*-.+:\\s.*$") + || trimmed.matches("^\\s*-.+-\\s.*$")) + && !escapedScalar; } } diff --git a/src/main/java/com/amihaiemil/eoyaml/WellIndented.java b/src/main/java/com/amihaiemil/eoyaml/WellIndented.java index 6f50d7cc..0101ae4d 100644 --- a/src/main/java/com/amihaiemil/eoyaml/WellIndented.java +++ b/src/main/java/com/amihaiemil/eoyaml/WellIndented.java @@ -103,7 +103,8 @@ public Iterator iterator() { withinBlockScalar = true; } int prevIndent = previous.indentation(); - if(previous.trimmed().matches("^\\s*-.*:(|\\s.*)$")) { + if(previous.trimmed().matches("^\\s*-.*:(|\\s.*)$") + || previous.trimmed().matches("^\\s*-.*-(|\\s.*)$")) { withinBlockScalar = false; prevIndent += 2; } diff --git a/src/test/java/com/amihaiemil/eoyaml/RtYamlInputTest.java b/src/test/java/com/amihaiemil/eoyaml/RtYamlInputTest.java index fbed76ce..4da3fddc 100644 --- a/src/test/java/com/amihaiemil/eoyaml/RtYamlInputTest.java +++ b/src/test/java/com/amihaiemil/eoyaml/RtYamlInputTest.java @@ -48,7 +48,7 @@ * @author Mihai Andronache (amihaiemil@gmail.com) * @version $Id$ * @since 1.0.1 - * @checkstyle ExecutableStatementCount (2000 lines) + * @checkstyle ExecutableStatementCount (3000 lines) */ public final class RtYamlInputTest { @@ -889,6 +889,47 @@ public void shouldReadSequenceWhenFirstKeyIsOnTheSameLineFourthCase() ); } + /** + * Corner case when key:value is on the same with sequence marker. + * #447 + * and based on + * PR + * @throws IOException If something is wrong. + */ + @Test + public void shouldReadSequenceWhenFirstNodeIsOnTheSameLine() + throws IOException { + final String fileName = "src/test/resources/issue_447_bug_mapping" + + "_case_5.yml"; + final YamlMapping root = Yaml.createYamlInput(new File(fileName)) + .readYamlMapping(); + MatcherAssert.assertThat( + root, + Matchers.equalTo( + Yaml.createYamlMappingBuilder() + .add("key", "value") + .add("seqMap", Yaml.createYamlSequenceBuilder() + .add(Yaml.createYamlMappingBuilder() + .add("alfa", "b") + .build()) + .add(Yaml.createYamlMappingBuilder() + .add("this", + Yaml.createYamlSequenceBuilder().add( + Yaml.createYamlSequenceBuilder() + .add("sequence") + .add("starting") + .add("at") + .add("dash") + .build() + ).build()) + .build()) + .build()) + .add("key2", "value") + .build() + ) + ); + } + /** * Corner case when key:value is on the same line as the sequence marker * and key contains dashes (which is the sequence marker itself). diff --git a/src/test/resources/issue_447_bug_mapping_case_5.yml b/src/test/resources/issue_447_bug_mapping_case_5.yml new file mode 100644 index 00000000..42edd516 --- /dev/null +++ b/src/test/resources/issue_447_bug_mapping_case_5.yml @@ -0,0 +1,9 @@ +key: value +seqMap: + - alfa: b + - this: + - - sequence + - starting + - at + - dash +key2: value \ No newline at end of file