Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -82,37 +82,30 @@ open class ImplicitNullsBenchmark {
private val serializer = Values.serializer()

@Benchmark
fun decodeNoNulls() {
fun decodeNoNulls() =
Json.decodeFromString(serializer, jsonNoNulls)
}

@Benchmark
fun decodeNoNullsImplicit() {
fun decodeNoNullsImplicit() =
jsonImplicitNulls.decodeFromString(serializer, jsonNoNulls)
}

@Benchmark
fun decodeNulls() {
fun decodeNulls() =
Json.decodeFromString(serializer, jsonWithNulls)
}

@Benchmark
fun decodeNullsImplicit() {
fun decodeNullsImplicit() =
jsonImplicitNulls.decodeFromString(serializer, jsonWithNulls)
}

@Benchmark
fun decodeAbsenceImplicit() {
fun decodeAbsenceImplicit() =
jsonImplicitNulls.decodeFromString(serializer, jsonWithAbsence)
}

@Benchmark
fun encodeNulls() {
fun encodeNulls() =
Json.encodeToString(serializer, valueWithNulls)
}

@Benchmark
fun encodeNullsImplicit() {
fun encodeNullsImplicit() =
jsonImplicitNulls.encodeToString(serializer, valueWithNulls)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ val globalCompilerArgs
"-P", "plugin:org.jetbrains.kotlinx.serialization:disableIntrinsic=false",
"-Xreport-all-warnings",
"-Xrender-internal-diagnostic-names",
"-Xreturn-value-checker=full",
)

val kotlin_Werror_override: String? by project
Expand Down
4 changes: 3 additions & 1 deletion buildSrc/src/main/kotlin/source-sets-conventions.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ kotlin {
freeCompilerArgs.add("-Xsuppress-version-warnings")
}
freeCompilerArgs.add("-Xexpect-actual-classes")
// for some reason, IDE does not enable feature in test source sets without this line:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please file an issue to investigate the root cause further?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to minimize the example, will post once it's ready

freeCompilerArgs.add("-Xreturn-value-checker=full")
}

sourceSets {
Expand All @@ -87,7 +89,7 @@ kotlin {

commonTest {
dependencies {
api(versionCatalog().findLibrary("kotlin.test").get())
implementation(versionCatalog().findLibrary("kotlin.test").get())
}
}

Expand Down
2 changes: 2 additions & 0 deletions core/commonMain/src/kotlinx/serialization/internal/Tagged.kt
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ public abstract class TaggedEncoder<Tag : Any?> : Encoder, CompositeEncoder {
tagStack.add(name)
}

@IgnorableReturnValue
protected fun popTag(): Tag =
if (tagStack.isNotEmpty())
tagStack.removeAt(tagStack.lastIndex)
Expand Down Expand Up @@ -316,6 +317,7 @@ public abstract class TaggedDecoder<Tag : Any?> : Decoder, CompositeDecoder {

private var flag = false

@IgnorableReturnValue
protected fun popTag(): Tag {
val r = tagStack.removeAt(tagStack.lastIndex)
flag = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import kotlinx.serialization.internal.EnumSerializer
import kotlinx.serialization.test.*
import kotlin.test.*

// This is unimplemented functionality that should be
@Suppress("RemoveExplicitTypeArguments") // This is exactly what's being tested
@Suppress("RETURN_VALUE_NOT_USED") // assertIs, KT-82363
class SerializersLookupEnumTest {
@Serializable(with = EnumExternalObjectSerializer::class)
enum class EnumExternalObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ class SealedInterfacesSerializationTest {
assertEquals(listOf("ImplA", "ImplB"), subclasses)
}

private fun SerialDescriptor.isDummy() = serialName == "Dummy"
private fun SerialDescriptor.isDummy() = assertEquals(serialName, "Dummy")

private fun SerialDescriptor.isPolymorphic() = kind == PolymorphicKind.OPEN
private fun SerialDescriptor.isPolymorphic() = assertEquals(kind, PolymorphicKind.OPEN)

operator fun SerialDescriptor.get(i: Int) = getElementDescriptor(i)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import kotlinx.serialization.*
class ObjectSerializerTest {
@Test
fun testSequentialDecoding() {
SimpleObject.serializer().deserialize(DummySequentialDecoder())
assertSame(SimpleObject, SimpleObject.serializer().deserialize(DummySequentialDecoder()))
}

@Serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TuplesTest {
fun testSequentialDecodingKeyValue() {
val decoder = DummySequentialDecoder()
val serializer = MapEntrySerializer(Unit.serializer(), Unit.serializer())
serializer.deserialize(decoder)
assertNotNull(serializer.deserialize(decoder))
assertEquals(decoder.beginStructureCalled, decoder.endStructureCalled)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ inline fun jvmOnly(test: () -> Unit) {
if (isJvm()) test()
}

inline fun <reified T : Throwable> assertFailsWithMessage(message: String, block: () -> Unit) {
inline fun <reified T : Throwable> assertFailsWithMessage(message: String, block: () -> Any?) {
val exception = assertFailsWith(T::class, null, block)
assertTrue(exception.message!!.contains(message), "Expected message '${exception.message}' to contain substring '$message'")
}
2 changes: 1 addition & 1 deletion core/jvmMain/src/kotlinx/serialization/internal/Caching.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import kotlin.reflect.KTypeProjection
* and fallback to ConcurrentHashMap-based cache.
*/
private val useClassValue = try {
Class.forName("java.lang.ClassValue")
val _ = Class.forName("java.lang.ClassValue")
true
} catch (_: Throwable) {
false
Expand Down
4 changes: 2 additions & 2 deletions core/jvmTest/src/kotlinx/serialization/CachingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CachingTest {
}

repeat(10) {
cache.get(typeOf<String>().kclass())
val _ = cache.get(typeOf<String>().kclass())
}

assertEquals(1, factoryCalled)
Expand All @@ -38,7 +38,7 @@ class CachingTest {
}

repeat(10) {
cache.get(typeOf<Map<*, *>>().kclass(), listOf(typeOf<String>(), typeOf<String>()))
val _ = cache.get(typeOf<Map<*, *>>().kclass(), listOf(typeOf<String>(), typeOf<String>()))
}

assertEquals(1, factoryCalled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class SerializationMethodInvocationOrderTest {
out.done()

val inp = Inp()
inp.decodeSerializableValue(serializer<Container>())
assertEquals(Container(Data("s1", 42)), inp.decodeSerializableValue(serializer<Container>()))
inp.done()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ internal class CborParser(private val input: ByteArrayInput, private val verifyO
readByte()
}

@IgnorableReturnValue
private fun readByte(): Int {
curByte = input.read()
return curByte
Expand Down Expand Up @@ -268,6 +269,7 @@ internal class CborParser(private val input: ByteArrayInput, private val verifyO
input.readExactNBytes(strLen)
}

@IgnorableReturnValue
private fun processTags(tags: ULongArray?): ULongArray? {
var index = 0
val collectedTags = mutableListOf<ULong>()
Expand Down Expand Up @@ -364,7 +366,7 @@ internal class CborParser(private val input: ByteArrayInput, private val verifyO
error("Unexpected EOF, available $availableBytes bytes, requested: $bytesCount")
}
val array = ByteArray(bytesCount)
read(array, 0, bytesCount)
val _ = read(array, 0, bytesCount)
return array
}

Expand Down Expand Up @@ -445,7 +447,7 @@ internal class CborParser(private val input: ByteArrayInput, private val verifyO
val header = curByte and 0b111_00000
val length = elementLength()
if (header == HEADER_TAG) {
readNumber()
val _ = readNumber()
} else if (header == HEADER_ARRAY || header == HEADER_MAP) {
if (length > 0) lengthStack.add(length)
else prune(lengthStack) // empty map or array automatically completes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ internal class DefiniteLengthCborWriter(cbor: Cbor, output: ByteArrayOutput) : C

override fun beginStructure(descriptor: SerialDescriptor): CompositeEncoder {
val current = Data(ByteArrayOutput(), 0)
structureStack.push(current)
val _ = structureStack.push(current)
return this
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal inline fun <reified T : Any> assertSerializedToBinaryAndRestored(
assertEquals(original, restored)
}

inline fun <reified T : Throwable> assertFailsWithMessage(message: String, block: () -> Unit) {
inline fun <reified T : Throwable> assertFailsWithMessage(message: String, block: () -> Any?) {
val exception = assertFailsWith(T::class, null, block)
assertTrue(exception.message!!.contains(message), "Expected message '${exception.message}' to contain substring '$message'")
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class CborDecoderTest {

@Test
fun testNullables() {
Cbor.decodeFromHexString<NullableByteStringDefaultNull>("a0")
assertEquals(NullableByteStringDefaultNull(), Cbor.decodeFromHexString<NullableByteStringDefaultNull>("a0"))
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ data class NullableByteStringDefaultNull(
if (this === other) return true
if (other == null || this::class != other::class) return false

other as NullableByteString
other as NullableByteStringDefaultNull

if (byteString != null) {
if (other.byteString == null) return false
Expand Down Expand Up @@ -150,4 +150,4 @@ value class ValueClassWithUnlabeledByteString(@ByteString val x: Inner) {
@JvmInline
@Serializable
value class Inner(val x: ByteArray)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class CborStacktraceRecoveryTest {
Cbor.decodeFromByteArray<String>(byteArrayOf(0xFF.toByte()))
}

private inline fun <reified E : Exception> checkRecovered(noinline block: () -> Unit) = runBlocking {
private inline fun <reified E : Exception> checkRecovered(noinline block: () -> Any?) = runBlocking {
val result = runCatching {
callBlockWithRecovery(block)
}
Expand All @@ -29,7 +29,7 @@ class CborStacktraceRecoveryTest {
}

// KLUDGE: A separate function with state-machine to ensure coroutine DebugMetadata is generated. See KT-41789
private suspend fun callBlockWithRecovery(block: () -> Unit) {
private suspend fun callBlockWithRecovery(block: () -> Any?) {
yield()
// use withContext to perform switch between coroutines and thus trigger exception recovery machinery
withContext(NonCancellable) {
Expand Down
6 changes: 3 additions & 3 deletions formats/json-tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ kotlin {
}
val commonTest by getting {
dependencies {
api(project(":kotlinx-serialization-json"))
api(project(":kotlinx-serialization-json-okio"))
api(project(":kotlinx-serialization-json-io"))
implementation(project(":kotlinx-serialization-json"))
implementation(project(":kotlinx-serialization-json-okio"))
implementation(project(":kotlinx-serialization-json-io"))
implementation(libs.kotlinx.io)
implementation(libs.okio)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ class JsonPathTest : JsonTestBase() {

@Serializable
@SerialName("n")
class Nesting(val f: Sealed) : Sealed()
data class Nesting(val f: Sealed) : Sealed()

@Serializable
@SerialName("b")
class Box(val s: String) : Sealed()
data class Box(val s: String) : Sealed()

@Serializable
@SerialName("d")
class DoubleNesting(val f: Sealed, val f2: Sealed) : Sealed()
data class DoubleNesting(val f: Sealed, val f2: Sealed) : Sealed()
}

// TODO use non-array polymorphism when https://github.com/Kotlin/kotlinx.serialization/issues/1839 is fixed
Expand All @@ -126,8 +126,8 @@ class JsonPathTest : JsonTestBase() {
outer = Sealed.Nesting(outer)
}
val str = json.encodeToString(Sealed.serializer(), outer)
// throw-away data
json.decodeFromString(Sealed.serializer(), str)
// check that data is correctly formed
assertEquals(outer, json.decodeFromString(Sealed.serializer(), str))

val malformed = str.replace("\"value\"", "42")
val expectedPath = "$" + ".value.f".repeat(101) + ".value.s"
Expand All @@ -147,9 +147,10 @@ class JsonPathTest : JsonTestBase() {
outer2 = Sealed.Nesting(outer2)
}

val str = json.encodeToString(Sealed.serializer(), Sealed.DoubleNesting(outer1, outer2))
// throw-away data
json.decodeFromString(Sealed.serializer(), str)
val value = Sealed.DoubleNesting(outer1, outer2)
val str = json.encodeToString(Sealed.serializer(), value)
// check that data is correctly formed
assertEquals(value, json.decodeFromString(Sealed.serializer(), str))

val malformed = str.replace("\"incorrect\"", "42")
val expectedPath = "$.value.f2" + ".value.f".repeat(34) + ".value.s"
Expand All @@ -169,15 +170,15 @@ class JsonPathTest : JsonTestBase() {
outer = SimpleNested(n = outer)
}
val str = Json.encodeToString(SimpleNested.serializer(), outer)
// throw-away data
Json.decodeFromString(SimpleNested.serializer(), str)
// check that data is correctly formed
assertEquals(outer, Json.decodeFromString(SimpleNested.serializer(), str))

val malformed = str.replace("{}", "42")
val expectedPath = "$" + ".n".repeat(20) + ".t\n"
expectPath(expectedPath) { Json.decodeFromString(SimpleNested.serializer(), malformed) }
}

private inline fun expectPath(path: String, block: () -> Unit) {
private inline fun expectPath(path: String, block: () -> Any?) {
val message = runCatching { block() }
.exceptionOrNull()!!.message!!
assertContains(message, path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class KeepGeneratedSerializerTest {
override val descriptor = PrimitiveSerialDescriptor("MyEnumSerializer", PrimitiveKind.INT)

override fun deserialize(decoder: Decoder): MyEnum {
decoder.decodeString()
val _ = decoder.decodeString()
return MyEnum.A
}

Expand Down Expand Up @@ -203,7 +203,7 @@ class KeepGeneratedSerializerTest {
override val descriptor = PrimitiveSerialDescriptor("ObjectSerializer", PrimitiveKind.INT)

override fun deserialize(decoder: Decoder): Object {
decoder.decodeInt()
val _ = decoder.decodeInt()
return Object
}
override fun serialize(encoder: Encoder, value: Object) {
Expand Down Expand Up @@ -244,4 +244,4 @@ class KeepGeneratedSerializerTest {
assertEquals(value, keepDecoded, "Json.decodeFromString(${T::class.simpleName}.generatedSerializer(), json)")
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package kotlinx.serialization.features

import kotlinx.serialization.*
import kotlinx.serialization.json.*
import kotlinx.serialization.test.checkSerializationException
import kotlin.test.*

class PolymorphicDeserializationErrorMessagesTest : JsonTestBase() {
Expand All @@ -15,13 +16,6 @@ class PolymorphicDeserializationErrorMessagesTest : JsonTestBase() {
@Serializable
class Holder(val d: DummyData)

// TODO: remove this after #2480 is merged
private fun checkSerializationException(action: () -> Unit, assertions: SerializationException.(String) -> Unit) {
val e = assertFailsWith(SerializationException::class, action)
assertNotNull(e.message)
e.assertions(e.message!!)
}

@Test
fun testNotRegisteredMessage() = parametrizedTest { mode ->
val input = """{"d":{"a":{"type":"my.Class", "value":42}}}"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class JsonConfigurationTest {
assertFailsWith<IllegalArgumentException> { json(true, "\tf\n") }
}

@IgnorableReturnValue
private fun json(prettyPrint: Boolean, prettyPrintIndent: String) = Json {
this.prettyPrint = prettyPrint
this.prettyPrintIndent = prettyPrintIndent
Expand Down
Loading