M6 introduced a bug in the AbstractJackson2Encoder
(I think I have narrowed it down to #28401):
Returning an empty Flux
from a WebFlux endpoint yields the following JSON:
]
I created a project that reproduces this here:
demo.zip
Unzip and run ./mvnw test
, a test failure pops up:
Response body: ']'
[ERROR] zero Time elapsed: 0.011 s <<< ERROR!
org.springframework.core.codec.DecodingException: JSON decoding error: Unexpected close marker ']': expected '}' (for root starting at [Source: UNKNOWN; line: 1, column: 0])
at com.example.demo.DemoApplicationTests.zero(DemoApplicationTests.java:19)
Open the pom and switch the Spring Framework version to 6.0.0-M5 and run again: Test is fixed, the logged response body is
[]
as expected.
The project has a controller:
@RestController
class TestController {
@GetMapping("/zero")
Flux<MyType> zero() {
return Flux.empty();
}
@GetMapping("/one")
Flux<MyType> one() {
return Flux.just(new MyType("one"));
}
@GetMapping("/two")
Flux<MyType> two() {
return Flux.just(new MyType("one"), new MyType("two"));
}
}
And a test that calls the controller methods:
@WebFluxTest
class DemoApplicationTests {
@Autowired
private WebTestClient webTestClient;
@Test
void zero() {
webTestClient.get()
.uri("/zero")
.exchange()
.expectBodyList(MyType.class).hasSize(0); // fails on M6 due to the invalid JSON response
}
@Test
void zeroToString() {
webTestClient.get()
.uri("/zero")
.exchange()
.expectBody(String.class)
.consumeWith(r -> System.out.println("Response body: '%s'".formatted(r.getResponseBody())));
}
@Test
void one() {
webTestClient.get()
.uri("/one")
.exchange()
.expectBodyList(MyType.class).hasSize(1)
.contains(new MyType("one"));
}
@Test
void two() {
webTestClient.get()
.uri("/two")
.exchange()
.expectBodyList(MyType.class).hasSize(2)
.contains(new MyType("one"), new MyType("two"));
}
}