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

BigDecimal in multi-value request body deserializes differently than single-value #24479

Closed
xdom opened this issue Feb 4, 2020 · 0 comments
Closed
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: backported An issue that has been backported to maintenance branches type: enhancement A general enhancement
Milestone

Comments

@xdom
Copy link

xdom commented Feb 4, 2020

Affects: spring-webflux:5.2.3.RELEASE


Given a WebFlux RestController has an endpoint mapped, with @RequestBody of type Flux<BigDecimal> and returns the first value of reactive stream as a response,
when calling the endpoint with payload [ -1E+2 ] (array of floats written in E notation),
then response contains -100.0, instead of -1E+2.

(Note: problem lays in deserialization of request body, not serialization of the response)

When request body is mapped as a single-value, i.e. Mono<BigDecimal>, the request body is not normalized and response looks as expected. Non-reactive servlet controller also works consistently, as expected, for both single-value BigDecimal request body and multi-value List<BigDecimal>.

Test to reproduce (single-value testcase passes, multi-values testcase fails):

package com.example.demo;

import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.math.BigDecimal;

class MyTest {
    private final WebTestClient client = WebTestClient.bindToController(new TestController()).build();

    @Test
    void shouldDeserializeSingleBigDecimal() {
        client.post().uri("/single")
              .contentType(MediaType.APPLICATION_JSON)
              .bodyValue("-1E+2")
              .exchange()
              .expectStatus().isOk()
              .expectBody().json("-1E+2");
    }

    @Test
    void shouldDeserializeMultipleBigDecimals() {
        client.post().uri("/multiple")
              .contentType(MediaType.APPLICATION_JSON)
              .bodyValue("[ -1E+2 ]")
              .exchange()
              .expectStatus().isOk()
              .expectBody().json("-1E+2");
    }

    @RestController
    static class TestController {

        @PostMapping("/single")
        Mono<BigDecimal> single(@RequestBody Mono<BigDecimal> entity) {
            return entity;
        }

        @PostMapping("/multiple")
        Mono<BigDecimal> multiple(@RequestBody Flux<BigDecimal> values) {
            return values.single();
        }
    }
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Feb 4, 2020
@poutsma poutsma self-assigned this Feb 6, 2020
@poutsma poutsma added in: web Issues in web modules (web, webmvc, webflux, websocket) for: backport-to-5.1.x Marks an issue as a candidate for backport to 5.1.x type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Feb 6, 2020
@spring-projects-issues spring-projects-issues added status: backported An issue that has been backported to maintenance branches and removed for: backport-to-5.1.x Marks an issue as a candidate for backport to 5.1.x labels Feb 6, 2020
@poutsma poutsma added this to the 5.2.4 milestone Feb 6, 2020
@poutsma poutsma closed this as completed in 45555f7 Feb 7, 2020
poutsma added a commit that referenced this issue Feb 7, 2020
After this commit, Jackson2Tokenizer honours ObjectMapper's
DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS feature when creating
TokenBuffers.

Closes gh-24479
@poutsma poutsma reopened this Feb 7, 2020
@poutsma poutsma closed this as completed in a03a116 Feb 7, 2020
poutsma added a commit that referenced this issue Feb 7, 2020
This commit makes the Jackson2Tokenizer enable
TokenBuffer.forceUseOfBigDecimal if the element type given to the
Decoder is BigDecimal. Previous to this commit, values would be
converted to floats.

Closes gh-24479
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) status: backported An issue that has been backported to maintenance branches type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants