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(jdbc): DateTimeFormatter can be reused in the JdbcMapper #1722

Merged
merged 1 commit into from
Jul 10, 2023
Merged
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
6 changes: 6 additions & 0 deletions core/src/test/resources/flows/valids/execution.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
id: execution-start-date
namespace: io.kestra.tests
tasks:
- id: hello
type: io.kestra.core.tasks.debugs.Return
format: "{{ execution.startDate }}"
12 changes: 5 additions & 7 deletions jdbc/src/main/java/io/kestra/jdbc/JdbcMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import java.time.format.DateTimeFormatter;

public abstract class JdbcMapper {
private static final DateTimeFormatter INSTANT_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
.withZone(ZoneOffset.UTC);
private static final DateTimeFormatter ZONED_DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
private static ObjectMapper MAPPER;

public static ObjectMapper of() {
Expand All @@ -24,19 +27,14 @@ public static ObjectMapper of() {
module.addSerializer(Instant.class, new JsonSerializer<>() {
@Override
public void serialize(Instant instant, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeString(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
.withZone(ZoneOffset.UTC)
.format(instant)
);
jsonGenerator.writeString(INSTANT_FORMATTER.format(instant));
}
});

module.addSerializer(ZonedDateTime.class, new JsonSerializer<>() {
@Override
public void serialize(ZonedDateTime instant, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeString(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
.format(instant)
);
jsonGenerator.writeString(ZONED_DATE_TIME_FORMATTER.format(instant));
}
});

Expand Down
8 changes: 8 additions & 0 deletions jdbc/src/test/java/io/kestra/jdbc/runner/JdbcRunnerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.matchesPattern;

@MicronautTest(transactional = false)
@TestInstance(TestInstance.Lifecycle.PER_CLASS) // must be per-class to allow calling once init() which took a lot of time
Expand Down Expand Up @@ -241,4 +242,11 @@ public void pauseRunDelay() throws Exception {
public void pauseRunTimeout() throws Exception {
pauseTest.runTimeout(runnerUtils);
}

@Test
void executionDate() throws TimeoutException {
Execution execution = runnerUtils.runOne("io.kestra.tests", "execution-start-date", null, null, Duration.ofSeconds(60));

assertThat((String) execution.getTaskRunList().get(0).getOutputs().get("value"), matchesPattern("^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{3}Z"));
}
}