Skip to content

Commit

Permalink
fix(core): Flow equalsWithoutRevision don't use serialization to comp…
Browse files Browse the repository at this point in the history
…are flows so that map orders don't matter

closes #6928
  • Loading branch information
brian-mulier-p committed Jan 24, 2025
1 parent 1c575e4 commit f6d62f8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
2 changes: 2 additions & 0 deletions core/src/main/java/io/kestra/core/models/flows/Flow.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
Expand Down Expand Up @@ -57,6 +58,7 @@ public class Flow extends AbstractFlow implements HasUID {
.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);

private static final ObjectMapper WITHOUT_REVISION_OBJECT_MAPPER = NON_DEFAULT_OBJECT_MAPPER.copy()
.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true)
.setAnnotationIntrospector(new JacksonAnnotationIntrospector() {
@Override
public boolean hasIgnoreMarker(final AnnotatedMember m) {
Expand Down
42 changes: 39 additions & 3 deletions core/src/test/java/io/kestra/core/models/flows/FlowTest.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package io.kestra.core.models.flows;

import io.kestra.core.exceptions.InternalException;
import io.kestra.core.junit.annotations.KestraTest;
import io.kestra.core.models.flows.input.StringInput;
import io.kestra.core.models.tasks.Task;
import io.kestra.core.models.validations.ModelValidator;
import io.kestra.core.serializers.YamlParser;
import io.kestra.plugin.core.debug.Return;
import io.kestra.core.utils.TestsUtils;
import io.kestra.core.junit.annotations.KestraTest;
import io.kestra.plugin.core.debug.Return;
import io.kestra.plugin.core.log.Log;
import jakarta.inject.Inject;
import jakarta.validation.ConstraintViolationException;
import org.junit.jupiter.api.Test;

import jakarta.validation.ConstraintViolationException;
import java.io.File;
import java.net.URL;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Optional;

Expand Down Expand Up @@ -154,6 +157,39 @@ void inputValidation() {
assertThat(validate.get().getMessage(), containsString("array: `itemType` cannot be `ARRAY"));
}

// This test is done to ensure the equals is checking the right fields and also make sure the Maps orders don't negate the equality even if they are not the same.
// This can happen for eg. in the persistence layer that don't necessarily track LinkedHashMaps original property orders.
@Test
void equals() {
Flow flowA = baseFlow();
LinkedHashMap<String, Object> triggerInputsReverseOrder = new LinkedHashMap<>();
triggerInputsReverseOrder.put("c", "d");
triggerInputsReverseOrder.put("a", "b");
Flow flowABis = baseFlow().toBuilder().revision(2).triggers(List.of(io.kestra.plugin.core.trigger.Flow.builder().inputs(triggerInputsReverseOrder).build())).build();
assertThat(flowA.equalsWithoutRevision(flowABis), is(true));

Flow flowB = baseFlow().toBuilder().id("b").build();
assertThat(flowA.equalsWithoutRevision(flowB), is(false));

Flow flowAnotherTenant = baseFlow().toBuilder().tenantId("b").build();
assertThat(flowA.equalsWithoutRevision(flowAnotherTenant), is(false));
}

private static Flow baseFlow() {
LinkedHashMap<String, Object> triggerInputs = new LinkedHashMap<>();
triggerInputs.put("a", "b");
triggerInputs.put("c", "d");
return Flow.builder()
.id("a")
.namespace("a")
.revision(1)
.tenantId("a")
.inputs(List.of(StringInput.builder().id("a").build(), StringInput.builder().id("b").build()))
.tasks(List.of(Log.builder().message("a").build(), Log.builder().message("b").build()))
.triggers(List.of(io.kestra.plugin.core.trigger.Flow.builder().inputs(triggerInputs).build()))
.build();
}

private Flow parse(String path) {
URL resource = TestsUtils.class.getClassLoader().getResource(path);
assert resource != null;
Expand Down

0 comments on commit f6d62f8

Please sign in to comment.