Yes, this looks like a bug in GenericData#validate(). It should use GenericData#resolveUnion().
Please file an issue in Jira. If you are able, attach a patch that includes a test. Thanks, Doug On Tue, Dec 9, 2014 at 12:30 PM, Jeffrey Mullins (BLOOMBERG/ BOSTON) <jmullin...@bloomberg.net> wrote: > Hi, > > When invoking GenericData.get().validate() with a union schema containing > multiple record schemas an IndexOutOfBoundsException is possible. > > The exception results under the following conditions: > 1) Union contains multiple record schemas > 2) The first record schema in the union contains more fields than > subsequent record schemas > 3) validate() is invoked with the union schema and a record of one of the > subsequent record schemas. > > Below is a simple unit test which reproduces the exception. > > @Test > public void testValidateUnion() { > Schema type1Schema = SchemaBuilder.record("Type1") > .fields() > .requiredString("myString") > .requiredInt("myInt") > .endRecord(); > > Schema type2Schema = SchemaBuilder.record("Type2") > .fields() > .requiredString("myString") > .endRecord(); > > Schema unionSchema = SchemaBuilder.unionOf() > .type(type1Schema).and().type(type2Schema) > .endUnion(); > > GenericRecord record = new GenericData.Record(type2Schema); > record.put("myString", "myValue"); > GenericData.get().validate(unionSchema, record); > } > > I'm happy to submit a JIRA for this issue, but thought I'd check in with the > mailing list before doing so. > > Thanks, > Jeff