googleapis / googleapis/google-cloud-java
[java-firestore] Does Firestore support writing to document from Groovy objects?
- Lenguaje dominante
- Java
- Estrellas
- 2.1k
- Forks
- 1.2k
- Merge medio
- 1 d 23 h
- PR fusionados (30 d)
- 154
Descripción
**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)
```
Guía de contribución
Evaluación
Este issue todavía no se ha evaluado.