22
33import io .kestra .core .exceptions .IllegalVariableEvaluationException ;
44import io .kestra .core .runners .pebble .*;
5+ import io .kestra .core .serializers .JacksonMapper ;
56import io .micronaut .context .ApplicationContext ;
67import io .micronaut .context .annotation .ConfigurationProperties ;
78import 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 *
0 commit comments