private JSONSchema(Class<T> pojo, Map<String, String> properties) { this.pojo = pojo; this.properties = properties; this.schema = ReflectData.AllowNull.get().getSchema(pojo); this.schemaInfo = new SchemaInfo(); this.schemaInfo.setName(""); this.schemaInfo.setProperties(properties); this.schemaInfo.setType(SchemaType.JSON); this.schemaInfo.setSchema(this.schema.toString().getBytes()); this.objectMapper = JSON_MAPPER.get(); }
@Test public void testWrappers() { // AllowNull only makes fields nullable, so testing must use a base record Schema wrappers = ReflectData.AllowNull.get().getSchema(Wrappers.class); Assert.assertEquals(nullableSchema(boolean.class), wrappers.getField("aBoolean").schema()); Assert.assertEquals(nullableSchema(byte.class), wrappers.getField("aByte").schema()); Assert.assertEquals(nullableSchema(short.class), wrappers.getField("aShort").schema()); Assert.assertEquals(nullableSchema(int.class), wrappers.getField("anInt").schema()); Assert.assertEquals(nullableSchema(long.class), wrappers.getField("aLong").schema()); Assert.assertEquals(nullableSchema(float.class), wrappers.getField("aFloat").schema()); Assert.assertEquals(nullableSchema(double.class), wrappers.getField("aDouble").schema()); Assert.assertEquals(nullableSchema(Primitives.class), wrappers.getField("anObject").schema()); }
@Test public void testPrimitives() { // AllowNull only makes fields nullable, so testing must use a base record Schema primitives = ReflectData.AllowNull.get().getSchema(Primitives.class); Assert.assertEquals(requiredSchema(boolean.class), primitives.getField("aBoolean").schema()); Assert.assertEquals(requiredSchema(byte.class), primitives.getField("aByte").schema()); Assert.assertEquals(requiredSchema(short.class), primitives.getField("aShort").schema()); Assert.assertEquals(requiredSchema(int.class), primitives.getField("anInt").schema()); Assert.assertEquals(requiredSchema(long.class), primitives.getField("aLong").schema()); Assert.assertEquals(requiredSchema(float.class), primitives.getField("aFloat").schema()); Assert.assertEquals(requiredSchema(double.class), primitives.getField("aDouble").schema()); }
@Test public void testReadNullsWithPrimitivesAllowNullSchema() { final String name = "allowNullPrimitives"; try { repo.create(NAMESPACE, name, new DatasetDescriptor.Builder() .schema(ReflectData.AllowNull.get().getSchema(ObjectPoJo.class)) .build(), ObjectPoJo.class); // should load the dataset because PrimitivePoJo can be used to write final Dataset<PrimitivePoJo> dataset = repo.load( NAMESPACE, name, PrimitivePoJo.class); TestHelpers.assertThrows("AllowNull primitives cannot read nullable type", IncompatibleSchemaException.class, new Runnable() { @Override public void run() { dataset.newReader(); } }); } catch (RuntimeException e) { throw e; } finally { repo.delete(NAMESPACE, name); } }
@Test public void testCreateWithAllowNullSchema() { String name = "allowNull"; try { repo.create(NAMESPACE, name, new DatasetDescriptor.Builder() .schema(ReflectData.AllowNull.get().getSchema(ObjectPoJo.class)) .build()); } catch (RuntimeException e) { throw e; } finally { repo.delete(NAMESPACE, name); } }
public Schema getSchema(Class<?> datasetClass) { if (schema != null) { return schema; } Schema s; if (allowNullValues) { s = ReflectData.AllowNull.get().getSchema(datasetClass); } else { s = ReflectData.get().getSchema(datasetClass); } return s; } }
@Test public void testAllowNullWithNullableAnnotation() { Schema withNullable = ReflectData.AllowNull.get() .getSchema(AllowNullWithNullable.class); Assert.assertEquals("Should produce a nullable double", nullableSchema(double.class), withNullable.getField("aDouble").schema()); Schema nullableDoubleOrLong = Schema.createUnion(Arrays.asList( Schema.create(Schema.Type.NULL), Schema.create(Schema.Type.DOUBLE), Schema.create(Schema.Type.LONG))); Assert.assertEquals("Should add null to a non-null union", nullableDoubleOrLong, withNullable.getField("doubleOrLong").schema()); Assert.assertEquals("Should add null to a non-null union", nullableDoubleOrLong, withNullable.getField("doubleOrLongOrNull1").schema()); Schema doubleOrLongOrNull = Schema.createUnion(Arrays.asList( Schema.create(Schema.Type.DOUBLE), Schema.create(Schema.Type.LONG), Schema.create(Schema.Type.NULL))); Assert.assertEquals("Should add null to a non-null union", doubleOrLongOrNull, withNullable.getField("doubleOrLongOrNull2").schema()); Assert.assertEquals("Should add null to a non-null union", doubleOrLongOrNull, withNullable.getField("doubleOrLongOrNull3").schema()); }
@Test public void testNullableStringableField() throws Exception { NullableStringable datum = new NullableStringable(); datum.number = java.math.BigDecimal.TEN; Schema schema = ReflectData.AllowNull.get().getSchema(NullableStringable.class); checkBinary(schema, datum); }
@Test public void testReflectWithAllowNullMatchStructure() throws SchemaValidationException { testValidatorPasses(builder.canBeReadStrategy().validateAll(), circleSchemaDifferentNames, ReflectData.AllowNull.get().getSchema(Circle.class)); }
private static <T> org.apache.avro.Schema createAvroSchema(Class<T> pojo) { return ReflectData.AllowNull.get().getSchema(pojo); }