Skip to content

Commit 8912b00

Browse files
Fix YAML print idempotency for asterisk placeholders inside block scalars (#7466)
The asterisk placeholder preprocessing (introduced in #6626 to handle credential values like `*** REMOVED ***`) replaces matched text with UUIDs before parsing and restores them afterward. Restoration for the variable/asterisk UUID map used exact-match against the scalar value, so when the regex matched inside a multi-line block scalar — e.g. markdown bold like `**CI Alert**` on a line containing `: ` from an emoji — the UUID was embedded within a longer scalar value and never restored, breaking print idempotency. Align restoration with the helm and single-brace template maps on the same code path, which already use `contains`/`replace`. UUIDs are random per-parse and only produced by the parser, so substring replacement is unambiguous.
1 parent a9ab1ae commit 8912b00

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,14 @@ private Yaml.Documents parseFromInput(Path sourceFile, EncodingDetectingInputStr
345345
scalarValue = scalarValue.replace(entry.getKey(), entry.getValue());
346346
}
347347
}
348-
// Then check for variable UUIDs (these are exact matches)
349-
if (variableByUuid.containsKey(scalarValue)) {
350-
scalarValue = variableByUuid.get(scalarValue);
348+
// Then restore any variable/asterisk placeholder UUIDs. The UUID may be
349+
// the entire scalar value (e.g. `key: @var@`) or embedded within a longer
350+
// scalar (e.g. markdown bold text inside a block scalar that happened to
351+
// match the asterisk placeholder regex).
352+
for (Map.Entry<String, String> entry : variableByUuid.entrySet()) {
353+
if (scalarValue.contains(entry.getKey())) {
354+
scalarValue = scalarValue.replace(entry.getKey(), entry.getValue());
355+
}
351356
}
352357

353358
Yaml.Scalar.Style style;

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,25 @@ void asteriskPlaceholdersWithAnchors() {
266266
);
267267
}
268268

269+
@Test
270+
void asteriskPatternInsideBlockScalar() {
271+
// Markdown bold text inside a literal block scalar must not be mangled
272+
// by the asterisk placeholder preprocessing.
273+
rewriteRun(
274+
yaml(
275+
"""
276+
jobs:
277+
alert:
278+
steps:
279+
- name: Post alert
280+
run: |
281+
echo ":rotating_light: **CI Alert**: repo has been failing for **5 days**"
282+
echo "- **Branch:** main"
283+
"""
284+
)
285+
);
286+
}
287+
269288
@Test
270289
void pipeLiteralInASequenceWithDoubleQuotes() {
271290
rewriteRun(

0 commit comments

Comments
 (0)