Home > database >  Spring webtestclient serializes dates to timestamps instead of dates
Spring webtestclient serializes dates to timestamps instead of dates

Time:12-10

I am trying to check if the data I get back from the webtestclient is the same as what I expect. But the ZonedDateTime from the User data class is not just shown as a date but as a timestamp while I have applied Jackson to the webtestclient codecs. Example: 2021-12-09T16:39:43.225207700 01:00 is converted to 1639064383.225207700 while I expect nothing to change. Could someone maybe explain what I am doing wrong. (Using this jackson config when calling this endpoint outside of the test gives the date not as timestamp)

WebTestClientUtil:

object WebTestClientUtil {
    fun webTestClient(routerFunction: RouterFunction<ServerResponse>): WebTestClient {
        return WebTestClient
            .bindToRouterFunction(routerFunction)
            .configureClient()
            .codecs { configurer: ClientCodecConfigurer ->
                configurer.defaultCodecs().jackson2JsonEncoder(Jackson2JsonEncoder(objectMapper, MediaType.APPLICATION_JSON))
                configurer.defaultCodecs().jackson2JsonDecoder(Jackson2JsonDecoder(objectMapper, MediaType.APPLICATION_JSON))
            }
            .build()
    }
}

Testcase:

@Test
fun `get user when given correct data`() {
    val user = GlobalMocks.mockedUser
    coEvery { userRepository.getUserWithData(any()) } returns user

    val result = webTestClient.get()
        .uri("/api/v1/user/${user.userId}")
        .exchange()
        .expectStatus().is2xxSuccessful
        .expectBody<Result>().returnResult().responseBody?.payload


    assertEquals(user, result)
}

data class Result(
    val payload: User
)

Jackson config:

class JacksonConfig {
    companion object {
        val serializationDateFormat: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXX")
        val deserializationDateFormat: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm[:ss][XXX][X]")
        val objectMapper = jacksonObjectMapper().applyDefaultSettings()

        private fun ObjectMapper.applyDefaultSettings() =
            apply {
                disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)
                disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
                disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)

                enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE)
                setSerializationInclusion(JsonInclude.Include.NON_NULL)

                registerModule(Jdk8Module())
                registerModule(ParameterNamesModule())
                registerModule(JsonComponentModule())
                registerModule(
                    JavaTimeModule().apply {
                        addSerializer(ZonedDateTime::class.java, ZonedDateTimeSerializer(serializationDateFormat))
                        addDeserializer(ZonedDateTime::class.java, ZonedDateTimeDeserializer())
                    }
                )
            }
    }

    class ZonedDateTimeDeserializer : JsonDeserializer<ZonedDateTime>() {
        override fun deserialize(jsonParser: JsonParser, deserializationContext: DeserializationContext): ZonedDateTime {
            val epochTime = jsonParser.text.toLongOrNull()
            return if (epochTime != null) {
                ZonedDateTime.ofInstant(
                    Instant.ofEpochSecond(epochTime),
                    currentZone
                )
            } else {
                ZonedDateTime.parse(jsonParser.text, deserializationDateFormat)
            }
        }
    }
}

EDIT: Also found this issue which makes me think that it might have something to do with bindToRouterFunction.

CodePudding user response:

You need to define an ObjectMapper bean so that the auto-configured one is not used:

@Configuration(proxyBeanMethods = false)
class JacksonConfiguration {

    companion object {
        val serializationDateFormat: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXX")
        val deserializationDateFormat: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm[:ss][XXX][X]")
    }

    @Bean
    fun objectMapper() = jacksonObjectMapper().applyDefaultSettings ()

    private fun ObjectMapper.applyDefaultSettings() =
        apply {
            disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)
            disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
            disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)

            enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE)
            setSerializationInclusion(JsonInclude.Include.NON_NULL)

            registerModule(Jdk8Module())
            registerModule(ParameterNamesModule())
            registerModule(JsonComponentModule())
            registerModule(
                JavaTimeModule().apply {
                    addSerializer(ZonedDateTime::class.java, ZonedDateTimeSerializer(serializationDateFormat))
                    addDeserializer(ZonedDateTime::class.java, ZonedDateTimeDeserializer())
                }
            )
        }

    class ZonedDateTimeDeserializer : JsonDeserializer<ZonedDateTime>() {
        override fun deserialize(jsonParser: JsonParser, deserializationContext: DeserializationContext): ZonedDateTime {
            val epochTime = jsonParser.text.toLongOrNull()
            return if (epochTime != null) {
                ZonedDateTime.ofInstant(
                    Instant.ofEpochSecond(epochTime),
                    currentZone
                )
            } else {
                ZonedDateTime.parse(jsonParser.text, deserializationDateFormat)
            }
        }
    }
}
  • Related