Skip to content

Commit

Permalink
[TH2-5051] Disable dates serialization as timestamps (#276)
Browse files Browse the repository at this point in the history
The default serialization format for LocalData, Time, DateTime is an array of its components. This was disabled. Now they are serialized as they `toString` representation
  • Loading branch information
OptimumCode authored Sep 7, 2023
1 parent c729897 commit 4745640
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# th2 common library (Java) (5.4.1)
# th2 common library (Java) (5.4.2)

## Usage

Expand Down Expand Up @@ -494,6 +494,13 @@ dependencies {

## Release notes

### 5.4.2-dev

#### Fix

+ The serialization of `LocalTime`, `LocalDate` and `LocalDateTime` instances corrected for th2 transport parsed message.
Old result would look like `[2023,9,7]`. Corrected serialization result looks like `2023-09-07`

### 5.4.1-dev
#### Fix
+ `SubscriberMonitor` is returned from `MessageRouter.subscribe` methods is proxy object to manage RabbitMQ subscribtion without internal listener
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
release_version=5.4.1
release_version=5.4.2
description='th2 common library (Java)'
vcs_url=https://github.com/th2-net/th2-common-j
kapt.include.compile.classpath=false
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.exactpro.th2.common.schema.message.impl.rabbitmq.transport

import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
Expand Down Expand Up @@ -321,6 +322,9 @@ object ParsedMessageCodec : AbstractCodec<ParsedMessage>(30u) {

@JvmField
val MAPPER: ObjectMapper = jacksonObjectMapper().registerModule(JavaTimeModule())
// otherwise, type supported by JavaTimeModule will be serialized as array of date component
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
// this is required to serialize nulls and empty collections
.setSerializationInclusion(JsonInclude.Include.ALWAYS)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ package com.exactpro.th2.common.schema.message.impl.rabbitmq.transport
import io.netty.buffer.ByteBufUtil
import io.netty.buffer.Unpooled
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.DynamicTest
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestFactory
import java.time.Instant
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.LocalTime

class CodecsTest {

Expand Down Expand Up @@ -121,4 +126,39 @@ class CodecsTest {
"unexpected raw body",
)
}

@TestFactory
fun dateTypesTests(): Collection<DynamicTest> {
val testData = listOf(
LocalDate.now(),
LocalTime.now(),
LocalDateTime.now(),
Instant.now(),
)
return testData.map {
DynamicTest.dynamicTest("serializes ${it::class.simpleName} as field") {
val parsedMessage = ParsedMessage.builder().apply {
setId(MessageId.DEFAULT)
setType("test")
setBody(
linkedMapOf(
"field" to it,
)
)
}.build()

val dest = Unpooled.buffer()
ParsedMessageCodec.encode(parsedMessage, dest)
val decoded = ParsedMessageCodec.decode(dest)
assertEquals(0, dest.readableBytes()) { "unexpected bytes left: ${ByteBufUtil.hexDump(dest)}" }

assertEquals(parsedMessage, decoded, "unexpected parsed result decoded")
assertEquals(
"{\"field\":\"$it\"}",
decoded.rawBody.toString(Charsets.UTF_8),
"unexpected raw body",
)
}
}
}
}

0 comments on commit 4745640

Please sign in to comment.