|
| 1 | +/* |
| 2 | + * Copyright 2017-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
| 3 | + */ |
| 4 | + |
| 5 | + |
| 6 | +package kotlinx.serialization.json |
| 7 | + |
| 8 | +import kotlinx.serialization.* |
| 9 | +import kotlin.test.* |
| 10 | + |
| 11 | + |
| 12 | +class JsonErrorMessagesTest : JsonTestBase() { |
| 13 | + |
| 14 | + @Test |
| 15 | + fun testJsonTokensAreProperlyReported() = parametrizedTest { mode -> |
| 16 | + val input1 = """{"boxed":4}""" |
| 17 | + val input2 = """{"boxed":"str"}""" |
| 18 | + |
| 19 | + val serString = serializer<Box<String>>() |
| 20 | + val serInt = serializer<Box<Int>>() |
| 21 | + |
| 22 | + checkSerializationException({ |
| 23 | + default.decodeFromString(serString, input1, mode) |
| 24 | + }, { message -> |
| 25 | + if (mode == JsonTestingMode.TREE) |
| 26 | + assertContains(message, "String literal for key 'boxed' should be quoted.") |
| 27 | + else |
| 28 | + assertContains( |
| 29 | + message, |
| 30 | + "Unexpected JSON token at offset 9: Expected quotation mark '\"', but had '4' instead at path: \$.boxed" |
| 31 | + ) |
| 32 | + }) |
| 33 | + |
| 34 | + checkSerializationException({ |
| 35 | + default.decodeFromString(serInt, input2, mode) |
| 36 | + }, { message -> |
| 37 | + if (mode != JsonTestingMode.TREE) |
| 38 | + // we allow number values to be quoted, so the message pointing to 's' is correct |
| 39 | + assertContains( |
| 40 | + message, |
| 41 | + "Unexpected JSON token at offset 9: Unexpected symbol 's' in numeric literal at path: \$.boxed" |
| 42 | + ) |
| 43 | + else |
| 44 | + assertContains(message, "Failed to parse literal as 'int' value") |
| 45 | + }) |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + fun testMissingClosingQuote() = parametrizedTest { mode -> |
| 50 | + val input1 = """{"boxed:4}""" |
| 51 | + val input2 = """{"boxed":"str}""" |
| 52 | + val input3 = """{"boxed:"str"}""" |
| 53 | + val serString = serializer<Box<String>>() |
| 54 | + val serInt = serializer<Box<Int>>() |
| 55 | + |
| 56 | + checkSerializationException({ |
| 57 | + default.decodeFromString(serInt, input1, mode) |
| 58 | + }, { message -> |
| 59 | + // For discussion: |
| 60 | + // Technically, both of these messages are correct despite them being completely different. |
| 61 | + // A `:` instead of `"` is a good guess, but `:`/`}` is a perfectly valid token inside Json string — for example, |
| 62 | + // it can be some kind of path `{"foo:bar:baz":"my:resource:locator:{123}"}` or even URI used as a string key/value. |
| 63 | + // So if the closing quote is missing, there's really no way to correctly tell where the key or value is supposed to end. |
| 64 | + // Although we may try to unify these messages for consistency. |
| 65 | + if (mode in setOf(JsonTestingMode.STREAMING, JsonTestingMode.TREE)) |
| 66 | + assertContains( |
| 67 | + message, |
| 68 | + "Unexpected JSON token at offset 7: Expected quotation mark '\"', but had ':' instead at path: \$" |
| 69 | + ) |
| 70 | + else |
| 71 | + assertContains( |
| 72 | + message, "Unexpected EOF at path: \$" |
| 73 | + ) |
| 74 | + }) |
| 75 | + |
| 76 | + checkSerializationException({ |
| 77 | + default.decodeFromString(serString, input2, mode) |
| 78 | + }, { message -> |
| 79 | + if (mode in setOf(JsonTestingMode.STREAMING, JsonTestingMode.TREE)) |
| 80 | + assertContains( |
| 81 | + message, |
| 82 | + "Unexpected JSON token at offset 13: Expected quotation mark '\"', but had '}' instead at path: \$" |
| 83 | + ) |
| 84 | + else |
| 85 | + assertContains(message, "Unexpected EOF at path: \$.boxed") |
| 86 | + }) |
| 87 | + |
| 88 | + checkSerializationException({ |
| 89 | + default.decodeFromString(serString, input3, mode) |
| 90 | + }, { message -> |
| 91 | + assertContains( |
| 92 | + message, |
| 93 | + "Unexpected JSON token at offset 9: Expected colon ':', but had 's' instead at path: \$" |
| 94 | + ) |
| 95 | + }) |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + fun testUnquoted() = parametrizedTest { mode -> |
| 100 | + val input1 = """{boxed:str}""" |
| 101 | + val input2 = """{"boxed":str}""" |
| 102 | + val ser = serializer<Box<String>>() |
| 103 | + |
| 104 | + checkSerializationException({ |
| 105 | + default.decodeFromString(ser, input1, mode) |
| 106 | + }, { message -> |
| 107 | + assertContains( |
| 108 | + message, |
| 109 | + """Unexpected JSON token at offset 1: Expected quotation mark '"', but had 'b' instead at path: ${'$'}""" |
| 110 | + ) |
| 111 | + }) |
| 112 | + |
| 113 | + checkSerializationException({ |
| 114 | + default.decodeFromString(ser, input2, mode) |
| 115 | + }, { message -> |
| 116 | + if (mode == JsonTestingMode.TREE) assertContains( |
| 117 | + message, |
| 118 | + """String literal for key 'boxed' should be quoted.""" |
| 119 | + ) |
| 120 | + else assertContains( |
| 121 | + message, |
| 122 | + """Unexpected JSON token at offset 9: Expected quotation mark '"', but had 's' instead at path: ${'$'}.boxed""" |
| 123 | + ) |
| 124 | + }) |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + fun testNullLiteralForNotNull() = parametrizedTest { mode -> |
| 129 | + val input = """{"boxed":null}""" |
| 130 | + val ser = serializer<Box<String>>() |
| 131 | + checkSerializationException({ |
| 132 | + default.decodeFromString(ser, input, mode) |
| 133 | + }, { message -> |
| 134 | + if (mode == JsonTestingMode.TREE) |
| 135 | + assertContains(message, "Unexpected 'null' literal when non-nullable string was expected") |
| 136 | + else |
| 137 | + assertContains( |
| 138 | + message, |
| 139 | + "Unexpected JSON token at offset 9: Expected string literal but 'null' literal was found at path: \$.boxed" |
| 140 | + ) |
| 141 | + }) |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + fun testEof() = parametrizedTest { mode -> |
| 146 | + val input = """{"boxed":""" |
| 147 | + checkSerializationException({ |
| 148 | + default.decodeFromString<Box<String>>(input, mode) |
| 149 | + }, { message -> |
| 150 | + if (mode == JsonTestingMode.TREE) |
| 151 | + assertContains(message, "Cannot read Json element because of unexpected end of the input at path: $") |
| 152 | + else |
| 153 | + assertContains(message, "Expected quotation mark '\"', but had 'EOF' instead at path: \$.boxed") |
| 154 | + |
| 155 | + }) |
| 156 | + |
| 157 | + } |
| 158 | + |
| 159 | + private fun checkSerializationException(action: () -> Unit, assertions: SerializationException.(String) -> Unit) { |
| 160 | + val e = assertFailsWith(SerializationException::class, action) |
| 161 | + assertNotNull(e.message) |
| 162 | + e.assertions(e.message!!) |
| 163 | + } |
| 164 | + |
| 165 | +} |
0 commit comments