We just upgraded our Kotlin project from Spring Boot 2.2.4 to 2.2.5.
Unfortunately now some WebFlux-RestController tests fail because a mandatory JSON payload field now leads to a 500 Internal Server Error instead of a 400 Bad Request.
This is an exemplary RestController with its Kotlin data class for the request payload:
@RestController
class Controller {
@PostMapping("/hello")
fun post(@RequestBody request: RequestPayload) =
ResponseEntity.ok().body("Hello ${request.foo} - ${request.bar}")
}
data class RequestPayload(
val foo: String,
val bar: String?
)
Please notice that attribute foo is mandatory (no ?).
This is the invalid payload (missing JSON field foo):
{ "bar": "bar text" }
When calling this RestController in a Spring Boot 2.2.4 app this results in a DecodingException which is correctly mapped to the status code 400.
But when calling this RestController in a Spring Boot 2.2.5 app this leads to CodecException which is not mapped to the status code 400 but results in a 500.
Please find the example in my GitHub repo: https://github.com/csh0711/boot-2-2-5-kotlin-codec-exception
Or use this ControllerTest:
@WebFluxTest(Controller::class)
internal class ControllerTests(
@Autowired private val webTestClient: WebTestClient
) {
@Test
fun `responds with HTTP 400 BAD_REQUEST for missing foo in payload`() {
webTestClient.post()
.uri("/hello")
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(""" { "bar": "bar text" } """)
.exchange()
.expectStatus().isBadRequest
}
}
Many thanks in advance! :-)
We just upgraded our Kotlin project from Spring Boot 2.2.4 to 2.2.5.
Unfortunately now some WebFlux-RestController tests fail because a mandatory JSON payload field now leads to a 500 Internal Server Error instead of a 400 Bad Request.
This is an exemplary RestController with its Kotlin data class for the request payload:
Please notice that attribute foo is mandatory (no
?).This is the invalid payload (missing JSON field
foo):{ "bar": "bar text" }When calling this RestController in a Spring Boot 2.2.4 app this results in a DecodingException which is correctly mapped to the status code 400.
But when calling this RestController in a Spring Boot 2.2.5 app this leads to CodecException which is not mapped to the status code 400 but results in a 500.
Please find the example in my GitHub repo: https://github.com/csh0711/boot-2-2-5-kotlin-codec-exception
Or use this ControllerTest:
Many thanks in advance! :-)