Skip to content

Commit

Permalink
Merge pull request #70 from esensar/hotfix/0.4.4
Browse files Browse the repository at this point in the history
Hotfix/0.4.4
  • Loading branch information
esensar committed Dec 20, 2021
2 parents f68e407 + 73d921a commit 5d704d6
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 9 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ All notable changes to this project will be documented in this file. This change

## [Unreleased]

## [0.4.4] - 2021-12-20
- Added support for ignoring unknown keys ([#69][i69])

```
val msgPack = MsgPack(MsgPackConfiguration.default.copy(ignoreUnknownKeys = true))
```

## [0.4.3] - 2021-12-17
- Added windows target using cross-compilation ([#60][i60])
- Added support for enum serialization ([#63][i63])
Expand Down Expand Up @@ -75,13 +82,15 @@ MsgPack.default.encodeToByteArray(...)
- `MsgPackDynamicSerializer` as placeholder for future [contextual serializer](https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serializers.md#contextual-serialization)
- Full implementation of msgpack spec excluding extension types and bin format family

[Unreleased]: https://github.com/esensar/kotlinx-serialization-msgpack/compare/0.4.2...main
[Unreleased]: https://github.com/esensar/kotlinx-serialization-msgpack/compare/0.4.4...main
[0.2.0]: https://github.com/esensar/kotlinx-serialization-msgpack/compare/0.1.0...0.2.0
[0.2.1]: https://github.com/esensar/kotlinx-serialization-msgpack/compare/0.2.0...0.2.1
[0.3.0]: https://github.com/esensar/kotlinx-serialization-msgpack/compare/0.2.1...0.3.0
[0.4.0]: https://github.com/esensar/kotlinx-serialization-msgpack/compare/0.3.0...0.4.0
[0.4.1]: https://github.com/esensar/kotlinx-serialization-msgpack/compare/0.4.0...0.4.1
[0.4.2]: https://github.com/esensar/kotlinx-serialization-msgpack/compare/0.4.1...0.4.2
[0.4.3]: https://github.com/esensar/kotlinx-serialization-msgpack/compare/0.4.2...0.4.3
[0.4.4]: https://github.com/esensar/kotlinx-serialization-msgpack/compare/0.4.3...0.4.4
[i6]: https://github.com/esensar/kotlinx-serialization-msgpack/issues/6
[i9]: https://github.com/esensar/kotlinx-serialization-msgpack/issues/9
[i10]: https://github.com/esensar/kotlinx-serialization-msgpack/issues/10
Expand All @@ -97,4 +106,5 @@ MsgPack.default.encodeToByteArray(...)
[i57]: https://github.com/esensar/kotlinx-serialization-msgpack/issues/57
[i60]: https://github.com/esensar/kotlinx-serialization-msgpack/issues/60
[i63]: https://github.com/esensar/kotlinx-serialization-msgpack/issues/63
[i69]: https://github.com/esensar/kotlinx-serialization-msgpack/issues/69
[p40]: https://github.com/esensar/kotlinx-serialization-msgpack/pull/40
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ org.gradle.parallel=true
org.gradle.caching=true
org.gradle.jvmargs=-XX:MaxMetaspaceSize=1g

version=0.4.3
version=0.4.4
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ data class MsgPackConfiguration(
* Useful when combined with strict mode type
* true by default
*/
val preventOverflows: Boolean = true
val preventOverflows: Boolean = true,
/**
* Prevent exceptions when unknown keys are found when deserializing
* Useful when only parts of data are of interest
* false by default
*/
val ignoreUnknownKeys: Boolean = false
) {
companion object {
@JvmStatic
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ensarsarajcic.kotlinx.serialization.msgpack.internal

import com.ensarsarajcic.kotlinx.serialization.msgpack.MsgPackConfiguration
import com.ensarsarajcic.kotlinx.serialization.msgpack.MsgPackNullableDynamicSerializer
import com.ensarsarajcic.kotlinx.serialization.msgpack.stream.MsgPackDataInputBuffer
import com.ensarsarajcic.kotlinx.serialization.msgpack.types.MsgPackType
import com.ensarsarajcic.kotlinx.serialization.msgpack.utils.joinToNumber
Expand Down Expand Up @@ -29,8 +30,14 @@ internal class BasicMsgPackDecoder(
if (descriptor.kind in arrayOf(StructureKind.CLASS, StructureKind.OBJECT)) {
val next = dataBuffer.peekSafely()
if (next != null && MsgPackType.String.isString(next)) {
val fieldName = kotlin.runCatching { decodeString() }.getOrNull() ?: return CompositeDecoder.DECODE_DONE
return descriptor.getElementIndex(fieldName)
val fieldName = kotlin.runCatching { decodeString() }.getOrNull() ?: return CompositeDecoder.UNKNOWN_NAME
val index = descriptor.getElementIndex(fieldName)
return if (index == CompositeDecoder.UNKNOWN_NAME && configuration.ignoreUnknownKeys) {
MsgPackNullableDynamicSerializer.deserialize(this)
decodeElementIndex(descriptor)
} else {
index
}
} else {
return CompositeDecoder.DECODE_DONE
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,15 @@ internal class MsgPackDecoderTest {
}
}

@Test
fun testDecodeIgnoreUnknownKeys() {
TestData.unknownKeysTestPairs.forEach { (input, result) ->
val decoder = BasicMsgPackDecoder(MsgPackConfiguration.default.copy(ignoreUnknownKeys = true), SerializersModule {}, input.hexStringToByteArray().toMsgPackBuffer())
val serializer = TestData.SampleClass.serializer()
assertEquals(result, serializer.deserialize(decoder))
}
}

private fun <RESULT> testPairs(decodeFunction: MsgPackDecoder.() -> RESULT, vararg pairs: Pair<String, RESULT>) {
pairs.forEach { (input, result) ->
MsgPackDecoder(BasicMsgPackDecoder(MsgPackConfiguration.default, SerializersModule {}, input.hexStringToByteArray().toMsgPackBuffer())).also {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,22 @@ internal class MsgPackTest {
)
}

@Test
fun testDecodeIgnoreUnknownKeys() {
fun <T> testPairs(dataList: Array<Pair<String, T>>, serializer: KSerializer<T>) {
dataList.forEach { (value, expectedResult) ->
val result = MsgPack(
configuration = MsgPackConfiguration(ignoreUnknownKeys = true)
).decodeFromByteArray(serializer, value.hexStringToByteArray())
assertEquals(expectedResult, result)
}
}
testPairs(
TestData.unknownKeysTestPairs,
TestData.SampleClass.serializer()
)
}

@Test
fun testOverflows() {
fun <T> testPairs(dataList: List<String>, serializer: KSerializer<T>) {
Expand Down Expand Up @@ -337,10 +353,6 @@ internal class MsgPackTest {
fun testNestedStructures() {
val sm1: NestedMessage = listOf("Alice" to "Bob", "Charley" to "Delta") to "Random message Body here"
val result = MsgPack.encodeToByteArray(sm1)
println(result.toHex())
println(result.toList())
println(result.size)
println(MsgPack.decodeFromByteArray<Pair<Int, Int>>(MsgPack.encodeToByteArray(1 to 2)))
val result2 = MsgPack.decodeFromByteArray<NestedMessage>(result)
assertEquals(sm1, result2)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ object TestData {
val sampleClassTestPairs = arrayOf(
"83aa74657374537472696e67a3646566a774657374496e747bab74657374426f6f6c65616ec3" to SampleClass("def", 123, true)
)
val unknownKeysTestPairs = arrayOf(
"85aa74657374537472696e67a3646566ab7465737449676e6f72656491a969676e6f7265206d65a774657374496e747bab74657374556e6b6e6f776ea7756e6b6e6f776eab74657374426f6f6c65616ec3" to SampleClass("def", 123, true)
)
val pairsTestPairs: Array<Pair<String, Pair<String, String>>> = arrayOf(
"82a56669727374a5416c696365a67365636f6e64a3426f62" to Pair("Alice", "Bob")
)
Expand Down

0 comments on commit 5d704d6

Please sign in to comment.