Skip to content

Commit

Permalink
fix read of sequence starting at dash
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Aug 15, 2024
1 parent 34b4679 commit 640404a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/main/java/com/amihaiemil/eoyaml/RtYamlInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
3 changes: 2 additions & 1 deletion src/main/java/com/amihaiemil/eoyaml/WellIndented.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ public Iterator<YamlLine> 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;
}
Expand Down
43 changes: 42 additions & 1 deletion src/test/java/com/amihaiemil/eoyaml/RtYamlInputTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
* @author Mihai Andronache ([email protected])
* @version $Id$
* @since 1.0.1
* @checkstyle ExecutableStatementCount (2000 lines)
* @checkstyle ExecutableStatementCount (3000 lines)
*/
public final class RtYamlInputTest {

Expand Down Expand Up @@ -889,6 +889,47 @@ public void shouldReadSequenceWhenFirstKeyIsOnTheSameLineFourthCase()
);
}

/**
* Corner case when key:value is on the same with sequence marker.
* <a href="https://github.com/decorators-squad/eo-yaml/issues/447">#447</a>
* and based on
* <a href="https://github.com/decorators-squad/eo-yaml/pull/416">PR</a>
* @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).
Expand Down
9 changes: 9 additions & 0 deletions src/test/resources/issue_447_bug_mapping_case_5.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
key: value
seqMap:
- alfa: b
- this:
- - sequence
- starting
- at
- dash
key2: value

0 comments on commit 640404a

Please sign in to comment.