Skip to content

Commit f160a92

Browse files
nkwiatkowskinKwiatkowski
andauthored
fix(core): exception when multiple types are in a string pebble expression (#15034)
Co-authored-by: nKwiatkowski <nkwiatkowski@kestra.io>
1 parent 8146600 commit f160a92

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

core/src/main/java/io/kestra/core/runners/VariableRenderer.java

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.kestra.core.exceptions.IllegalVariableEvaluationException;
44
import io.kestra.core.runners.pebble.*;
5+
import io.kestra.core.serializers.JacksonMapper;
56
import io.micronaut.context.ApplicationContext;
67
import io.micronaut.context.annotation.ConfigurationProperties;
78
import io.micronaut.core.annotation.Nullable;
@@ -30,16 +31,16 @@ public class VariableRenderer {
3031
public VariableRenderer(ApplicationContext applicationContext, @Nullable VariableConfiguration variableConfiguration) {
3132
this(applicationContext.getBean(PebbleEngineFactory.class), variableConfiguration);
3233
}
33-
34+
3435
public VariableRenderer(PebbleEngineFactory pebbleEngineFactory, @Nullable VariableConfiguration variableConfiguration) {
3536
this.variableConfiguration = variableConfiguration != null ? variableConfiguration : new VariableConfiguration();
3637
this.pebbleEngine = pebbleEngineFactory.create();
3738
}
38-
39+
3940
public void setPebbleEngine(final PebbleEngine pebbleEngine) {
4041
this.pebbleEngine = pebbleEngine;
4142
}
42-
43+
4344
public static IllegalVariableEvaluationException properPebbleException(PebbleException initialExtension) {
4445
if (initialExtension instanceof AttributeNotFoundException current) {
4546
return new IllegalVariableEvaluationException(
@@ -98,9 +99,27 @@ public Object renderOnce(Object inline, Map<String, Object> variables, boolean s
9899
try {
99100
PebbleTemplate compiledTemplate = this.pebbleEngine.getLiteralTemplate((String) result);
100101

101-
OutputWriter writer = stringify ? new JsonWriter() : new TypedObjectWriter();
102-
compiledTemplate.evaluate(writer, variables);
103-
result = writer.output();
102+
try {
103+
OutputWriter writer = stringify ? new JsonWriter() : new TypedObjectWriter();
104+
compiledTemplate.evaluate(writer, variables);
105+
result = writer.output();
106+
} catch (IllegalArgumentException e) {
107+
//can happen in case of mixed type in string
108+
if (!stringify) {
109+
JsonWriter fallbackWriter = new JsonWriter();
110+
compiledTemplate.evaluate(fallbackWriter, variables);
111+
Object rendered = fallbackWriter.output();
112+
113+
if (rendered instanceof String renderedString) {
114+
result = tryParseJson(renderedString);
115+
} else {
116+
result = rendered;
117+
}
118+
} else {
119+
throw e;
120+
}
121+
}
122+
104123
} catch (IOException | PebbleException e) {
105124
String alternativeRender = this.alternativeRender(e, (String) inline, variables);
106125
if (alternativeRender == null) {
@@ -121,6 +140,14 @@ public Object renderOnce(Object inline, Map<String, Object> variables, boolean s
121140
return result;
122141
}
123142

143+
private Object tryParseJson(String value) {
144+
try {
145+
return JacksonMapper.ofJson().readValue(value, Object.class);
146+
} catch (Exception ignored) {
147+
return value;
148+
}
149+
}
150+
124151
/**
125152
* This method can be used in fallback for rendering an input string.
126153
*

core/src/test/java/io/kestra/core/runners/VariableRendererTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ void shouldRenderContactTypedStringExpression() throws IllegalVariableEvaluation
4747
Assertions.assertEquals("io.kestra.unittest", render);
4848
}
4949

50+
@Test
51+
void shouldRenderMixedTypeInString() throws IllegalVariableEvaluationException {
52+
TestVariableRenderer renderer = new TestVariableRenderer(applicationContext, variableConfiguration);
53+
Object render = renderer.renderTyped("{\"a\": {{[1,2,3 ]}} }", Map.of());
54+
Assertions.assertEquals(Map.of("a", List.of(1, 2, 3)), render);
55+
}
56+
5057
@Test
5158
void shouldRenderContactTypedNumberExpression() throws IllegalVariableEvaluationException {
5259
TestVariableRenderer renderer = new TestVariableRenderer(applicationContext, variableConfiguration);

0 commit comments

Comments
 (0)