Skip to content

Commit 0328d28

Browse files
dsgrievetimtebeek
andauthored
fix for incorrect parsing of tags as first line of mapping sequence (#6281)
* fix for incorrect parsing of tags as first line of mapping sequence * Validate the tag is correctly parsed --------- Co-authored-by: Tim te Beek <tim@moderne.io>
1 parent 7954b4b commit 0328d28

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

rewrite-yaml/src/main/java/org/openrewrite/yaml/YamlParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ private Yaml.Documents parseFromInput(Path sourceFile, EncodingDetectingInputStr
197197
}
198198

199199
String fullPrefix = reader.readStringFromBuffer(lastEnd, event.getEndMark().getIndex() - 1);
200-
int startIndex = commentAwareIndexOf(':', fullPrefix) + 1;
200+
int startIndex = commentAwareIndexOf(Arrays.asList(':', '-'), fullPrefix) + 1;
201201
Yaml.Tag tag = null;
202202
if (mappingStartEvent.getTag() != null) {
203203
String prefixAfterColon = fullPrefix.substring(startIndex);

rewrite-yaml/src/test/java/org/openrewrite/yaml/YamlParserTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,23 @@ void withAnchorSequenceOnRootLevel() {
466466
);
467467
}
468468

469+
@Test
470+
void parseTagsCorrectlyOnFirstLineOfMappingEntry() {
471+
rewriteRun(
472+
yaml(
473+
"""
474+
- !SOMETAG
475+
a: b
476+
""",
477+
spec -> spec.afterRecipe(docs -> {
478+
Yaml.Sequence sequence = (Yaml.Sequence) docs.getDocuments().getFirst().getBlock();
479+
Yaml.Mapping mapping = (Yaml.Mapping) sequence.getEntries().getFirst().getBlock();
480+
assertThat(mapping.getTag().getName()).isEqualTo("SOMETAG");
481+
})
482+
)
483+
);
484+
}
485+
469486
@Issue("https://github.com/moderneinc/customer-requests/issues/1471")
470487
@Test
471488
void yamlWithDocumentEndMarker() {

0 commit comments

Comments
 (0)