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

Trailing zeros are stripped when deserializing BigDecimal values inside a @JsonUnwrapped property #2784

Closed
mjustin opened this issue Jul 2, 2020 · 2 comments
Milestone

Comments

@mjustin
Copy link

mjustin commented Jul 2, 2020

BigDecimal values within a @JsonUnwrapped property are stripping trailing zeros from the JSON source when deserialized. This differs from the behavior of a non-@JsonUnwrapped property, where the trailing zeroes are retained. I would expect the same behavior in both cases, and that ideally (for my use case) the trailing zeroes are retained.

I have confirmed this issue in the latest Jackson, version 2.11.1.

This appears to be the same issue as #632, which was filed against Jackson 2.4.0. I'm opening this as a new issue as requested.

From my tests, this appears to only affect deserialization, and not serialization.

Here is a JUnit 5 test that verifies this difference:

import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class BigDecimalTrailingZeroesTest {
    private ObjectMapper objectMapper = new ObjectMapper();

    @Test
    public void bigDecimalTrailingZeroes() throws JsonProcessingException {
        // Passes
        assertEquals(new BigDecimal("5.00"),
                objectMapper.readValue("{\"value\": 5.00}", BigDecimalHolder.class).getValue());
    }

    @Test
    public void unwrappedBigDecimalTrailingZeroes() throws JsonProcessingException {
        // org.opentest4j.AssertionFailedError:
        // Expected :5.00
        // Actual   :5.0
        assertEquals(new BigDecimal("5.00"),
                objectMapper.readValue("{\"value\": 5.00}", NestedBigDecimalHolder.class).getHolder().getValue());
    }

    private static class BigDecimalHolder {
        private BigDecimal value;

        public BigDecimal getValue() {
            return value;
        }

        public void setValue(BigDecimal value) {
            this.value = value;
        }
    }

    private static class NestedBigDecimalHolder {
        @JsonUnwrapped
        private BigDecimalHolder holder;

        public BigDecimalHolder getHolder() {
            return holder;
        }

        public void setHolder(BigDecimalHolder holder) {
            this.holder = holder;
        }
    }
}
@cowtowncoder
Copy link
Member

One quick note: I wonder if this might be due to buffering required by @JsonUnwrapped, perhaps similar to #2644. It is not exactly same issue but possible related.

@cowtowncoder
Copy link
Member

Yes, I can reproduce this, will add a failing test. Most likely same problem as #2644, and same work-around applies here too: enable DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS.

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

No branches or pull requests

2 participants