Skip to content
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

Do not try to end structure in encode/decode stucture extensions if a… #1201

Merged
merged 1 commit into from
Nov 24, 2020
Merged
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 @@ -5,9 +5,9 @@
package kotlinx.serialization.encoding

import kotlinx.serialization.*
import kotlinx.serialization.builtins.*
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.modules.*
import kotlinx.serialization.builtins.*

/**
* Decoder is a core deserialization primitive that encapsulates the knowledge of the underlying
Expand Down Expand Up @@ -524,10 +524,15 @@ public inline fun <T> Decoder.decodeStructure(
block: CompositeDecoder.() -> T
): T {
val composite = beginStructure(descriptor)
var ex: Throwable? = null
sandwwraith marked this conversation as resolved.
Show resolved Hide resolved
try {
return composite.block()
} catch (e: Throwable) {
ex = e
throw e
} finally {
composite.endStructure(descriptor)
// End structure only if there is no exception, otherwise it can be swallowed
if (ex == null) composite.endStructure(descriptor)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,9 +413,14 @@ public interface CompositeEncoder {
*/
public inline fun Encoder.encodeStructure(descriptor: SerialDescriptor, block: CompositeEncoder.() -> Unit) {
val composite = beginStructure(descriptor)
var ex: Throwable? = null
try {
composite.block()
} catch (e: Throwable) {
ex = e
throw e
} finally {
composite.endStructure(descriptor)
// End structure only if there is no exception, otherwise it can be swallowed
if (ex == null) composite.endStructure(descriptor)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package kotlinx.serialization

import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.*
import kotlinx.serialization.json.*
import kotlin.test.*

class EncodingExtensionsTest {

@Serializable(with = BoxSerializer::class)
class Box(val i: Int)

@Serializer(forClass = Box::class)
object BoxSerializer : KSerializer<Box> {
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("Box") {
element<Int>("i")
}

override fun serialize(encoder: Encoder, value: Box) {
encoder.encodeStructure(descriptor) {
throw ArithmeticException()
}
}

override fun deserialize(decoder: Decoder): Box {
decoder.decodeStructure(descriptor) {
throw ArithmeticException()
}
}
}

@Test
fun testEncodingExceptionNotSwallowed() {
assertFailsWith<ArithmeticException> { Json.encodeToString(Box(1)) }
}

@Test
fun testDecodingExceptionNotSwallowed() {
assertFailsWith<ArithmeticException> { Json.decodeFromString<Box>("""{"i":1}""") }
}
}