Skip to content

Commit

Permalink
fix(core): keep key order when rendering a Map
Browse files Browse the repository at this point in the history
  • Loading branch information
yvrng authored and loicmathieu committed Jul 15, 2024
1 parent 5749c3e commit 8792b6b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public Map<String, Object> render(Map<String, Object> in, Map<String, Object> va
}

public Map<String, Object> render(Map<String, Object> in, Map<String, Object> variables, boolean recursive) throws IllegalVariableEvaluationException {
Map<String, Object> map = new HashMap<>();
Map<String, Object> map = new LinkedHashMap<>();

for (Map.Entry<String, Object> r : in.entrySet()) {
String key = this.render(r.getKey(), variables);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package io.kestra.core.runners;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;

import io.kestra.core.exceptions.IllegalVariableEvaluationException;
import io.micronaut.context.ApplicationContext;
import io.kestra.core.junit.annotations.KestraTest;
import jakarta.inject.Inject;
import java.util.LinkedHashMap;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand All @@ -18,13 +22,35 @@ class VariableRendererTest {
@Inject
VariableRenderer.VariableConfiguration variableConfiguration;

@Inject
VariableRenderer variableRenderer;

@Test
void shouldRenderUsingAlternativeRendering() throws IllegalVariableEvaluationException {
TestVariableRenderer renderer = new TestVariableRenderer(applicationContext, variableConfiguration);
String render = renderer.render("{{ dummy }}", Map.of());
Assertions.assertEquals("result", render);
}

@Test
void shouldKeepKeyOrderWhenRenderingMap() throws IllegalVariableEvaluationException {
final Map<String, Object> input = new LinkedHashMap<>();
input.put("foo-1", "A");
input.put("foo-2", "B");

final Map<String, Object> input_value3 = new LinkedHashMap<>();
input_value3.put("bar-1", "C");
input_value3.put("bar-2", "D");
input_value3.put("bar-3", "E");
//
input.put("foo-3", input_value3);

final Map<String, Object> result = variableRenderer.render(input, Map.of());
assertThat(result.keySet(), contains("foo-1", "foo-2", "foo-3"));

final Map<String, Object> result_value3 = (Map<String, Object>) result.get("foo-3");
assertThat(result_value3.keySet(), contains("bar-1", "bar-2", "bar-3"));
}

public static class TestVariableRenderer extends VariableRenderer {

Expand Down

0 comments on commit 8792b6b

Please sign in to comment.