Skip to content

Commit

Permalink
Fix NPE when emitting null BigQuery record fields.
Browse files Browse the repository at this point in the history
When a record field on a class were marshalling is null, we used to throw an exception.

java.lang.NullPointerException
  at com.google.appengine.tools.mapreduce.impl.BigQueryDataMarshallerByType.mapFieldNameToValue(BigQueryDataMarshallerByType.java:54)
  at com.google.appengine.tools.mapreduce.impl.BigQueryDataMarshallerByType.mapFieldNameToValue(BigQueryDataMarshallerByType.java:72)
  at com.google.appengine.tools.mapreduce.impl.BigQueryMarshallerByType.toBytes(BigQueryMarshallerByType.java:61)
  at com.google.appengine.tools.mapreduce.BigQueryDataMarshallerTest.toJsonString(BigQueryDataMarshallerTest.java:81)
  at com.google.appengine.tools.mapreduce.BigQueryDataMarshallerTest.testGeneratedJson(BigQueryDataMarshallerTest.java:60)
  at com.google.appengine.tools.mapreduce.BigQueryDataMarshallerTest.testGeneratedJsonForClassWithRecord(BigQueryDataMarshallerTest.java:347)

With this fix we'll now emit 'null' as the json value. For example:  {'record':null}.
  • Loading branch information
bmenasha committed Sep 8, 2015
1 parent 3baf858 commit 51e09c0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public BigQueryDataMarshallerByType(Map<Field, BigqueryFieldMarshaller> marshall
* @return a nested map of field name to field value.
*/
Map<String, Object> mapFieldNameToValue(Object toMap) {
if (toMap == null) {
return null;
}
Class<?> typeOfObjectToMap = toMap.getClass();
Map<String, Object> toRet = new HashMap<>();
Set<Field> fieldsToMap = getFieldsToSerialize(typeOfObjectToMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void testGeneratedJson(String expected, T actual) {
try {
assertTrue(mapper.readTree(expected).equals(mapper.readTree(actualJson)));
} catch (IOException e) {
fail("Exception while serializing. Expected " + expected);
fail("Exception while serializing. Expected " + expected + " got " + actualJson);
}
}

Expand Down Expand Up @@ -338,6 +338,44 @@ public ClassWithMap(Map map, int id) {
}
}

@SuppressWarnings("unused")
public void testGeneratedJsonForClassWithRecord() {
BigQueryDataMarshallerTester<ClassWithRecord> tester =
new BigQueryDataMarshallerTester<ClassWithRecord>(
new BigQueryMarshallerByType<ClassWithRecord>(ClassWithRecord.class));

tester.testGeneratedJson("{\"record\":null}",
new ClassWithRecord(null));

tester.testGeneratedJson("{\"record\":{\"name\":\"myname\",\"value\":\"myvalue\"}}",
new ClassWithRecord(new ClassWithRecord.Record("myname","myvalue")));

tester.testSchema(new TableSchema().setFields(Lists.newArrayList(new TableFieldSchema()
.setName("record").setType("record").setFields(Lists.newArrayList(new TableFieldSchema()
.setName("name").setType("string"), new TableFieldSchema()
.setName("value").setType("string"))))));
}


private static class ClassWithRecord {
private static class Record{
public String name;
public String value;
public Record(String name, String value){
this.name=name;
this.value=value;
}
}

@SuppressWarnings("unused")
Record record;

@SuppressWarnings("unused")
public ClassWithRecord(Record record) {
this.record = record;
}
}

public void testGeneratedJsonForClassExtendingAbstractClass() {
BigQueryDataMarshallerTester<ClassExtendingAbstract> tester =
new BigQueryDataMarshallerTester<>(
Expand Down

0 comments on commit 51e09c0

Please sign in to comment.