Skip to content

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

Closed as duplicate

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

Activity

  1. self-assigned this
    on Jan 21, 2026
  2. added this to the 7.0.4 milestone on Feb 5, 2026
  3. added
    in: webIssues in web modules (web, webmvc, webflux, websocket)
    and removed on Feb 5, 2026
  4. sdeleuze commented on Feb 5, 2026

    @sdeleuze
    Contributor

    I was indeed able to reproduce with Kotlinx Serialization but not with Jackson.

  5. modified the milestones: 7.0.4, 7.0.5, 7.0.x on Feb 11, 2026
  6. dmitrysulman commented on Apr 4, 2026

    @dmitrysulman
    Contributor

    @sdeleuze I opened a PR for this: #36597

  7. sdeleuze commented on Apr 4, 2026

    @sdeleuze
    Contributor

    @dmitrysulman Thanks, closing this issue in favor of the PR.

  8. removed this from the 7.0.x milestone on Apr 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)status: supersededAn issue that has been superseded by anothertheme: kotlinAn issue related to Kotlin supporttype: bugA general bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions