Skip to content

[java-firestore] CustomClassMapper.convertBigDecimal calls .abs() on Double, unintentionally flipping negative values to positive #14533

Description

@hritikpgupta

Environment

  • Artifact: com.google.cloud:google-cloud-firestore (also used via com.google.firebase:firebase-admin)
  • Affected Versions: 3.21.1 through 3.48.0 (latest)
  • File: com.google.cloud.firestore.encoding.CustomClassMapper.java

Describe the Bug

When deserializing a Firestore document containing a negative floating point number into a Java POJO field of type java.math.BigDecimal using DocumentSnapshot.toObject(Class<T>), the negative value is converted to a positive number.


Offending Code

In com.google.cloud.firestore.encoding.CustomClassMapper.java :

private static BigDecimal convertBigDecimal(Object o, DeserializeContext.ErrorPath errorPath) {
    if (o instanceof Integer) {
        return BigDecimal.valueOf(((Integer) o).intValue());
    } else if (o instanceof Long) {
        return BigDecimal.valueOf(((Long) o).longValue());
    } else if (o instanceof Double) {
        return BigDecimal.valueOf(((Double) o).doubleValue()).abs(); // <--- Calls .abs()
    } else if (o instanceof BigDecimal) {
        return (BigDecimal) o;
    } else if (o instanceof String) {
        return new BigDecimal((String) o);
    } else {
        throw errorPath.deserializeError(
            "Failed to convert a value of type " + o.getClass().getName() + " to BigDecimal");
    }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions