This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 39c38489e1c5 CAMEL-27363: camel-yaml-io - an expression with an empty
text keeps it when dumped, so camel validate normalize no longer turns
constant: "" into constant: {} (#30579)
39c38489e1c5 is described below
commit 39c38489e1c501d57f3f182c0fed1543b3b54bec
Author: Claus Ibsen <[email protected]>
AuthorDate: Sun Nov 22 11:31:09 2026 +0100
CAMEL-27363: camel-yaml-io - an expression with an empty text keeps it when
dumped, so camel validate normalize no longer turns constant: "" into constant:
{} (#30579)
The YAML model writer skipped an empty expression text, so setBody with
constant: "" was dumped (and normalized) as constant: {}, which has no
expression. The empty text is now written as expression: "". Only the
method call expression still skips an empty text: the Java DSL gives it
one, as the bean is given by its attributes.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Claus Ibsen <[email protected]>
---
.../org/apache/camel/yaml/out/YamlModelWriter.java | 2 +-
.../camel/yaml/out/YamlModelWriterSupport.java | 16 ++++++++++++-
.../camel/yaml/out/YamlWriterEdgeCasesTest.java | 28 ++++++++++++++++++++++
.../main/resources/velocity/model-yaml-writer.vm | 2 ++
4 files changed, 46 insertions(+), 2 deletions(-)
diff --git
a/core/camel-yaml-io/src/generated/java/org/apache/camel/yaml/out/YamlModelWriter.java
b/core/camel-yaml-io/src/generated/java/org/apache/camel/yaml/out/YamlModelWriter.java
index 8e0543005eb1..93ad006f9d9f 100644
---
a/core/camel-yaml-io/src/generated/java/org/apache/camel/yaml/out/YamlModelWriter.java
+++
b/core/camel-yaml-io/src/generated/java/org/apache/camel/yaml/out/YamlModelWriter.java
@@ -3026,7 +3026,7 @@ public class YamlModelWriter extends
YamlModelWriterSupport {
doWriteAttribute(jo, "method", def.getMethod(), null);
doWriteAttribute(jo, "scope", def.getScope(), "Singleton");
doWriteAttribute(jo, "validate", def.getValidate(), "true");
- doWriteValue(jo, def.getExpression());
+ doWriteNonEmptyValue(jo, def.getExpression());
return jo;
}
protected JsonObject doWriteMvelExpression(MvelExpression def) {
diff --git
a/core/camel-yaml-io/src/main/java/org/apache/camel/yaml/out/YamlModelWriterSupport.java
b/core/camel-yaml-io/src/main/java/org/apache/camel/yaml/out/YamlModelWriterSupport.java
index a83ac7310f3e..bdcb4f74dd46 100644
---
a/core/camel-yaml-io/src/main/java/org/apache/camel/yaml/out/YamlModelWriterSupport.java
+++
b/core/camel-yaml-io/src/main/java/org/apache/camel/yaml/out/YamlModelWriterSupport.java
@@ -63,12 +63,26 @@ public abstract class YamlModelWriterSupport {
}
}
+ /**
+ * The text of an expression. An empty text is written too, as it is the
value of the expression:
+ * <code>constant: {expression: ""}</code> sets an empty body,
<code>constant: {}</code> has no expression.
+ */
protected void doWriteValue(JsonObject jo, String value) {
- if (value != null && !value.isEmpty()) {
+ if (value != null) {
jo.put("expression", value);
}
}
+ /**
+ * The text of a method call expression, when it is not empty: the Java
DSL gives it an empty text, as the bean is
+ * given by the attributes (ref, beanType and method).
+ */
+ protected void doWriteNonEmptyValue(JsonObject jo, String value) {
+ if (value != null && !value.isEmpty()) {
+ doWriteValue(jo, value);
+ }
+ }
+
protected <T> void doWriteChildElement(JsonObject jo, String key, T value,
Function<T, JsonObject> writer) {
if (value != null) {
JsonObject child = writer.apply(value);
diff --git
a/core/camel-yaml-io/src/test/java/org/apache/camel/yaml/out/YamlWriterEdgeCasesTest.java
b/core/camel-yaml-io/src/test/java/org/apache/camel/yaml/out/YamlWriterEdgeCasesTest.java
index 7ce72aa8a0eb..bcb842d21fae 100644
---
a/core/camel-yaml-io/src/test/java/org/apache/camel/yaml/out/YamlWriterEdgeCasesTest.java
+++
b/core/camel-yaml-io/src/test/java/org/apache/camel/yaml/out/YamlWriterEdgeCasesTest.java
@@ -24,6 +24,9 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import org.apache.camel.model.RouteDefinition;
import org.apache.camel.model.dataformat.JsonDataFormat;
+import org.apache.camel.model.language.ConstantExpression;
+import org.apache.camel.model.language.MethodCallExpression;
+import org.apache.camel.model.language.SimpleExpression;
import org.apache.camel.util.json.JsonObject;
import org.apache.camel.yaml.LwModelToYAMLDumper;
import org.junit.jupiter.api.Test;
@@ -47,6 +50,31 @@ public class YamlWriterEdgeCasesTest {
assertThat(steps.get(1).get("log").get("message").asText()).as(yaml).isEqualTo("1e3");
}
+ @Test
+ public void testEmptyExpressionText() throws Exception {
+ RouteDefinition route = new RouteDefinition();
+ route.from("direct:a").setBody(new
ConstantExpression("")).setHeader("foo", new SimpleExpression(""))
+ .bean("myBean", "hello").setBody(new
MethodCallExpression("myBean", "hello"));
+
+ YamlModelWriter writer = new YamlModelWriter();
+ JsonObject jo = writer.writeRouteDefinition(route);
+ String yaml = writer.printAsYaml(List.of(jo));
+
+ JsonNode node = new YAMLMapper().readTree(yaml);
+ JsonNode steps = node.get(0).get("route").get("from").get("steps");
+ // the empty text is the value of the expression: constant: {} would
have no expression
+ JsonNode constant =
steps.get(0).get("setBody").get("expression").get("constant");
+ assertThat(constant.has("expression")).as(yaml).isTrue();
+ assertThat(constant.get("expression").asText()).as(yaml).isEmpty();
+ JsonNode simple =
steps.get(1).get("setHeader").get("expression").get("simple");
+ assertThat(simple.has("expression")).as(yaml).isTrue();
+ assertThat(simple.get("expression").asText()).as(yaml).isEmpty();
+ // the bean of a method call is given by its attributes, its empty
text is not written
+ JsonNode method =
steps.get(3).get("setBody").get("expression").get("method");
+ assertThat(method.get("ref").asText()).as(yaml).isEqualTo("myBean");
+ assertThat(method.has("expression")).as(yaml).isFalse();
+ }
+
@Test
public void testDumpDataFormats() throws Exception {
JsonDataFormat json1 = new JsonDataFormat();
diff --git
a/tooling/maven/camel-package-maven-plugin/src/main/resources/velocity/model-yaml-writer.vm
b/tooling/maven/camel-package-maven-plugin/src/main/resources/velocity/model-yaml-writer.vm
index 127b2be78341..fd86efa6942d 100644
---
a/tooling/maven/camel-package-maven-plugin/src/main/resources/velocity/model-yaml-writer.vm
+++
b/tooling/maven/camel-package-maven-plugin/src/main/resources/velocity/model-yaml-writer.vm
@@ -288,6 +288,8 @@ public class YamlModelWriter extends YamlModelWriterSupport
{
#set( $value =
$mojo.getValues($mojo.getClassAndSuper($clazz)).findFirst() )
#if( $value.isPresent() && $name == "ValueDefinition" )
doWriteAttribute(jo, "value", def.${value.get().getter}(), null);
+ #elseif( $value.isPresent() && $name == "MethodCallExpression" )
+ doWriteNonEmptyValue(jo, def.${value.get().getter}());
#elseif( $value.isPresent() )
doWriteValue(jo, def.${value.get().getter}());
#end