Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AbstractJackson2Encoder encodes empty Flux to invalid JSON #29274

Closed
kzander91 opened this issue Oct 7, 2022 · 0 comments
Closed

AbstractJackson2Encoder encodes empty Flux to invalid JSON #29274

kzander91 opened this issue Oct 7, 2022 · 0 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: regression A bug that is also a regression
Milestone

Comments

@kzander91
Copy link
Contributor

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"));
    }

}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Oct 7, 2022
@bclozel bclozel added in: web Issues in web modules (web, webmvc, webflux, websocket) type: regression A bug that is also a regression and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Oct 7, 2022
@bclozel bclozel added this to the 6.0.0-RC1 milestone Oct 7, 2022
@snicoll snicoll changed the title [WebFlux] AbstractJackson2Encoder Encodes Empty Flux to Invalid JSON AbstractJackson2Encoder encodes empty Flux to invalid JSON Oct 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: regression A bug that is also a regression
Projects
None yet
Development

No branches or pull requests

4 participants