-
Notifications
You must be signed in to change notification settings - Fork 532
YAML: prevent block-scalar value mutations from corrupting siblings #8154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
steve-aom-elliott
wants to merge
5
commits into
main
Choose a base branch
from
fix-yaml-block-scalar-value-mutation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+634
−16
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9becb98
YAML: prevent block-scalar value mutations from corrupting siblings
steve-aom-elliott bc78df8
YAML: keep block-scalar line endings on CRLF files
steve-aom-elliott fd1bf85
YAML: trim block-scalar fix comments and normalize test indentation
steve-aom-elliott 4ed2190
YAML: trim Yaml.Scalar.value javadoc per PR feedback
steve-aom-elliott 92c7333
YAML: drop BlockScalarUtils.withBody trailing-newline fallback
steve-aom-elliott File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
rewrite-yaml/src/main/java/org/openrewrite/yaml/internal/BlockScalarUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| /* | ||
| * Copyright 2026 the original author or authors. | ||
| * <p> | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * <p> | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * <p> | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.openrewrite.yaml.internal; | ||
|
|
||
| import org.openrewrite.internal.StringUtils; | ||
| import org.openrewrite.yaml.tree.Yaml; | ||
|
|
||
| /** | ||
| * Internal helpers for safely mutating FOLDED ({@code >}, {@code >-}, {@code >+}) and | ||
| * LITERAL ({@code |}, {@code |-}, {@code |+}) block scalars: the {@link Yaml.Scalar#value} | ||
| * field carries the block envelope (chomp indicator, indented body, trailing whitespace | ||
| * bounding the next sibling), so a naïve Lombok-generated {@code withValue} replacement | ||
| * corrupts the surrounding structure. | ||
| * | ||
| * <p><b>TODO:</b> promote these onto {@link Yaml.Scalar} as instance {@code getBody} / | ||
| * {@code withBody} methods once enough downstream CLI bundles ship a {@code rewrite-yaml} | ||
| * including them — promoting now would {@code NoSuchMethodError} customer recipes that | ||
| * adopted the new API but load against an older bundled {@code Yaml.Scalar}. | ||
| */ | ||
| public final class BlockScalarUtils { | ||
|
|
||
| private BlockScalarUtils() { | ||
| } | ||
|
|
||
| /** | ||
| * Returns the body content of {@code scalar}, stripped of any style-specific envelope. | ||
| * For PLAIN and quoted styles this returns {@link Yaml.Scalar#value} verbatim. For block | ||
| * styles the body is dedented to column zero with interior line breaks normalized to | ||
| * {@code \n} (so callers can compare or regex against it irrespective of the file's | ||
| * CRLF/LF convention). | ||
| */ | ||
| public static String getBody(Yaml.Scalar scalar) { | ||
| if (!isBlockStyle(scalar)) { | ||
| return scalar.getValue(); | ||
| } | ||
| String value = scalar.getValue(); | ||
| int headerEnd = value.indexOf('\n'); | ||
| if (headerEnd < 0) { | ||
| return ""; | ||
| } | ||
| int bodyEnd = value.length(); | ||
| while (bodyEnd > headerEnd + 1 && Character.isWhitespace(value.charAt(bodyEnd - 1))) { | ||
| bodyEnd--; | ||
| } | ||
| if (bodyEnd <= headerEnd + 1) { | ||
| return ""; | ||
| } | ||
| String bodyRegion = value.substring(headerEnd + 1, bodyEnd); | ||
| int indent = 0; | ||
| while (indent < bodyRegion.length() && bodyRegion.charAt(indent) == ' ') { | ||
| indent++; | ||
| } | ||
| String indentStr = bodyRegion.substring(0, indent); | ||
| String[] lines = bodyRegion.split("\r\n|\r|\n", -1); | ||
| StringBuilder out = new StringBuilder(bodyRegion.length()); | ||
| for (int i = 0; i < lines.length; i++) { | ||
| String line = lines[i]; | ||
| if (indent > 0 && line.startsWith(indentStr)) { | ||
| line = line.substring(indent); | ||
| } | ||
| if (i > 0) { | ||
| out.append('\n'); | ||
| } | ||
| out.append(line); | ||
| } | ||
| return out.toString(); | ||
| } | ||
|
|
||
| /** {@link #withBody(Yaml.Scalar, String, int)} with a 2-space empty-body indent fallback. */ | ||
| public static Yaml.Scalar withBody(Yaml.Scalar scalar, String newBody) { | ||
| return withBody(scalar, newBody, 2); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a copy of {@code scalar} with its body replaced by {@code newBody}. For PLAIN | ||
| * and quoted styles this just sets {@link Yaml.Scalar#value} (via the Lombok-generated | ||
| * {@code withValue}); for block styles the chomp indicator, header newline, body indent, | ||
| * and trailing whitespace are preserved, and each line of {@code newBody} is emitted in | ||
| * the existing value's line-ending convention. {@code defaultIndentSpaces} is used as the | ||
| * body indent width when the existing block scalar has an empty body — pass an | ||
| * {@code IndentsStyle#getIndentSize()} to honor the document's configured indent. | ||
| */ | ||
| public static Yaml.Scalar withBody(Yaml.Scalar scalar, String newBody, int defaultIndentSpaces) { | ||
| if (!isBlockStyle(scalar)) { | ||
| return scalar.withValue(newBody); | ||
| } | ||
| String value = scalar.getValue(); | ||
| int headerEnd = value.indexOf('\n'); | ||
| String header = headerEnd < 0 ? value : value.substring(0, headerEnd + 1); | ||
| String newLine = (headerEnd > 0 && value.charAt(headerEnd - 1) == '\r') ? "\r\n" : "\n"; | ||
| int bodyEnd = value.length(); | ||
| while (bodyEnd > 0 && Character.isWhitespace(value.charAt(bodyEnd - 1))) { | ||
| bodyEnd--; | ||
| } | ||
| String indent; | ||
| if (headerEnd >= 0 && headerEnd + 1 < bodyEnd) { | ||
| int indentEnd = headerEnd + 1; | ||
| while (indentEnd < bodyEnd && value.charAt(indentEnd) == ' ') { | ||
| indentEnd++; | ||
| } | ||
| indent = value.substring(headerEnd + 1, indentEnd); | ||
| if (indent.isEmpty()) { | ||
| indent = StringUtils.repeat(" ", defaultIndentSpaces); | ||
| } | ||
| } else { | ||
| indent = StringUtils.repeat(" ", defaultIndentSpaces); | ||
| } | ||
| String trailing = value.substring(bodyEnd); | ||
| String[] lines = newBody.split("\r\n|\r|\n", -1); | ||
| StringBuilder body = new StringBuilder(); | ||
| for (int i = 0; i < lines.length; i++) { | ||
| if (i > 0) { | ||
| body.append(newLine); | ||
| } | ||
| if (!lines[i].isEmpty()) { | ||
| body.append(indent).append(lines[i]); | ||
| } | ||
| } | ||
| return scalar.withValue(header + body + trailing); | ||
| } | ||
|
|
||
| private static boolean isBlockStyle(Yaml.Scalar scalar) { | ||
| return scalar.getStyle() == Yaml.Scalar.Style.FOLDED || | ||
| scalar.getStyle() == Yaml.Scalar.Style.LITERAL; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not super comfortable with Utils classes. Can you search for where else we would put such convience classes often?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's already a
StringUtilsin the same package. In the ideal, these would have just been put directly intoYaml.Scalarand not had a utility class at all, but given that changes the available method list on a core tree type, it would mean that newer recipes using those methods but running on an older CLI would more than likely end up throwingNoSuchMethodError, unless I've misunderstood the whole parent-loading thing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These should probably just be methods on
Yaml.Scalaritself