Skip to content

WebTestClient.ResponseSpec.expectBodyList not working with kotlinx-serialization module #36187

@marks0mmers

Description

@marks0mmers

Attached is a minimal sample application that reproduces this issue. The issue can be seen when running ./gradlew test

Brief Description:
When using kotlinx-serialization, I have a basic controller that returns a simple data class like the following:

package com.example.kotlin_serialization_bug

import kotlinx.serialization.Serializable
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController

@Serializable data class Response(val message: String)

@RestController
class ExampleController {
    @GetMapping("test")
    suspend fun test(): List<Response> =
        listOf(
            Response("Hello"),
            Response("World"),
        )
}

This is then tested using a basic WebTestClient setup like the following:

package com.example.kotlin_serialization_bug

import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.webflux.test.autoconfigure.WebFluxTest
import org.springframework.test.web.reactive.server.WebTestClient

@WebFluxTest(ExampleController::class)
class ExampleControllerTest(@param:Autowired private val webTestClient: WebTestClient) {
    @Test
    suspend fun testEndpoint() {
        webTestClient
            .get()
            .uri("/test")
            .exchangeSuccessfully()
            .expectBodyList(Response::class.java)
            .hasSize(2)
    }
}

Expected: Tests should all pass

Result: This test fails with the following exception:

org.springframework.core.codec.DecodingException: Decoding error: Unexpected JSON token at offset 0: Expected start of the object '{', but had '[' instead at path: $
JSON input: [{"message":"Hello"},{"message":"World"}]

Reproduction Project: kotlin-serialization-bug.zip

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)theme: kotlinAn issue related to Kotlin supporttype: bugA general bug

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions