Closed bcmedeiros closed 2 months ago
In case you need a quick repro. issue-33138.zip
@sdeleuze are you sure catching IllegalArgumentException
is fine? In my local tests I had to catch kotlinx.serialization.json.internal.JsonDecodingException
for things to work as expected.
Yes, JsonDecodingException
inherit from IllegalArgumentException
, we have tests tests checking that now, I checked with your repro and that's what Kotlin Serialization advises to do.
Yes,
JsonDecodingException
inherit fromIllegalArgumentException
, we have tests tests checking that now, I checked with your repro
True, I didn't go up far enough in the class hierarchy, I guess.
and that's what Kotlin Serialization advises to do.
Also true, for reference: https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/-serialization-exception/ That would also catch exceptions thrown in the constructor for validation purposes, which makes a lot of sense.
Consider the following controller:
If I run
curl -X POST --location http://localhost:8080/test -H "Content-Type: application/json" -d '{"message2": "hello"}'
, I get a 500 instead of a 400. If I just comment out the@Serializable
from theBody
data class, then Spring falls back to Jackson and I get a 400 instead.I did some quick debugging, and it seems that
KotlinSerializationJsonDecoder
is at fault here; if you look atorg.springframework.http.codec.json.AbstractJackson2Decoder#processException
, it is catchingcom.fasterxml.jackson.core.JsonProcessingException
and throwingorg.springframework.core.codec.DecodingException
, which is then caught byorg.springframework.web.reactive.result.method.annotation.AbstractMessageReaderArgumentResolver#handleReadError
and converted into aorg.springframework.web.server.ServerWebInputException
which eventually results on a 400.KotlinSerializationJsonDecoder
, on the other hand, never catcheskotlinx.serialization.json.internal.JsonDecodingException
(which is thrown bydecodeFromString
) and converts it toorg.springframework.core.codec.DecodingException
, so it bubbles all the way up as a 500 error: