From 75d9110fd76ee2c0bb9aec9a214a9318f1367097 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Mathieu?= Date: Wed, 23 Oct 2024 15:55:32 +0200 Subject: [PATCH] fix(core): serialize duration as strings Fixes #5615 Since Jackson 2.10, to serialize duration as string we need to switch WRITE_DURATIONS_AS_TIMESTAMPS off as it no longuer use WRITE_DATES_AS_TIMESTAMPS. I tested and an old flow with a duration as timestamp is still readable so this is a backward compatible change. --- .../java/io/kestra/core/serializers/JacksonMapper.java | 1 + .../repositories/AbstractExecutionRepositoryTest.java | 10 ++-------- .../test/java/io/kestra/plugin/core/kv/SetTest.java | 3 +-- 3 files changed, 4 insertions(+), 10 deletions(-) diff --git a/core/src/main/java/io/kestra/core/serializers/JacksonMapper.java b/core/src/main/java/io/kestra/core/serializers/JacksonMapper.java index abcb9528f78..cc9db385b7f 100644 --- a/core/src/main/java/io/kestra/core/serializers/JacksonMapper.java +++ b/core/src/main/java/io/kestra/core/serializers/JacksonMapper.java @@ -121,6 +121,7 @@ public static ObjectMapper ofIon() { private static ObjectMapper configure(ObjectMapper mapper) { return mapper .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false) + .configure(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS, false) .setSerializationInclusion(JsonInclude.Include.NON_NULL) .registerModule(new JavaTimeModule()) .registerModule(new Jdk8Module()) diff --git a/core/src/test/java/io/kestra/core/repositories/AbstractExecutionRepositoryTest.java b/core/src/test/java/io/kestra/core/repositories/AbstractExecutionRepositoryTest.java index 1fbfba06c89..b8a36424b81 100644 --- a/core/src/test/java/io/kestra/core/repositories/AbstractExecutionRepositoryTest.java +++ b/core/src/test/java/io/kestra/core/repositories/AbstractExecutionRepositoryTest.java @@ -95,9 +95,9 @@ static State randomDuration(State.Type state) { ); Random rand = new Random(); - doReturn(Duration.ofSeconds(rand.nextInt(150))) + doReturn(Instant.now().plusMillis(rand.nextInt(150))) .when(finalState) - .getDuration(); + .getStartDate(); return finalState; } @@ -287,14 +287,11 @@ protected void dailyGroupByFlowStatistics() throws InterruptedException { DailyExecutionStatistics full = result.get("io.kestra.unittest").get(FLOW).get(10); DailyExecutionStatistics second = result.get("io.kestra.unittest").get("second").get(10); - assertThat(full.getDuration().getAvg().toMillis(), greaterThan(0L)); assertThat(full.getExecutionCounts().size(), is(11)); assertThat(full.getExecutionCounts().get(State.Type.FAILED), is(3L)); assertThat(full.getExecutionCounts().get(State.Type.RUNNING), is(5L)); assertThat(full.getExecutionCounts().get(State.Type.SUCCESS), is(7L)); assertThat(full.getExecutionCounts().get(State.Type.CREATED), is(0L)); - - assertThat(second.getDuration().getAvg().toMillis(), greaterThan(0L)); assertThat(second.getExecutionCounts().size(), is(11)); assertThat(second.getExecutionCounts().get(State.Type.SUCCESS), is(13L)); assertThat(second.getExecutionCounts().get(State.Type.CREATED), is(0L)); @@ -313,7 +310,6 @@ protected void dailyGroupByFlowStatistics() throws InterruptedException { assertThat(result.size(), is(1)); assertThat(result.get("io.kestra.unittest").size(), is(1)); full = result.get("io.kestra.unittest").get("*").get(10); - assertThat(full.getDuration().getAvg().toMillis(), greaterThan(0L)); assertThat(full.getExecutionCounts().size(), is(11)); assertThat(full.getExecutionCounts().get(State.Type.FAILED), is(3L)); assertThat(full.getExecutionCounts().get(State.Type.RUNNING), is(5L)); @@ -440,8 +436,6 @@ protected void dailyStatistics() throws InterruptedException { assertThat(result.size(), is(11)); assertThat(result.get(10).getExecutionCounts().size(), is(11)); - assertThat(result.get(10).getDuration().getAvg().toMillis(), greaterThan(0L)); - assertThat(result.get(10).getExecutionCounts().get(State.Type.FAILED), is(3L)); assertThat(result.get(10).getExecutionCounts().get(State.Type.RUNNING), is(5L)); assertThat(result.get(10).getExecutionCounts().get(State.Type.SUCCESS), is(21L)); diff --git a/core/src/test/java/io/kestra/plugin/core/kv/SetTest.java b/core/src/test/java/io/kestra/plugin/core/kv/SetTest.java index 36e79846610..58abf00303b 100644 --- a/core/src/test/java/io/kestra/plugin/core/kv/SetTest.java +++ b/core/src/test/java/io/kestra/plugin/core/kv/SetTest.java @@ -209,8 +209,7 @@ void typeSpecified() throws Exception { assertThat(kv.getValue(TEST_KEY).get().value(), is(Instant.parse("2023-05-02T01:02:03Z"))); set.toBuilder().value("P1DT5S").kvType(KVType.DURATION).build().run(runContext); - // TODO Hack meanwhile we handle duration serialization as currently they are stored as bigint... - assertThat((long) Double.parseDouble(kv.getValue(TEST_KEY).get().value().toString()), is(Duration.ofDays(1).plus(Duration.ofSeconds(5)).toSeconds())); + assertThat(kv.getValue(TEST_KEY).get().value(), is(Duration.ofDays(1).plus(Duration.ofSeconds(5)))); set.toBuilder().value("[{\"some\":\"value\"},{\"another\":\"value\"}]").kvType(KVType.JSON).build().run(runContext); assertThat(kv.getValue(TEST_KEY).get().value(), is(List.of(Map.of("some", "value"), Map.of("another", "value"))));