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

"FieldValue.delete() is not supported at field" #480

Closed
alamothe opened this issue Nov 29, 2020 · 3 comments
Closed

"FieldValue.delete() is not supported at field" #480

alamothe opened this issue Nov 29, 2020 · 3 comments
Assignees
Labels
api: firestore Issues related to the googleapis/java-firestore API. type: question Request for information or clarification. Not an issue.

Comments

@alamothe
Copy link

Environment details

  1. Specify the API at the beginning of the title. For example, "BigQuery: ...").
  2. OS type and version: MacOS
  3. Java version: 11
  4. firestore version(s): com.google.firebase:firebase-admin:7.0.1

Steps to reproduce

Exception thrown while trying to delete a field:

batch.update(docRef, FieldPath.of("abc", "184.174.14.109", "xyz"), FieldValue.delete())

Error:

java.lang.IllegalArgumentException: FieldValue.delete() is not supported at field 'abc.`184`.`174`.`14`.`109`.xyz'.

	at com.google.common.base.Preconditions.checkArgument(Preconditions.java:217)
	at com.google.cloud.firestore.UserDataConverter.encodeValue(UserDataConverter.java:100)
	at com.google.cloud.firestore.UserDataConverter.encodeValue(UserDataConverter.java:163)
	at com.google.cloud.firestore.UserDataConverter.encodeValue(UserDataConverter.java:163)
	at com.google.cloud.firestore.UserDataConverter.encodeValue(UserDataConverter.java:163)
	at com.google.cloud.firestore.DocumentSnapshot.fromObject(DocumentSnapshot.java:91)
	at com.google.cloud.firestore.UpdateBuilder.performUpdate(UpdateBuilder.java:498)
	at com.google.cloud.firestore.UpdateBuilder.performUpdate(UpdateBuilder.java:486)
	at com.google.cloud.firestore.UpdateBuilder.update(UpdateBuilder.java:396)
	at com.google.cloud.firestore.DocumentReference.update(DocumentReference.java:279)

This happens before even reaching the backend, in client code.

I am also wondering if there is any workaround until this is resolved?

@product-auto-label product-auto-label bot added the api: firestore Issues related to the googleapis/java-firestore API. label Nov 29, 2020
@athakor athakor self-assigned this Nov 29, 2020
@athakor athakor added type: question Request for information or clarification. Not an issue. status: investigating The issue is under investigation, which is determined to be non-trivial. labels Nov 29, 2020
@athakor
Copy link
Contributor

athakor commented Dec 3, 2020

@alamothe Thanks for raising this issue. I follow your step and able to reproduce this issue. The cause of issue is that firebase-admin-7.0.1 use the firestore 1.35.0 version and that is quite old. I believe these issue is resolved in latest releases, see here.

To fix these problem exclude google-cloud-firestore:1.35.0 from firebase-admin-7.0.1 and explicitly add the latest version of firestore dependency i.e google-cloud-firestore:2.1.0 like below.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>issue-480</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>com.google.firebase</groupId>
            <artifactId>firebase-admin</artifactId>
            <version>7.0.1</version>
            <exclusions>
                <exclusion>
                    <groupId>com.google.cloud</groupId>
                    <artifactId>google-cloud-firestore</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.google.cloud</groupId>
            <artifactId>google-cloud-firestore</artifactId>
            <version>2.1.0</version>
        </dependency>
    </dependencies>
</project>

Sample

import com.google.api.core.ApiFuture;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.ServiceOptions;
import com.google.cloud.firestore.DocumentReference;
import com.google.cloud.firestore.DocumentSnapshot;
import com.google.cloud.firestore.FieldPath;
import com.google.cloud.firestore.FieldValue;
import com.google.cloud.firestore.Firestore;
import com.google.cloud.firestore.FirestoreOptions;
import com.google.cloud.firestore.WriteResult;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;

public class Test {
    public static void main(String[] args) throws ExecutionException, InterruptedException, IOException {

        FirestoreOptions firestoreOptions =
                FirestoreOptions.getDefaultInstance().toBuilder()
                        .setProjectId(ServiceOptions.getDefaultProjectId())
                        .setCredentials(GoogleCredentials.getApplicationDefault())
                        .build();
        Firestore db = firestoreOptions.getService();
        DocumentReference docRef = db.collection("col1").document("doc1");

        Map<String,Object> xyzMap = new HashMap();
        xyzMap.put("xyz","data");

        Map<String,Object> dotMap = new HashMap();
        dotMap.put("184.174.14.109",xyzMap);

        Map<String, Object> data = new HashMap();
        data.put("abc", dotMap);
        ApiFuture<WriteResult> writeResult = docRef.update(data); //NOTE: create doc1 if not exists using docRef.create(data)

        System.out.println("Before FieldValue.delete");
        DocumentSnapshot documentSnapshot = docRef.get().get();
        System.out.println(documentSnapshot.getData());

        FieldPath path = FieldPath.of("abc", "184.174.14.109", "xyz");
        docRef.update(path, FieldValue.delete()).get();

        System.out.println("After FieldValue.delete");
        documentSnapshot = docRef.get().get();
        System.out.println(documentSnapshot.getData());
    }
}

Output

Before FieldValue.delete
{abc={184.174.14.109={xyz=data}}}
After FieldValue.delete
{abc={184.174.14.109={}}}

Please let us know if this helps!

@athakor athakor removed the status: investigating The issue is under investigation, which is determined to be non-trivial. label Dec 3, 2020
@alamothe
Copy link
Author

alamothe commented Dec 7, 2020

Interesting! Thank you for answer. I'm actually pretty sure that we don't need firebase-admin at all, it was what we started with initially based on some docs. I will make the switch and let you know.

@alamothe
Copy link
Author

alamothe commented Dec 7, 2020

Works! Closing now, thanks!

@alamothe alamothe closed this as completed Dec 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: firestore Issues related to the googleapis/java-firestore API. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

2 participants