Skip to content

Commit

Permalink
AVRO-1544. Java: Fix GenericData#validate for unions with null. Contr…
Browse files Browse the repository at this point in the history
…ibuted by Matthew Hayes.

git-svn-id: https://svn.apache.org/repos/asf/avro/trunk@1615764 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
cutting committed Aug 4, 2014
1 parent 0d57724 commit b8dec14
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Trunk (not yet released)

AVRO-1553. Java: MapReduce never uses MapOutputValueSchema (tomwhite)

AVRO-1544. Java: Fix GenericData#validate for unions with null.
(Matthew Hayes via cutting)

Avro 1.7.7 (23 July 2014)

NEW FEATURES
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ public boolean validate(Schema schema, Object datum) {
}
return true;
case ENUM:
if (datum == null) return false;
return schema.getEnumSymbols().contains(datum.toString());
case ARRAY:
if (!(isArray(datum))) return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,4 +404,40 @@ public void testByteBufferDeepCopy() {

assertEquals(buffer, buffer_copy);
}

@Test
public void testValidateNullableEnum() {
List<Schema> unionTypes = new ArrayList<Schema>();
Schema schema;
Schema nullSchema = Schema.create(Type.NULL);
Schema enumSchema = Schema.createEnum("AnEnum", null, null, Arrays.asList("X","Y","Z"));
GenericEnumSymbol w = new GenericData.EnumSymbol(enumSchema, "W");
GenericEnumSymbol x = new GenericData.EnumSymbol(enumSchema, "X");
GenericEnumSymbol y = new GenericData.EnumSymbol(enumSchema, "Y");
GenericEnumSymbol z = new GenericData.EnumSymbol(enumSchema, "Z");

// null is first
unionTypes.clear();
unionTypes.add(nullSchema);
unionTypes.add(enumSchema);
schema = Schema.createUnion(unionTypes);

assertTrue(GenericData.get().validate(schema, z));
assertTrue(GenericData.get().validate(schema, y));
assertTrue(GenericData.get().validate(schema, x));
assertFalse(GenericData.get().validate(schema, w));
assertTrue(GenericData.get().validate(schema, null));

// null is last
unionTypes.clear();
unionTypes.add(enumSchema);
unionTypes.add(nullSchema);
schema = Schema.createUnion(unionTypes);

assertTrue(GenericData.get().validate(schema, z));
assertTrue(GenericData.get().validate(schema, y));
assertTrue(GenericData.get().validate(schema, x));
assertFalse(GenericData.get().validate(schema, w));
assertTrue(GenericData.get().validate(schema, null));
}
}

0 comments on commit b8dec14

Please sign in to comment.