Skip to content

Commit 63cdb68

Browse files
committed
GH-1189 Fix regression with ObjectMapper configurationb
Resolves #1189
1 parent f637b98 commit 63cdb68

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/config/ContextFunctionCatalogAutoConfiguration.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,9 @@ private JsonMapper jackson(ApplicationContext context) {
223223
}
224224
catch (Exception e) {
225225
mapper = new ObjectMapper();
226+
mapper.registerModule(new JavaTimeModule());
226227
}
227-
mapper.registerModule(new JavaTimeModule());
228+
228229
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
229230
mapper.configure(DeserializationFeature.FAIL_ON_TRAILING_TOKENS, true);
230231
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

spring-cloud-function-context/src/test/java/org/springframework/cloud/function/utils/JsonMapperTests.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package org.springframework.cloud.function.utils;
1818

19+
import java.nio.charset.StandardCharsets;
20+
import java.time.ZonedDateTime;
21+
import java.util.Date;
1922
import java.util.List;
2023
import java.util.stream.Stream;
2124

@@ -27,9 +30,13 @@
2730
import org.junit.jupiter.params.ParameterizedTest;
2831
import org.junit.jupiter.params.provider.MethodSource;
2932

33+
import org.springframework.boot.SpringApplication;
34+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
3035
import org.springframework.cloud.function.json.GsonMapper;
3136
import org.springframework.cloud.function.json.JacksonMapper;
3237
import org.springframework.cloud.function.json.JsonMapper;
38+
import org.springframework.context.ApplicationContext;
39+
import org.springframework.context.annotation.Configuration;
3340
import org.springframework.core.ResolvableType;
3441

3542
import static org.assertj.core.api.Assertions.assertThat;
@@ -67,6 +74,16 @@ public void objectNode_isJsonStringRepresentsCollection() {
6774
assertThat(JsonMapper.isJsonStringRepresentsCollection(nodeAsString)).isFalse();
6875
}
6976

77+
// see https://github.com/spring-cloud/spring-cloud-function/issues/1189
78+
@Test
79+
public void testJsonDateTimeConversion() {
80+
ApplicationContext context = SpringApplication.run(EmptyConfiguration.class);
81+
JsonMapper jsonMapper = context.getBean(JsonMapper.class);
82+
StringVsTimestamp dom = new StringVsTimestamp("2024-10-16T16:13:29.964361+02:00");
83+
String convertedJson = new String(jsonMapper.toJson(dom), StandardCharsets.UTF_8);
84+
assertThat(convertedJson).contains("\"zonedDateTime\":\"2024-10-16T16:13:29.964361+02:00\"");
85+
}
86+
7087
@ParameterizedTest
7188
@MethodSource("params")
7289
public void vanillaArray(JsonMapper mapper) {
@@ -140,4 +157,48 @@ public void setValue(String value) {
140157

141158
}
142159

160+
@EnableAutoConfiguration
161+
@Configuration
162+
static class EmptyConfiguration {
163+
164+
}
165+
166+
static class StringVsTimestamp {
167+
168+
private String type;
169+
170+
private Date date;
171+
172+
private ZonedDateTime zonedDateTime;
173+
174+
StringVsTimestamp(String zonedDate) {
175+
type = "StringVsTimestamp";
176+
date = new Date();
177+
zonedDateTime = ZonedDateTime.parse(zonedDate);
178+
}
179+
180+
public String getType() {
181+
return type;
182+
}
183+
184+
public void setType(String type) {
185+
this.type = type;
186+
}
187+
188+
public Date getDate() {
189+
return date;
190+
}
191+
192+
public void setDate(Date date) {
193+
this.date = date;
194+
}
195+
196+
public ZonedDateTime getZonedDateTime() {
197+
return zonedDateTime;
198+
}
199+
200+
public void setZonedDateTime(ZonedDateTime zonedDateTime) {
201+
this.zonedDateTime = zonedDateTime;
202+
}
203+
}
143204
}

0 commit comments

Comments
 (0)