Search in sources :

Example 11 with RecordDataSchema

use of com.linkedin.data.schema.RecordDataSchema in project rest.li by linkedin.

the class AnyRecordValidator method validate.

@Override
public void validate(ValidatorContext context) {
    DataElement dataElement = context.dataElement();
    Object value = dataElement.getValue();
    DataSchema schema = dataElement.getSchema();
    if (schema.getType() != DataSchema.Type.RECORD || (((RecordDataSchema) schema).getFullName()).equals(SCHEMA_NAME) == false) {
        context.addResult(new Message(context.dataElement().path(), "%1$s invoked on schema that is not %2$s", AnyRecordValidator.class.getName(), SCHEMA_NAME));
    } else if (value.getClass() != DataMap.class) {
        context.addResult(new Message(context.dataElement().path(), "%1$s expects data to be a DataMap, data is %2$s", AnyRecordValidator.class.getName(), value));
    } else {
        DataMap dataMap = (DataMap) value;
        if (dataMap.size() != 1) {
            context.addResult(new Message(context.dataElement().path(), "%1$s expects data to be a DataMap with one entry, data is %2$s", AnyRecordValidator.class.getName(), value));
        } else {
            Map.Entry<String, Object> entry = dataMap.entrySet().iterator().next();
            String anySchemaName = entry.getKey();
            Object anyValue = entry.getValue();
            DataSchema anySchema = schemaFromName(context, anySchemaName);
            if (anySchema != null) {
                DataElement anyElement = new SimpleDataElement(anyValue, entry.getKey(), anySchema, dataElement);
                // do we want to have cache for anySchemaName to validator
                // do we care about classMap argument to DataSchemaAnnotationValidator
                DataSchemaAnnotationValidator validator = new DataSchemaAnnotationValidator(anySchema);
                if (validator.isInitOk() == false) {
                    boolean errorIfNotValidated = getParameter(context.validationOptions()).isValidSchema();
                    context.addResult(new Message(context.dataElement().path(), errorIfNotValidated, "%1$s failed to initialize %2$s with %3$s", AnyRecordValidator.class.getName(), DataSchemaAnnotationValidator.class.getSimpleName(), anySchema));
                    addResult(context, errorIfNotValidated, validator.getInitMessages());
                } else {
                    ValidationResult result = ValidateDataAgainstSchema.validate(anyElement, context.validationOptions(), validator);
                    addResult(context, result.getMessages());
                    if (result.hasFix())
                        context.setHasFix(true);
                    if (result.hasFixupReadOnlyError())
                        context.setHasFixupReadOnlyError(true);
                }
            }
        }
    }
}
Also used : RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) SimpleDataElement(com.linkedin.data.element.SimpleDataElement) DataElement(com.linkedin.data.element.DataElement) Message(com.linkedin.data.message.Message) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) SimpleDataElement(com.linkedin.data.element.SimpleDataElement) ValidationResult(com.linkedin.data.schema.validation.ValidationResult) DataMap(com.linkedin.data.DataMap)

Example 12 with RecordDataSchema

use of com.linkedin.data.schema.RecordDataSchema in project rest.li by linkedin.

the class TestDataTemplateUtil method fieldInfo.

public static FieldInfo fieldInfo(RecordTemplate recordTemplate, String fieldName) {
    RecordDataSchema schema = recordTemplate.schema();
    RecordDataSchema.Field field = schema.getField(fieldName);
    String getterName = methodName(field.getType().getDereferencedType() == DataSchema.Type.BOOLEAN ? "is" : "get", fieldName);
    try {
        Method method = recordTemplate.getClass().getMethod(getterName);
        Class<?> fieldClass = method.getReturnType();
        return new FieldInfo(field, fieldClass);
    } catch (NoSuchMethodException e) {
        throw new IllegalStateException("Cannot find method " + getterName, e);
    }
}
Also used : RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) Method(java.lang.reflect.Method)

Example 13 with RecordDataSchema

use of com.linkedin.data.schema.RecordDataSchema in project rest.li by linkedin.

the class TestSchemaSampleDataGenerator method testTyperefSchema.

@Test
public void testTyperefSchema() {
    final RecordDataSchema schema = (RecordDataSchema) DataTemplateUtil.getSchema(TyperefTest.class);
    final DataMap value = SchemaSampleDataGenerator.buildRecordData(schema, _spec);
    for (RecordDataSchema.Field field : schema.getFields()) {
        final DataSchema fieldSchema = field.getType();
        if (!(fieldSchema instanceof TyperefDataSchema)) {
            continue;
        }
        final TyperefDataSchema fieldTyperefSchema = (TyperefDataSchema) field.getType();
        final Object fieldValue = value.get(field.getName());
        final Object rebuildValue = SchemaSampleDataGenerator.buildData(fieldTyperefSchema.getDereferencedDataSchema(), _spec);
        Assert.assertSame(fieldValue.getClass(), rebuildValue.getClass());
    }
}
Also used : UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) PrimitiveDataSchema(com.linkedin.data.schema.PrimitiveDataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) TyperefTest(com.linkedin.pegasus.generator.test.TyperefTest) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataMap(com.linkedin.data.DataMap) UnionTest(com.linkedin.pegasus.generator.test.UnionTest) Test(org.testng.annotations.Test) TyperefTest(com.linkedin.pegasus.generator.test.TyperefTest)

Example 14 with RecordDataSchema

use of com.linkedin.data.schema.RecordDataSchema in project rest.li by linkedin.

the class TestSchemaSampleDataGenerator method testRecursivelyReferencedSchema.

@Test
public void testRecursivelyReferencedSchema() {
    try {
        final RecordDataSchema schema = (RecordDataSchema) DataTemplateUtil.getSchema(SelfReference.class);
        final DataMap data = SchemaSampleDataGenerator.buildRecordData(schema, _spec);
        Assert.assertTrue(data.getDataList("listRef").getDataMap(0).getDataList("listRef").isEmpty(), "Self referenced schema in list should not be embedded recursively");
        final String firstKey = data.getDataMap("mapRef").keySet().iterator().next();
        Assert.assertTrue(data.getDataMap("mapRef").getDataMap(firstKey).getDataMap("mapRef").isEmpty(), "Self referenced schema in map should not be embedded recursively");
        Assert.assertFalse(data.getDataMap("indirectRef").containsKey("ref"), "Self referenced schema (via indirect reference) should not be embedded recursively");
        Assert.assertFalse(data.getDataMap("unionRef").containsKey("com.linkedin.pegasus.generator.test.SelfReference"), "Self referenced schema in union should not be embedded recursively");
    } catch (StackOverflowError e) {
        Assert.fail("Self reference in schema should not cause stack overflow during doc gen.");
    }
}
Also used : SelfReference(com.linkedin.pegasus.generator.test.SelfReference) InvalidSelfReference(com.linkedin.pegasus.generator.test.InvalidSelfReference) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ByteString(com.linkedin.data.ByteString) DataMap(com.linkedin.data.DataMap) UnionTest(com.linkedin.pegasus.generator.test.UnionTest) Test(org.testng.annotations.Test) TyperefTest(com.linkedin.pegasus.generator.test.TyperefTest)

Example 15 with RecordDataSchema

use of com.linkedin.data.schema.RecordDataSchema in project rest.li by linkedin.

the class TestCustomAnyRecord method testCustomAnyRecordSchema.

@Test
public void testCustomAnyRecordSchema() {
    RecordDataSchema schemaFromInstance = (new AnyRecord()).schema();
    DataSchema schemaFromClass = DataTemplateUtil.getSchema(AnyRecord.class);
    assertSame(schemaFromClass, schemaFromInstance);
    CustomAnyRecord custom = new CustomAnyRecord();
    RecordDataSchema customSchemaFromInstance = custom.schema();
    DataSchema customSchemaFromClass = DataTemplateUtil.getSchema(CustomAnyRecord.class);
    assertSame(customSchemaFromClass, customSchemaFromInstance);
    assertEquals(customSchemaFromClass, schemaFromClass);
}
Also used : RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) Test(org.testng.annotations.Test)

Aggregations

RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)63 DataMap (com.linkedin.data.DataMap)26 DataSchema (com.linkedin.data.schema.DataSchema)25 Test (org.testng.annotations.Test)24 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)15 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)14 MapDataSchema (com.linkedin.data.schema.MapDataSchema)12 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)12 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)10 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)8 Name (com.linkedin.data.schema.Name)8 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)7 ArrayList (java.util.ArrayList)7 FieldDef (com.linkedin.data.template.FieldDef)6 Schema (org.apache.avro.Schema)6 DataList (com.linkedin.data.DataList)5 ActionResponse (com.linkedin.restli.common.ActionResponse)5 GenericRecord (org.apache.avro.generic.GenericRecord)5 SchemaParser (com.linkedin.data.schema.SchemaParser)4 TyperefTest (com.linkedin.pegasus.generator.test.TyperefTest)4