diff options
author | Leonid Startsev <sandwwraith@users.noreply.github.com> | 2023-12-18 15:09:29 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-18 15:09:29 +0100 |
commit | ad9ddd10e9fea9660fe7b024cd6e6399d4922db5 (patch) | |
tree | 87758fbce998b7e63798a9b2b5fbfd6f4b5e7d8b /formats/json-tests | |
parent | afebbcbb6a66085915a62b6e61981db8f5922876 (diff) | |
download | kotlinx.serialization-ad9ddd10e9fea9660fe7b024cd6e6399d4922db5.tar.gz |
Do not try to coerce input values for properties (#2530)
Do not try to coerce input values for properties that do not have default values.
Trying so leads to confusing errors about missing values despite a json key actually
present in the input.
Fixes #2529
Diffstat (limited to 'formats/json-tests')
-rw-r--r-- | formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonCoerceInputValuesTest.kt | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonCoerceInputValuesTest.kt b/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonCoerceInputValuesTest.kt index ecb946cb..3d7c3322 100644 --- a/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonCoerceInputValuesTest.kt +++ b/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonCoerceInputValuesTest.kt @@ -29,6 +29,16 @@ class JsonCoerceInputValuesTest : JsonTestBase() { val enum: SampleEnum? ) + @Serializable + class Uncoercable( + val s: String + ) + + @Serializable + class UncoercableEnum( + val e: SampleEnum + ) + val json = Json { coerceInputValues = true isLenient = true @@ -112,4 +122,24 @@ class JsonCoerceInputValuesTest : JsonTestBase() { decoded = decodeFromString<NullableEnumHolder>("""{"enum": OptionA}""") assertEquals(SampleEnum.OptionA, decoded.enum) } + + @Test + fun propertiesWithoutDefaultValuesDoNotChangeErrorMsg() { + val json2 = Json(json) { coerceInputValues = false } + parametrizedTest { mode -> + val e1 = assertFailsWith<SerializationException>() { json.decodeFromString<Uncoercable>("""{"s":null}""", mode) } + val e2 = assertFailsWith<SerializationException>() { json2.decodeFromString<Uncoercable>("""{"s":null}""", mode) } + assertEquals(e2.message, e1.message) + } + } + + @Test + fun propertiesWithoutDefaultValuesDoNotChangeErrorMsgEnum() { + val json2 = Json(json) { coerceInputValues = false } + parametrizedTest { mode -> + val e1 = assertFailsWith<SerializationException> { json.decodeFromString<UncoercableEnum>("""{"e":"UNEXPECTED"}""", mode) } + val e2 = assertFailsWith<SerializationException> { json2.decodeFromString<UncoercableEnum>("""{"e":"UNEXPECTED"}""", mode) } + assertEquals(e2.message, e1.message) + } + } } |