Skip to content

Add YAML StringUtils.quoteIfNeeded() for safe scalar quoting#7416

Merged
Jenson3210 merged 2 commits intomainfrom
Jenson3210/yaml-quote-strings
Apr 17, 2026
Merged

Add YAML StringUtils.quoteIfNeeded() for safe scalar quoting#7416
Jenson3210 merged 2 commits intomainfrom
Jenson3210/yaml-quote-strings

Conversation

@Jenson3210
Copy link
Copy Markdown
Contributor

@Jenson3210 Jenson3210 commented Apr 17, 2026

Summary

  • Adds StringUtils.quoteIfNeeded(String) in org.openrewrite.yaml.internal that quotes YAML scalar values only when syntactically necessary
  • Quotes when: value is empty, starts with an indicator character, contains : or #, starts with ---/..., has leading/trailing whitespace, or contains control characters
  • Uses minimal quoting: single quotes by default, double quotes only when escape sequences are needed (e.g., value contains ' or control chars)
  • Does not quote valid typed values like true, null, 123 — those are valid YAML as-is

Motivation

  • This utility addresses the root cause of openrewrite/rewrite-spring#999, where AddSpringProperty fails to quote values containing colons. The quoteValue method in rewrite-spring uses a regex that only matches strings made entirely of special characters, missing mixed values like TODO: Follow this link https://example.com/page.

With this utility in rewrite-yaml, rewrite-spring (and other modules) can replace ad-hoc quoting logic with a spec-compliant implementation.

Test plan

  • 43 unit tests covering: already-quoted passthrough, plain strings, empty strings, indicator characters, colon-space/space-hash patterns, document markers, whitespace, control characters, double-quote escaping, URLs, and the exact scenario from rewrite-spring#999
  • ./gradlew :rewrite-yaml:test --tests "org.openrewrite.yaml.internal.StringUtilsTest"

Add a utility method that quotes YAML scalar values only when
syntactically necessary (indicator characters, colon-space, space-hash,
document markers, whitespace, control characters). Uses minimal quoting:
single quotes by default, double quotes only when escape sequences are
needed.
Collapse the 43 near-identical @test methods into 8 @ParameterizedTest
methods (plus one @test for the single empty-string case) grouped by
the existing category headers, so the focus is on the (input, expected)
table rather than repeated assertion boilerplate.
@github-project-automation github-project-automation bot moved this from In Progress to Ready to Review in OpenRewrite Apr 17, 2026
@Jenson3210 Jenson3210 merged commit 10ffca7 into main Apr 17, 2026
1 check passed
@Jenson3210 Jenson3210 deleted the Jenson3210/yaml-quote-strings branch April 17, 2026 14:34
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Apr 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants