Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): properties as map is now working properly with expression within base maps #6800

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
brian-mulier-p marked this conversation as resolved.
Show resolved Hide resolved
);

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
Loading