From bb5c151ee0004aa400e567a7a9d6fd47289bd5c6 Mon Sep 17 00:00:00 2001 From: Filip Hrisafov Date: Wed, 11 Dec 2019 23:42:59 +0100 Subject: [PATCH] Disable SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS by default See gh-19352 --- .../jackson/JacksonAutoConfiguration.java | 1 + .../jackson/JacksonAutoConfigurationTests.java | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration.java index 79caab4a3e..ed6518cfb8 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration.java @@ -88,6 +88,7 @@ public class JacksonAutoConfiguration { static { Map featureDefaults = new HashMap<>(); featureDefaults.put(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + featureDefaults.put(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS, false); FEATURE_DEFAULTS = Collections.unmodifiableMap(featureDefaults); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfigurationTests.java index 85f751f435..cbdaae8b0e 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfigurationTests.java @@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.jackson; import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; +import java.time.Duration; import java.util.Date; import java.util.HashSet; import java.util.Set; @@ -378,6 +379,15 @@ class JacksonAutoConfigurationTests { }); } + @Test + void writeDurationAsTimestampsDefault() { + this.contextRunner.run((context) -> { + ObjectMapper mapper = context.getBean(ObjectMapper.class); + Duration duration = Duration.ofHours(2); + assertThat(mapper.writeValueAsString(duration)).isEqualTo("\"PT2H\""); + }); + } + @Test void writeWithVisibility() { this.contextRunner