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

Does Firestore support writing to document from Groovy objects? #996

Open
emmileaf opened this issue Jul 26, 2022 · 0 comments
Open

Does Firestore support writing to document from Groovy objects? #996

emmileaf opened this issue Jul 26, 2022 · 0 comments
Labels
api: firestore Issues related to the googleapis/java-firestore API. type: question Request for information or clarification. Not an issue.

Comments

@emmileaf
Copy link

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 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)

@emmileaf emmileaf added the type: question Request for information or clarification. Not an issue. label Jul 26, 2022
@product-auto-label product-auto-label bot added the api: firestore Issues related to the googleapis/java-firestore API. label Jul 26, 2022
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

1 participant