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

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

                Author: ASF GitHub Bot
            Created on: 18/Apr/18 18:02
            Start Date: 18/Apr/18 18:02
    Worklog Time Spent: 10m 
      Work Description: tgroh closed pull request #5126: [BEAM-4069] Gracefully 
deserialize empty options structs
URL: https://github.com/apache/beam/pull/5126
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/runners/core-construction-java/src/test/java/org/apache/beam/runners/core/construction/PipelineOptionsTranslationTest.java
 
b/runners/core-construction-java/src/test/java/org/apache/beam/runners/core/construction/PipelineOptionsTranslationTest.java
index eb59bac95a1..24fd3811d34 100644
--- 
a/runners/core-construction-java/src/test/java/org/apache/beam/runners/core/construction/PipelineOptionsTranslationTest.java
+++ 
b/runners/core-construction-java/src/test/java/org/apache/beam/runners/core/construction/PipelineOptionsTranslationTest.java
@@ -20,12 +20,15 @@
 
 import static org.hamcrest.Matchers.equalTo;
 import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
 import static org.hamcrest.Matchers.nullValue;
 import static org.junit.Assert.assertThat;
 
 import com.fasterxml.jackson.annotation.JsonIgnore;
 import com.google.common.collect.ImmutableList;
+import com.google.protobuf.NullValue;
 import com.google.protobuf.Struct;
+import com.google.protobuf.Value;
 import org.apache.beam.sdk.options.ApplicationNameOptions;
 import org.apache.beam.sdk.options.Default;
 import org.apache.beam.sdk.options.PipelineOptions;
@@ -116,6 +119,24 @@ public void defaultsRestored() throws Exception {
 
       assertThat(deserialized.as(TestDefaultOptions.class).getDefault(), 
equalTo(19));
     }
+
+    @Test
+    public void emptyStructDeserializes() throws Exception {
+      Struct serialized = Struct.getDefaultInstance();
+      PipelineOptions deserialized = 
PipelineOptionsTranslation.fromProto(serialized);
+
+      assertThat(deserialized, notNullValue());
+    }
+
+    @Test
+    public void structWithNullOptionsDeserializes() throws Exception {
+      Struct serialized = Struct.newBuilder()
+          .putFields("options", 
Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build())
+          .build();
+      PipelineOptions deserialized = 
PipelineOptionsTranslation.fromProto(serialized);
+
+      assertThat(deserialized, notNullValue());
+    }
   }
 
   /** {@link PipelineOptions} with an unserializable option. */
diff --git 
a/sdks/java/core/src/main/java/org/apache/beam/sdk/options/ProxyInvocationHandler.java
 
b/sdks/java/core/src/main/java/org/apache/beam/sdk/options/ProxyInvocationHandler.java
index a127fd15e25..ccbe2ff50f8 100644
--- 
a/sdks/java/core/src/main/java/org/apache/beam/sdk/options/ProxyInvocationHandler.java
+++ 
b/sdks/java/core/src/main/java/org/apache/beam/sdk/options/ProxyInvocationHandler.java
@@ -732,15 +732,19 @@ private void ensureSerializable(
     @Override
     public PipelineOptions deserialize(JsonParser jp, DeserializationContext 
ctxt)
         throws IOException, JsonProcessingException {
-      ObjectNode objectNode = (ObjectNode) jp.readValueAsTree();
-      ObjectNode optionsNode = (ObjectNode) objectNode.get("options");
+      ObjectNode objectNode = jp.readValueAsTree();
+      JsonNode rawOptionsNode = objectNode.get("options");
 
       Map<String, JsonNode> fields = Maps.newHashMap();
-      for (Iterator<Map.Entry<String, JsonNode>> iterator = 
optionsNode.fields();
-          iterator.hasNext(); ) {
-        Map.Entry<String, JsonNode> field = iterator.next();
-        fields.put(field.getKey(), field.getValue());
+      if (rawOptionsNode != null && !rawOptionsNode.isNull()) {
+        ObjectNode optionsNode = (ObjectNode) rawOptionsNode;
+        for (Iterator<Map.Entry<String, JsonNode>> iterator = 
optionsNode.fields();
+            iterator != null && iterator.hasNext(); ) {
+          Map.Entry<String, JsonNode> field = iterator.next();
+          fields.put(field.getKey(), field.getValue());
+        }
       }
+
       PipelineOptions options =
           new ProxyInvocationHandler(Maps.newHashMap(), 
fields).as(PipelineOptions.class);
       ValueProvider.RuntimeValueProvider.setRuntimeOptions(options);


 

----------------------------------------------------------------
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: 92193)
    Time Spent: 1h 20m  (was: 1h 10m)

> Empty pipeline options can be gracefully serialized/deserialized
> ----------------------------------------------------------------
>
>                 Key: BEAM-4069
>                 URL: https://issues.apache.org/jira/browse/BEAM-4069
>             Project: Beam
>          Issue Type: Bug
>          Components: runner-core
>            Reporter: Ben Sidhom
>            Assignee: Ben Sidhom
>            Priority: Minor
>          Time Spent: 1h 20m
>  Remaining Estimate: 0h
>
> PipelineOptionsTranslation.fromProto currently crashes with a 
> NullPointerException when passed an empty options Struct. This is due to 
> ProxyInvocationHandler.Deserializer expecting a non-empty enclosing Struct.
> Empty pipeline options may be passed by SDKs interacting with a job server, 
> so this case needs to be handled. Note that testing a round-trip of an 
> effectively-empty Java PipelineOptions object is not sufficient to catch this 
> because "empty" Java options still contain default fields not defined in 
> other SDKs.



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

Reply via email to