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);
}
}
}
}
}
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);
}
}
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());
}
}
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.");
}
}
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);
}
Aggregations