-
Notifications
You must be signed in to change notification settings - Fork 623
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix serializing nulls for a property of a parameterized type with a nullable upper bound with Protobuf #2561
Merged
sandwwraith
merged 4 commits into
Kotlin:dev
from
huanshankeji:protobuf-type-parameter-nullable-upper-bound
Mar 20, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
16fc6de
Support serializing nulls for a property of a parameterized type with…
ShreckYe 7f046a0
Fix the bug in the implementation that cause the tests to fail in com…
ShreckYe a34a8f5
Slightly improve `encodeSerializableElement`
ShreckYe 779a81c
Remove some unnecessary braces
ShreckYe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
102 changes: 102 additions & 0 deletions
102
formats/protobuf/commonTest/src/kotlinx/serialization/protobuf/ProtobufTypeParameterTest.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,102 @@ | ||
/* | ||
* Copyright 2017-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.serialization.protobuf | ||
|
||
import kotlinx.serialization.* | ||
import kotlin.test.* | ||
|
||
class ProtobufTypeParameterTest { | ||
@Serializable | ||
data class Box<T>(val value: T) | ||
|
||
@Serializable | ||
data class ExplicitNullableUpperBoundBox<T : Any?>(val value: T) | ||
|
||
@Serializable | ||
data class ExplicitNullableUpperNullablePropertyBoundBox<T : Any?>(val value: T?) | ||
|
||
inline fun <reified T> testBox(value: T, expectedHexString: String) { | ||
testConversion(Box(value), expectedHexString) | ||
testConversion(ExplicitNullableUpperBoundBox(value), expectedHexString) | ||
testConversion(ExplicitNullableUpperNullablePropertyBoundBox(value), expectedHexString) | ||
} | ||
|
||
@Serializable | ||
private data class DefaultArgPair<T>(val first: T, val second: T = first) | ||
|
||
companion object { | ||
val testList0 = emptyList<Int>() | ||
val testList1 = listOf(0) | ||
val testMap0 = emptyMap<Int, Int>() | ||
val testMap1 = mapOf(0 to 0) | ||
} | ||
|
||
|
||
@Test | ||
fun testNothingBoxesWithNull() { | ||
// Cannot use 'Nothing?' as reified type parameter | ||
//testBox(null, "") | ||
testConversion(Box(null), "") | ||
testConversion(ExplicitNullableUpperBoundBox(null), "") | ||
@Suppress("RemoveExplicitTypeArguments") | ||
testConversion(ExplicitNullableUpperNullablePropertyBoundBox<Nothing>(null), "") | ||
testConversion(ExplicitNullableUpperNullablePropertyBoundBox<Nothing?>(null), "") | ||
} | ||
|
||
@Test | ||
fun testIntBoxes() { | ||
testBox(0, "0800") | ||
testBox(1, "0801") | ||
} | ||
|
||
@Test | ||
fun testNullableIntBoxes() { | ||
testBox<Int?>(null, "") | ||
testBox<Int?>(0, "0800") | ||
} | ||
|
||
@Test | ||
fun testCollectionBoxes() { | ||
testBox(testList0, "") | ||
testBox(testList1, "0800") | ||
testBox(testMap0, "") | ||
testBox(testMap1, "0A0408001000") | ||
} | ||
|
||
@Test | ||
fun testNullableCollectionBoxes() { | ||
fun assertFailsForNullForCollectionTypes(block: () -> Unit) { | ||
try { | ||
block() | ||
fail() | ||
} catch (e: SerializationException) { | ||
assertEquals( | ||
"'null' is not supported for collection types in ProtoBuf", e.message | ||
) | ||
} | ||
} | ||
assertFailsForNullForCollectionTypes { | ||
testBox<List<Int>?>(null, "") | ||
} | ||
assertFailsForNullForCollectionTypes { | ||
testBox<Map<Int, Int>?>(null, "") | ||
} | ||
testBox<List<Int>?>(testList0, "") | ||
testBox<Map<Int, Int>?>(testMap0, "") | ||
} | ||
|
||
@Test | ||
fun testWithDefaultArguments() { | ||
testConversion(DefaultArgPair(null), "") | ||
testConversion(DefaultArgPair(1), "0801") | ||
testConversion(DefaultArgPair(null, null), "") | ||
testConversion(DefaultArgPair(null, 1), "1001") | ||
assertFailsWith<SerializationException> { | ||
testConversion(DefaultArgPair(1, null), "0801") | ||
} | ||
testConversion(DefaultArgPair(1, 1), "0801") | ||
testConversion(DefaultArgPair(1, 2), "08011002") | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
formats/protobuf/commonTest/src/kotlinx/serialization/protobuf/TestFunctions.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,20 @@ | ||
/* | ||
* Copyright 2017-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.serialization.protobuf | ||
|
||
import kotlinx.serialization.* | ||
import kotlin.test.* | ||
|
||
fun <T> testConversion(data: T, serializer: KSerializer<T>, expectedHexString: String) { | ||
val string = ProtoBuf.encodeToHexString(serializer, data).uppercase() | ||
assertEquals(expectedHexString, string) | ||
assertEquals(data, ProtoBuf.decodeFromHexString(serializer, string)) | ||
} | ||
|
||
inline fun <reified T> testConversion(data: T, expectedHexString: String) { | ||
val string = ProtoBuf.encodeToHexString(data).uppercase() | ||
assertEquals(expectedHexString, string) | ||
assertEquals(data, ProtoBuf.decodeFromHexString(string)) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mind also adding test for
class Foo<T: Any>(val t: T)
to check that non-nullable type parameters still rejectnull
s?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry I am not sure how to do this. I could add some tests for
class Foo<T: Any>(val t: T)
but it's impossible to set anull
for the membert
, unless I go for some hacks like calling the constructor in Java and passing anull
argument (I am not completely sure this works though).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, you're right, that's impossible for Kotlin's type system. Never mind :)