Skip to content

Commit

Permalink
fix(core): properties as map is now working properly with expression …
Browse files Browse the repository at this point in the history
…within base maps
  • Loading branch information
brian-mulier-p committed Jan 17, 2025
1 parent 960a1bb commit dd04042
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
21 changes: 17 additions & 4 deletions core/src/main/java/io/kestra/core/models/property/Property.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
import io.kestra.core.exceptions.IllegalVariableEvaluationException;
import io.kestra.core.runners.RunContext;
import io.kestra.core.serializers.JacksonMapper;
import lombok.*;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.NoArgsConstructor;

import java.io.IOException;
import java.io.Serial;
Expand Down Expand Up @@ -168,10 +171,20 @@ public static <T, I> T asList(Property<T> property, RunContext runContext, Class
*/
public static <T, K,V> T asMap(Property<T> property, RunContext runContext, Class<K> keyClass, Class<V> valueClass) throws IllegalVariableEvaluationException {
if (property.value == null) {
String rendered = runContext.render(property.expression);
JavaType type = MAPPER.getTypeFactory().constructMapType(Map.class, keyClass, valueClass);
JavaType targetMapType = MAPPER.getTypeFactory().constructMapType(Map.class, keyClass, valueClass);

try {
property.value = MAPPER.readValue(rendered, type);
String trimmedExpression = property.expression.trim();
// We need to detect if the expression is already a map or if it's a pebble expression (for eg. referencing a variable containing a map).
// Doing that allows us to, if it's an expression, first render then read it as a map.
if (trimmedExpression.startsWith("{{") && trimmedExpression.endsWith("}}")) {
property.value = MAPPER.readValue(runContext.render(property.expression), targetMapType);
}
// Otherwise if it's already a map we read it as a map first then render it from run context which handle map rendering by rendering each entry of the map (otherwise it will fail with nested expressions in values for eg.)
else {
Map asRawMap = MAPPER.readValue(property.expression, Map.class);
property.value = MAPPER.convertValue(runContext.render(asRawMap), targetMapType);
}
} catch (JsonProcessingException e) {
throw new IllegalVariableEvaluationException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static io.kestra.core.utils.Rethrow.throwFunction;

@SuperBuilder
@ToString
Expand Down Expand Up @@ -52,21 +55,21 @@ public class DynamicPropertyExampleTask extends Task implements RunnableTask<Dyn
public Output run(RunContext runContext) throws Exception {
String value = String.format(
"%s - %s - %s - %s",
runContext.render(string).as(String.class).orElseThrow(),
runContext.render(number).as(Integer.class).orElseThrow(),
runContext.render(withDefault).as(String.class).orElseThrow(),
runContext.render(someDuration).as(Duration.class).orElseThrow()
runContext.render(string).as(String.class).orElse(null),
runContext.render(number).as(Integer.class).orElse(null),
runContext.render(withDefault).as(String.class).orElse(null),
runContext.render(someDuration).as(Duration.class).orElse(null)
);

Level level =runContext.render(this.level).as(Level.class).orElseThrow();
Level level = runContext.render(this.level).as(Level.class).orElse(null);

List<String> list = runContext.render(items).asList(String.class);

Map<String, String> map = runContext.render(properties).asMap(String.class, String.class);

List<Message> outputMessages = data.flux(runContext, Message.class, message -> Message.fromMap(message))
List<Message> outputMessages = Optional.ofNullable(data).map(throwFunction(d -> d.flux(runContext, Message.class, message -> Message.fromMap(message))
.collectList()
.block();
.block())).orElse(null);

return Output.builder()
.value(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,29 @@ void of() {
assertThat(prop, notNullValue());
}

@Test
void arrayAndMapToRender() throws Exception {
var task = DynamicPropertyExampleTask.builder()
.items(new Property<>("{{renderOnce(listToRender)}}"))
.properties(new Property<>("{{renderOnce(mapToRender)}}"))
.build();
var runContext = runContextFactory.of(Map.ofEntries(
entry("arrayValueToRender", "arrayValue1"),
entry("listToRender", List.of("{{arrayValueToRender}}", "arrayValue2")),
entry("mapKeyToRender", "mapKey1"),
entry("mapValueToRender", "mapValue1"),
entry("mapToRender", Map.of("{{mapKeyToRender}}", "{{mapValueToRender}}", "mapKey2", "mapValue2"))
));

var output = task.run(runContext);

assertThat(output, notNullValue());
assertThat(output.getList(), containsInAnyOrder("arrayValue1", "arrayValue2"));
assertThat(output.getMap(), aMapWithSize(2));
assertThat(output.getMap().get("mapKey1"), is("mapValue1"));
assertThat(output.getMap().get("mapKey2"), is("mapValue2"));
}

@Builder
@Getter
private static class TestObj {
Expand Down

0 comments on commit dd04042

Please sign in to comment.