Skip to content

Conversation

Prabal864
Copy link

Context / Problem

In Spring Framework 6 / WebFlux, using WebClient to decode a JSON array of strings
(e.g., ["hello", "world"]) into a List<String> sometimes resulted in unexpected behavior:
the JSON array was decoded as a single String containing the raw JSON ("["hello","world"]")
instead of a proper List<String>.

This issue also occurs for empty arrays: decoding "[]" into a List<String> could produce
a list containing a single string "[]" rather than an empty list.

This behavior is confusing and can lead to subtle bugs when consuming JSON arrays
from reactive endpoints.


Solution

This PR adds self-contained reproducer tests to the spring-webflux module
demonstrating the correct way to decode JSON arrays into List<String> using
ParameterizedTypeReference.

Key points:

  1. Embedded Netty server for tests

    • The tests spin up a lightweight embedded Netty server on a dynamic port.
    • This ensures the tests are self-contained and do not rely on external services.
  2. Correct JSON decoding

    • bodyToMono(new ParameterizedTypeReference<List<String>>() {}) correctly converts
      the JSON array into a List<String>.
  3. Covers both empty and non-empty arrays

    • decodeNotEmptyList() → decodes ["hello", "world"] into a list of two strings.
    • decodeEmptyList() → decodes [] into an empty list.
  4. Assertions

    • Uses AssertJ assertThat() to verify the decoded lists exactly match expected values.

Benefits

  • Ensures WebClient correctly decodes JSON arrays into collections.
  • Provides a reproducible test case for this edge case in WebFlux.
  • Improves reliability for developers consuming JSON arrays in reactive applications.
  • Fully self-contained; can run in CI without external dependencies.

Test Instructions

  1. Run the tests locally:

    ./gradlew :spring-webflux:test --tests "org.springframework.web.reactive.function.client.StringArrayDecodeTests"

@Prabal864 Prabal864 force-pushed the fix-string-array-decode branch from 619b975 to 6f65c29 Compare September 23, 2025 19:53
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Sep 23, 2025
@rstoyanchev
Copy link
Contributor

This is expected behavior documented at https://docs.spring.io/spring-framework/reference/web/webflux/reactive-spring.html#webflux-codecs-jackson (note at the end of section).

@rstoyanchev rstoyanchev closed this Oct 1, 2025
@rstoyanchev rstoyanchev removed the status: waiting-for-triage An issue we've not yet triaged or decided on label Oct 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants