From a6c60335578f4f6b2fa9cdf9255388c084e4f462 Mon Sep 17 00:00:00 2001 From: Tyler Van Gorder <1878529+tkvangorder@users.noreply.github.com> Date: Wed, 4 Dec 2024 09:31:09 -0800 Subject: [PATCH] Add logic to quote yaml scalar value (#644) Co-authored-by: Tyler Van Gorder --- .../spring/ChangeSpringPropertyValue.java | 12 ++++++++-- .../spring/ChangeSpringPropertyValueTest.java | 23 +++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/openrewrite/java/spring/ChangeSpringPropertyValue.java b/src/main/java/org/openrewrite/java/spring/ChangeSpringPropertyValue.java index 3bb55468..ac145f85 100644 --- a/src/main/java/org/openrewrite/java/spring/ChangeSpringPropertyValue.java +++ b/src/main/java/org/openrewrite/java/spring/ChangeSpringPropertyValue.java @@ -23,6 +23,8 @@ import org.openrewrite.properties.tree.Properties; import org.openrewrite.yaml.tree.Yaml; +import java.util.regex.Pattern; + @EqualsAndHashCode(callSuper = false) @Value public class ChangeSpringPropertyValue extends Recipe { @@ -68,7 +70,7 @@ public String getDescription() { Boolean relaxedBinding; @Override - public Validated validate() { + public Validated validate() { return super.validate().and( Validated.test("oldValue", "is required if `regex` is enabled", oldValue, value -> !(Boolean.TRUE.equals(regex) && StringUtils.isNullOrEmpty(value)))); @@ -77,7 +79,8 @@ public Validated validate() { @Override public TreeVisitor getVisitor() { Recipe changeProperties = new org.openrewrite.properties.ChangePropertyValue(propertyKey, newValue, oldValue, regex, relaxedBinding); - Recipe changeYaml = new org.openrewrite.yaml.ChangePropertyValue(propertyKey, newValue, oldValue, regex, relaxedBinding, null); + String yamlValue = quoteValue(newValue) ? "\"" + newValue + "\"" : newValue; + Recipe changeYaml = new org.openrewrite.yaml.ChangePropertyValue(propertyKey, yamlValue, oldValue, regex, relaxedBinding, null); return new TreeVisitor() { @Override public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) { @@ -90,4 +93,9 @@ public TreeVisitor getVisitor() { } }; } + + private static final Pattern scalarNeedsAQuote = Pattern.compile("[^a-zA-Z\\d\\s]*"); + private boolean quoteValue(String value) { + return scalarNeedsAQuote.matcher(value).matches(); + } } diff --git a/src/test/java/org/openrewrite/java/spring/ChangeSpringPropertyValueTest.java b/src/test/java/org/openrewrite/java/spring/ChangeSpringPropertyValueTest.java index 299d6cc3..5b7c4c83 100644 --- a/src/test/java/org/openrewrite/java/spring/ChangeSpringPropertyValueTest.java +++ b/src/test/java/org/openrewrite/java/spring/ChangeSpringPropertyValueTest.java @@ -56,4 +56,27 @@ void regex() { yaml("server.port: 53", "server.port: 8053") ); } + + @Test + void yamlValueQuoted() { + rewriteRun( + spec -> spec.recipe(new ChangeSpringPropertyValue("management.endpoints.web.exposure.include", "*", null, null, null)), + properties("management.endpoints.web.exposure.include=info,health", "management.endpoints.web.exposure.include=*"), + yaml( + """ + management: + endpoints: + web: + exposure: + include: info,health + """, + """ + management: + endpoints: + web: + exposure: + include: "*" + """) + ); + } }