Skip to content

CodecException (from JSON conversion) in RestController is not mapped to 400 but 500 (SB 2.2.5 & Kotlin) #24646

@csh0711

Description

@csh0711

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! :-)

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: regressionA bug that is also a regression

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions