[ 
https://issues.apache.org/jira/browse/BEAM-6452?focusedWorklogId=186063&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-186063
 ]

ASF GitHub Bot logged work on BEAM-6452:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 16/Jan/19 23:52
            Start Date: 16/Jan/19 23:52
    Worklog Time Spent: 10m 
      Work Description: reuvenlax commented on pull request #7536: [BEAM-6452] 
Fix NullPointerException caused by nested collections
URL: https://github.com/apache/beam/pull/7536#discussion_r248495238
 
 

 ##########
 File path: 
sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/FieldValueTypeInformation.java
 ##########
 @@ -157,68 +157,94 @@ public FieldValueTypeInformation withName(String name) {
     return toBuilder().setName(name).build();
   }
 
-  private static Type getArrayComponentType(Field field) {
+  private static FieldValueTypeInformation getArrayComponentType(Field field) {
     return getArrayComponentType(TypeDescriptor.of(field.getGenericType()));
   }
 
   @Nullable
-  private static Type getArrayComponentType(TypeDescriptor valueType) {
+  private static FieldValueTypeInformation 
getArrayComponentType(TypeDescriptor valueType) {
+    // TODO: Figure out nullable elements.
+    TypeDescriptor componentType = null;
     if (valueType.isArray()) {
       Type component = valueType.getComponentType().getType();
       if (!component.equals(byte.class)) {
-        return component;
+        componentType = TypeDescriptor.of(component);
       }
     } else if (valueType.isSubtypeOf(TypeDescriptor.of(Collection.class))) {
       TypeDescriptor<Collection<?>> collection = 
valueType.getSupertype(Collection.class);
       if (collection.getType() instanceof ParameterizedType) {
         ParameterizedType ptype = (ParameterizedType) collection.getType();
         java.lang.reflect.Type[] params = ptype.getActualTypeArguments();
         checkArgument(params.length == 1);
-        return params[0];
+        componentType = TypeDescriptor.of(params[0]);
       } else {
         throw new RuntimeException("Collection parameter is not 
parameterized!");
       }
     }
-    return null;
+    if (componentType == null) {
+      return null;
+    }
+
+    return new AutoValue_FieldValueTypeInformation.Builder()
+        .setName("")
+        .setNullable(false)
+        .setType(componentType)
+        .setRawType(componentType.getRawType())
+        .setElementType(getArrayComponentType(componentType))
+        .setMapKeyType(getMapKeyType(componentType))
+        .setMapValueType(getMapValueType(componentType))
+        .build();
   }
 
   // If the Field is a map type, returns the key type, otherwise returns a 
null reference.
   @Nullable
-  private static Type getMapKeyType(Field field) {
+  private static FieldValueTypeInformation getMapKeyType(Field field) {
     return getMapKeyType(TypeDescriptor.of(field.getGenericType()));
   }
 
   @Nullable
-  private static Type getMapKeyType(TypeDescriptor<?> typeDescriptor) {
+  private static FieldValueTypeInformation getMapKeyType(TypeDescriptor<?> 
typeDescriptor) {
     return getMapType(typeDescriptor, 0);
   }
 
   // If the Field is a map type, returns the value type, otherwise returns a 
null reference.
   @Nullable
-  private static Type getMapValueType(Field field) {
+  private static FieldValueTypeInformation getMapValueType(Field field) {
     return getMapType(TypeDescriptor.of(field.getGenericType()), 1);
   }
 
   @Nullable
-  private static Type getMapValueType(TypeDescriptor typeDescriptor) {
+  private static FieldValueTypeInformation getMapValueType(TypeDescriptor 
typeDescriptor) {
     return getMapType(typeDescriptor, 1);
   }
 
   // If the Field is a map type, returns the key or value type (0 is key type, 
1 is value).
   // Otherwise returns a null reference.
   @SuppressWarnings("unchecked")
   @Nullable
-  private static Type getMapType(TypeDescriptor valueType, int index) {
+  private static FieldValueTypeInformation getMapType(TypeDescriptor 
valueType, int index) {
+    TypeDescriptor mapType = null;
     if (valueType.isSubtypeOf(TypeDescriptor.of(Map.class))) {
       TypeDescriptor<Collection<?>> map = valueType.getSupertype(Map.class);
       if (map.getType() instanceof ParameterizedType) {
         ParameterizedType ptype = (ParameterizedType) map.getType();
         java.lang.reflect.Type[] params = ptype.getActualTypeArguments();
-        return params[index];
+        mapType = TypeDescriptor.of(params[index]);
       } else {
         throw new RuntimeException("Map type is not parameterized! " + map);
       }
     }
-    return null;
+    if (mapType == null) {
+      return null;
+    }
+    return new AutoValue_FieldValueTypeInformation.Builder()
+        .setName("")
+        .setNullable(false)
+        .setType(mapType)
+        .setRawType(mapType.getRawType())
+        .setElementType(getArrayComponentType(mapType))
+        .setMapKeyType(getMapKeyType(mapType))
+        .setMapValueType(getMapValueType(mapType))
+        .build();
   }
-}
+}
 
 Review comment:
   done
 
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Issue Time Tracking
-------------------

    Worklog Id:     (was: 186063)
    Time Spent: 1h 20m  (was: 1h 10m)

> Nested collection types cause NullPointerException when converting to a POJO
> ----------------------------------------------------------------------------
>
>                 Key: BEAM-6452
>                 URL: https://issues.apache.org/jira/browse/BEAM-6452
>             Project: Beam
>          Issue Type: Sub-task
>          Components: beam-model
>            Reporter: Reuven Lax
>            Assignee: Reuven Lax
>            Priority: Major
>          Time Spent: 1h 20m
>  Remaining Estimate: 0h
>




--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to