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

Fix error triggered by 'consume leading class discriminator' polymorphic parsing optimization #2362

Merged
merged 1 commit into from
Jul 14, 2023
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 @@ -143,4 +143,42 @@ class PolymorphismTest : JsonTestBase() {
val s = json.encodeToString(Wrapper.serializer(), obj, jsonTestingMode)
assertEquals("""{"polyBase1":{"type":"even","parity":"even"},"polyBase2":{"type":"odd","parity":"odd"}}""", s)
}

@Serializable
sealed class Conf {
@Serializable
@SerialName("empty")
object Empty : Conf() // default

@Serializable
@SerialName("simple")
data class Simple(val value: String) : Conf()
}

private val jsonForConf = Json {
isLenient = false
ignoreUnknownKeys = true
serializersModule = SerializersModule {
polymorphicDefaultDeserializer(Conf::class) { Conf.Empty.serializer() }
}
}

@Test
fun defaultSerializerWithEmptyBodyTest() = parametrizedTest { mode ->
assertEquals(Conf.Simple("123"), jsonForConf.decodeFromString<Conf>("""{"type": "simple", "value": "123"}""", mode))
assertEquals(Conf.Empty, jsonForConf.decodeFromString<Conf>("""{"type": "default"}""", mode))
assertEquals(Conf.Empty, jsonForConf.decodeFromString<Conf>("""{"unknown": "Meow"}""", mode))
assertEquals(Conf.Empty, jsonForConf.decodeFromString<Conf>("""{}""", mode))
}

@Test
fun testTypeKeysInLenientMode() = parametrizedTest { mode ->
val json = Json(jsonForConf) { isLenient = true }

assertEquals(Conf.Simple("123"), json.decodeFromString<Conf>("""{type: simple, value: 123}""", mode))
assertEquals(Conf.Empty, json.decodeFromString<Conf>("""{type: default}""", mode))
assertEquals(Conf.Empty, json.decodeFromString<Conf>("""{unknown: Meow}""", mode))
assertEquals(Conf.Empty, json.decodeFromString<Conf>("""{}""", mode))

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ internal open class StreamingJsonDecoder(
}

val discriminator = deserializer.descriptor.classDiscriminator(json)
val type = lexer.consumeLeadingMatchingValue(discriminator, configuration.isLenient)
val type = lexer.peekLeadingMatchingValue(discriminator, configuration.isLenient)
var actualSerializer: DeserializationStrategy<Any>? = null
if (type != null) {
actualSerializer = deserializer.findPolymorphicSerializerOrNull(this, type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ internal abstract class AbstractJsonLexer {
return current
}

abstract fun consumeLeadingMatchingValue(keyToMatch: String, isLenient: Boolean): String?
abstract fun peekLeadingMatchingValue(keyToMatch: String, isLenient: Boolean): String?

fun peekString(isLenient: Boolean): String? {
val token = peekNextToken()
Expand All @@ -301,6 +301,10 @@ internal abstract class AbstractJsonLexer {
return string
}

fun discardPeeked() {
peekedString = null
}

open fun indexOf(char: Char, startPos: Int) = source.indexOf(char, startPos)
open fun substring(startPos: Int, endPos: Int) = source.substring(startPos, endPos)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ internal class ReaderJsonLexer(
}

// Can be carefully implemented but postponed for now
override fun consumeLeadingMatchingValue(keyToMatch: String, isLenient: Boolean): String? = null
override fun peekLeadingMatchingValue(keyToMatch: String, isLenient: Boolean): String? = null

fun release() {
CharArrayPoolBatchSize.release(buffer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,20 @@ internal class StringJsonLexer(override val source: String) : AbstractJsonLexer(
(if (isLenient) consumeStringLenient() else consumeString()).chunked(BATCH_SIZE).forEach(consumeChunk)
}

override fun consumeLeadingMatchingValue(keyToMatch: String, isLenient: Boolean): String? {
override fun peekLeadingMatchingValue(keyToMatch: String, isLenient: Boolean): String? {
val positionSnapshot = currentPosition
try {
// Malformed JSON, bailout
if (consumeNextToken() != TC_BEGIN_OBJ) return null
val firstKey = if (isLenient) consumeKeyString() else consumeStringLenientNotNull()
if (firstKey == keyToMatch) {
if (consumeNextToken() != TC_COLON) return null
val result = if (isLenient) consumeString() else consumeStringLenientNotNull()
return result
}
return null
if (consumeNextToken() != TC_BEGIN_OBJ) return null // Malformed JSON, bailout
val firstKey = peekString(isLenient)
if (firstKey != keyToMatch) return null
discardPeeked() // consume firstKey
if (consumeNextToken() != TC_COLON) return null
return peekString(isLenient)
} finally {
// Restore the position
currentPosition = positionSnapshot
discardPeeked()
}
}
}