You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2022/04/28 15:39:30 UTC

[GitHub] [beam] andreigurau commented on a diff in pull request #17477: [BEAM-10265] Display error message if trying to infer recursive schema from POJO/Proto objects

andreigurau commented on code in PR #17477:
URL: https://github.com/apache/beam/pull/17477#discussion_r861041123


##########
sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/utils/StaticSchemaInference.java:
##########
@@ -87,22 +88,33 @@ enum MethodType {
    * public getter methods, or special annotations on the class.
    */
   public static Schema schemaFromClass(
-      Class<?> clazz, FieldValueTypeSupplier fieldValueTypeSupplier) {
+      Class<?> clazz,
+      FieldValueTypeSupplier fieldValueTypeSupplier,
+      HashSet<Class> alreadyVisitedSchemas) {
+    if (alreadyVisitedSchemas.contains(clazz)) {
+      throw new RuntimeException(
+          "Cannot infer schema with a circular reference. Class: " + clazz.getTypeName());
+    }
+    alreadyVisitedSchemas.add(clazz);
     Schema.Builder builder = Schema.builder();
     for (FieldValueTypeInformation type : fieldValueTypeSupplier.get(clazz)) {
-      Schema.FieldType fieldType = fieldFromType(type.getType(), fieldValueTypeSupplier);
+      Schema.FieldType fieldType =
+          fieldFromType(type.getType(), fieldValueTypeSupplier, alreadyVisitedSchemas);
       if (type.isNullable()) {
         builder.addNullableField(type.getName(), fieldType);
       } else {
         builder.addField(type.getName(), fieldType);
       }
     }
+    alreadyVisitedSchemas.remove(clazz);

Review Comment:
   > It might be possible that multiple types nested under the same type reference each other in a non-circular way.
   
   I'm not following what you mean here, do you have an example?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org