forked from Kotlin/kotlinx.serialization
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to disallow repeated keys in CBOR
Fixes Kotlin#2662 by adding a `visitKey` method to `CompositeDecoder`; map and set serializers should call this so that decoders have an opportunity to throw an error when a duplicate key is detected. Calls to this method are added to: - `MapLikeSerializer.readElement` to handle maps - `CborReader.decodeElementIndex` to handle data classes A new config option `forbidDuplicateKeys` can be set to true to enable this new behavior. This can form the basis of a Strict Mode in the future.
- Loading branch information
Showing
12 changed files
with
125 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
formats/cbor/commonTest/src/kotlinx/serialization/cbor/CborStrictModeTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package kotlinx.serialization.cbor | ||
|
||
import kotlinx.serialization.assertFailsWithMessage | ||
import kotlinx.serialization.decodeFromByteArray | ||
import kotlinx.serialization.HexConverter | ||
import kotlinx.serialization.DuplicateKeyException | ||
import kotlinx.serialization.Serializable | ||
import kotlin.test.Test | ||
|
||
class CborStrictModeTest { | ||
private val strict = Cbor { forbidDuplicateKeys = true } | ||
|
||
/** Duplicate keys are rejected in generic maps. */ | ||
@Test | ||
fun testDuplicateKeysInMap() { | ||
val duplicateKeys = HexConverter.parseHexBinary("A2617805617806") | ||
assertFailsWithMessage<DuplicateKeyException>("Duplicate keys not allowed. Key appeared twice: x") { | ||
strict.decodeFromByteArray<Map<String, Long>>(duplicateKeys) | ||
} | ||
} | ||
|
||
@Serializable | ||
data class ExampleClass(val x: Long) | ||
|
||
/** Duplicate keys are rejected in classes. */ | ||
@Test | ||
fun testDuplicateKeysInDataClass() { | ||
// {"x": 5, "x", 6} | ||
val duplicateKeys = HexConverter.parseHexBinary("A2617805617806") | ||
assertFailsWithMessage<DuplicateKeyException>("Duplicate keys not allowed. Key appeared twice: x") { | ||
strict.decodeFromByteArray<ExampleClass>(duplicateKeys) | ||
} | ||
} | ||
|
||
/** Duplicate unknown keys are rejected as well. */ | ||
@Test | ||
fun testDuplicateUnknownKeys() { | ||
// {"a": 1, "a", 2, "x", 6} | ||
val duplicateKeys = HexConverter.parseHexBinary("A3616101616102617806") | ||
val cbor = Cbor(strict) { ignoreUnknownKeys = true } | ||
assertFailsWithMessage<DuplicateKeyException>("Duplicate keys not allowed. Key appeared twice: a") { | ||
cbor.decodeFromByteArray<ExampleClass>(duplicateKeys) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters