Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Firestore: set() does not accept pojos with float fields #5610

@menulis

Description

@menulis

google-cloud-firestore version: 1.10.0

Steps to reproduce

  1. Create a class having a java.lang.Float field
  2. Save an instance of such class to Firestore calling set() that accepts pojo

Stack trace

Exception in thread "main" com.google.cloud.firestore.FirestoreException: Cannot convert 0.1 to Firestore Value
	at com.google.cloud.firestore.FirestoreException.invalidState(FirestoreException.java:51)
	at com.google.cloud.firestore.UserDataConverter.encodeValue(UserDataConverter.java:178)
	at com.google.cloud.firestore.DocumentSnapshot.fromObject(DocumentSnapshot.java:89)
	at com.google.cloud.firestore.UpdateBuilder.performSet(UpdateBuilder.java:245)
	at com.google.cloud.firestore.UpdateBuilder.set(UpdateBuilder.java:229)
	at com.google.cloud.firestore.UpdateBuilder.set(UpdateBuilder.java:207)

I assume this is a bug, because Float (along with Double) is an accepted data type in CustomClassMapper#serialize method:

 private static <T> Object serialize(T o, ErrorPath path) {
    if (path.getLength() > MAX_DEPTH) {
      throw serializeError(
          path,
          "Exceeded maximum depth of "
              + MAX_DEPTH
              + ", which likely indicates there's an object cycle");
    }
    if (o == null) {
      return null;
    } else if (o instanceof Number) {
      if (o instanceof Long || o instanceof Integer || o instanceof Double || o instanceof Float) {
        return o;
      } else {
        throw serializeError(
            path,
            String.format(
                "Numbers of type %s are not supported, please use an int, long, float or double",
                o.getClass().getSimpleName()));
      }
    } else if (o instanceof String) {
    // [...]

However, this is not the case for the UserDataConverter#encodeValue method, where Integer, Long and Double are the only allowed numeric data types, thus FirestoreException is throw for floats.

Metadata

Metadata

Assignees

Labels

api: firestoreIssues related to the Firestore API.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions