Skip to content

Commit

Permalink
Properly handle quoted 'null' literals in lenient mode (#1637)
Browse files Browse the repository at this point in the history
Fixes #1600
  • Loading branch information
qwwdfsad authored Aug 17, 2021
1 parent 4755b24 commit d00bff2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -368,13 +368,21 @@ internal class JsonLexer(private val source: String) {

fun consumeStringLenientNotNull(): String {
val result = consumeStringLenient()
if (result == NULL) { // Check if lenient value is 'null' and fail for non-nullable read if so
/*
* Check if lenient value is 'null' _without_ quotation marks and fail for non-nullable read if so.
*/
if (result == NULL && wasUnquotedString()) {
fail("Unexpected 'null' value instead of string literal")
}
return result
}

// Allows to consume unquoted string
private fun wasUnquotedString(): Boolean {
// Is invoked _only_ when the 'null' string was read, thus 'cP - 1' is always within bounds
return source[currentPosition - 1] != STRING
}

// Allows consuming unquoted string
fun consumeStringLenient(): String {
if (peekedString != null) {
return takePeeked()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@

package kotlinx.serialization.json

import kotlinx.serialization.Serializable
import kotlinx.serialization.json.internal.JsonDecodingException
import kotlinx.serialization.json.internal.JsonException
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlinx.serialization.*
import kotlinx.serialization.builtins.*
import kotlinx.serialization.json.internal.*
import kotlin.test.*

class LenientTest : JsonTestBase() {

Expand Down Expand Up @@ -81,5 +79,17 @@ class LenientTest : JsonTestBase() {
assertEquals(NullableString("nul"), lenient.decodeFromString("""{"s":nul}""", it))
assertEquals(NullableString("null1"), lenient.decodeFromString("""{"s":null1}""", it))
assertEquals(NullableString(null), lenient.decodeFromString("""{"s":null}""", it))
assertEquals(NullableString("null"), lenient.decodeFromString("""{"s":"null"}""", it))
assertEquals(NullableString("null"), lenient.decodeFromString("""{"s":"null" }""", it))
assertEquals(NullableString("null "), lenient.decodeFromString("""{"s":"null " }""", it))
}

@Test
fun testTopLevelNulls() = parametrizedTest {
assertEquals("nul", lenient.decodeFromString("""nul""", it))
assertEquals("null1", lenient.decodeFromString("""null1""", it))
assertEquals(null, lenient.decodeFromString(String.serializer().nullable, """null""", it))
assertEquals("null", lenient.decodeFromString(""""null"""", it))
assertEquals("null ", lenient.decodeFromString(""""null """", it))
}
}

0 comments on commit d00bff2

Please sign in to comment.