Skip to content

Commit af1000e

Browse files
authored
Fixed parser issue to support flow style mappings in sequences. (#6394)
1 parent d76100a commit af1000e

2 files changed

Lines changed: 79 additions & 1 deletion

File tree

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -748,11 +748,30 @@ public MappingWithPrefix(String prefix, @Nullable String startBracePrefix, List<
748748
this.prefix = prefix;
749749
}
750750

751+
public MappingWithPrefix(UUID id, Markers markers, @Nullable String openingBracePrefix, List<Entry> entries, @Nullable String closingBracePrefix, @Nullable Anchor anchor, @Nullable Tag tag, String prefix) {
752+
super(id, markers, openingBracePrefix, entries, closingBracePrefix, anchor, tag);
753+
this.prefix = prefix;
754+
}
755+
751756
@Override
752757
public Mapping withPrefix(String prefix) {
753758
this.prefix = prefix;
754759
return this;
755760
}
761+
762+
@Override
763+
public MappingWithPrefix withClosingBracePrefix(@Nullable String closingBracePrefix) {
764+
return new MappingWithPrefix(
765+
getId(),
766+
getMarkers(),
767+
getOpeningBracePrefix(),
768+
getEntries(),
769+
closingBracePrefix,
770+
getAnchor(),
771+
getTag(),
772+
this.prefix
773+
);
774+
}
756775
}
757776

758777
@Getter
@@ -842,7 +861,7 @@ public Yaml.Mapping visitMapping(Yaml.Mapping mapping, Integer p) {
842861
if (mapping instanceof MappingWithPrefix) {
843862
MappingWithPrefix mappingWithPrefix = (MappingWithPrefix) mapping;
844863
return super.visitMapping(new Yaml.Mapping(mappingWithPrefix.getId(),
845-
mappingWithPrefix.getMarkers(), mappingWithPrefix.getOpeningBracePrefix(), mappingWithPrefix.getEntries(), null, mappingWithPrefix.getAnchor(), mappingWithPrefix.getTag()), p);
864+
mappingWithPrefix.getMarkers(), mappingWithPrefix.getOpeningBracePrefix(), mappingWithPrefix.getEntries(), mappingWithPrefix.getClosingBracePrefix(), mappingWithPrefix.getAnchor(), mappingWithPrefix.getTag()), p);
846865
}
847866
return super.visitMapping(mapping, p);
848867
}

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,4 +559,63 @@ void helmTemplateMatchingDocumentEndParsesCorrectly() {
559559
)
560560
);
561561
}
562+
563+
@Test
564+
void flowStyleMappingsInSequences() {
565+
rewriteRun(
566+
yaml(
567+
"""
568+
tasks:
569+
- {"task_type": "Shell"}
570+
- { "task_type": "Shell2"}
571+
"""
572+
),
573+
yaml(
574+
"""
575+
items:
576+
- name: block-style
577+
type: mapping
578+
- {"name": "flow-style", "type": "mapping"}
579+
- key: another-block
580+
"""
581+
),
582+
yaml(
583+
"""
584+
items:
585+
- {}
586+
- {"key": "value"}
587+
"""
588+
),
589+
yaml(
590+
"""
591+
data:
592+
- {"list": [1, 2, 3], "map": {"nested": "value"}}
593+
- {"array": [{"inner": "map"}]}
594+
"""
595+
),
596+
yaml(
597+
"""
598+
items:
599+
- {
600+
"key": "value",
601+
"another": "test"
602+
}
603+
"""
604+
),
605+
yaml(
606+
"""
607+
items:
608+
- {"key": "value",}
609+
"""
610+
),
611+
yaml(
612+
"""
613+
defaults: &defaults {"type": "default", "enabled": true}
614+
items:
615+
- *defaults
616+
- {"type": "custom"}
617+
"""
618+
)
619+
);
620+
}
562621
}

0 commit comments

Comments
 (0)