googleapis / googleapis/google-cloud-java

[java-firestore] Does Firestore support writing to document from Groovy objects?

Đang mở
#13,129 1 bình luận 0 reaction 0 người được giao Xem trên GitHub
api: firestore type: question
Ngôn ngữ chính
Java
Star
2.1k
Fork
1.2k
Merge trung bình
1 ngày 23 giờ
Pull request đã merge (30 ngày)
154

Mô tả

**Question:**

Does the Firestore client library currently (or have plans to) support writing data to document from Groovy objects?

**Context:**

Spring Cloud GCP currently calls the client library to serialize objects and convert them into documents for Firestore, but have received an issue (GoogleCloudPlatform/spring-cloud-gcp#939) regarding Groovy objects which have additional hidden fields. In particular, the hidden field `metaClass.additionalMetaMethods` is of type array and cannot be serialized by `CustomClassMapper`.

Looking at the existing logic in `CustomClassMapper` to filter out transient fields, it seems like [traversing the class hierarchy](https://github.com/googleapis/java-firestore/blob/9c42c70062dc16493ad1361cff4e04ce117a3d14/google-cloud-firestore/src/main/java/com/google/cloud/firestore/CustomClassMapper.java#L761) would reach the Superclass ` groovy.lang.MetaClassImpl` before `java.lang.Object`. Unlike the `User` subclass’s `metaClass` field, these Superclass fields are not marked as transient and therefore getting serialized.

cc: @elefeint

**Snippets to reproduce issue in Java:**

`User.groovy`
```
class User {
String id;
String name;

}
```

`Application.java`
```
public class Application {

private final GroovyClassLoader loader;
private final Firestore db;

public Application() throws IOException {
this.loader = new GroovyClassLoader(this.getClass().getClassLoader());
this.db = FirestoreOptions.getDefaultInstance().getService();
}

public static void main(String[] args) throws Exception {
Application app = new Application();
app.writeDocumentFromObjectGroovy();
}

private void writeDocumentFromObjectGroovy() throws Exception {
// Add document data with id "103" using a custom Groovy User class
Class userClass = loader.parseClass(new File("src/main/groovy/", "User.groovy"));
GroovyObject data =
(GroovyObject)
userClass
.getDeclaredConstructor(String.class, String.class)
.newInstance("103", "Carol");

// The following line throws error below:
WriteResult writeResult = this.db.collection("users").document("103").set(data).get();
}
}
```

**Error stack trace:**

```
Exception in thread "main" java.lang.IllegalArgumentException: Could not serialize object. Serializing Arrays is not supported, please use Lists instead (found in field 'metaClass.additionalMetaMethods')
at com.google.cloud.firestore.CustomClassMapper.serializeError(CustomClassMapper.java:606)
at com.google.cloud.firestore.CustomClassMapper.serialize(CustomClassMapper.java:161)
at com.google.cloud.firestore.CustomClassMapper.access$500(CustomClassMapper.java:53)
at com.google.cloud.firestore.CustomClassMapper$BeanMapper.serialize(CustomClassMapper.java:969)
at com.google.cloud.firestore.CustomClassMapper.serialize(CustomClassMapper.java:181)
at com.google.cloud.firestore.CustomClassMapper.access$500(CustomClassMapper.java:53)
at com.google.cloud.firestore.CustomClassMapper$BeanMapper.serialize(CustomClassMapper.java:969)
at com.google.cloud.firestore.CustomClassMapper.serialize(CustomClassMapper.java:181)
at com.google.cloud.firestore.CustomClassMapper.serialize(CustomClassMapper.java:104)
at com.google.cloud.firestore.CustomClassMapper.convertToPlainJavaTypes(CustomClassMapper.java:79)
at com.google.cloud.firestore.UpdateBuilder.set(UpdateBuilder.java:239)
at com.google.cloud.firestore.UpdateBuilder.set(UpdateBuilder.java:221)
at com.google.cloud.firestore.DocumentReference.set(DocumentReference.java:202)
at Application.writeDocumentFromObjectGroovy(Application.java:40)
at Application.main(Application.java:26)

```

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.