Skip to content

Commit

Permalink
GH-1189 Fix regression with ObjectMapper configurationb
Browse files Browse the repository at this point in the history
Resolves #1189
  • Loading branch information
olegz committed Oct 16, 2024
1 parent 6dee668 commit dbafb30
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,15 @@ private JsonMapper gson(ApplicationContext context) {
}

private JsonMapper jackson(ApplicationContext context) {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
ObjectMapper mapper;
try {
mapper = context.getBean(ObjectMapper.class);
}
catch (Exception e) {
mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
}

mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
mapper.configure(DeserializationFeature.FAIL_ON_TRAILING_TOKENS, true);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package org.springframework.cloud.function.utils;

import java.nio.charset.StandardCharsets;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Stream;

Expand All @@ -27,9 +30,13 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.function.json.GsonMapper;
import org.springframework.cloud.function.json.JacksonMapper;
import org.springframework.cloud.function.json.JsonMapper;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.ResolvableType;

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

// see https:/spring-cloud/spring-cloud-function/issues/1189
@Test
public void testJsonDateTimeConversion() {
ApplicationContext context = SpringApplication.run(EmptyConfiguration.class);
JsonMapper jsonMapper = context.getBean(JsonMapper.class);
StringVsTimestamp dom = new StringVsTimestamp("2024-10-16T16:13:29.964361+02:00");
String convertedJson = new String(jsonMapper.toJson(dom), StandardCharsets.UTF_8);
assertThat(convertedJson).contains("\"zonedDateTime\":\"2024-10-16T16:13:29.964361+02:00\"");
}

@ParameterizedTest
@MethodSource("params")
public void vanillaArray(JsonMapper mapper) {
Expand Down Expand Up @@ -140,4 +157,48 @@ public void setValue(String value) {

}

@EnableAutoConfiguration
@Configuration
static class EmptyConfiguration {

}

static class StringVsTimestamp {

private String type;

private Date date;

private ZonedDateTime zonedDateTime;

StringVsTimestamp(String zonedDate) {
type = "StringVsTimestamp";
date = new Date();
zonedDateTime = ZonedDateTime.parse(zonedDate);
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}

public ZonedDateTime getZonedDateTime() {
return zonedDateTime;
}

public void setZonedDateTime(ZonedDateTime zonedDateTime) {
this.zonedDateTime = zonedDateTime;
}
}
}

0 comments on commit dbafb30

Please sign in to comment.