use of com.linkedin.data.schema.RecordDataSchema in project rest.li by linkedin.
the class DataSchemaAnnotationValidator method validate.
@Override
public void validate(ValidatorContext context) {
DataElement element = context.dataElement();
DataSchema schema = element.getSchema();
if (schema != null) {
validateSchema(context, schema);
}
DataElement parentElement = element.getParent();
if (parentElement != null) {
DataSchema parentSchema = parentElement.getSchema();
// validators.
if (parentSchema != null && parentSchema.getType() == DataSchema.Type.RECORD) {
Object name = element.getName();
if (name.getClass() == String.class) {
RecordDataSchema recordDataSchema = (RecordDataSchema) parentSchema;
RecordDataSchema.Field field = recordDataSchema.getField((String) name);
if (field != null) {
getAndInvokeValidatorList(context, field);
}
}
}
}
}
use of com.linkedin.data.schema.RecordDataSchema in project rest.li by linkedin.
the class SchemaSampleDataGenerator method buildRecordData.
private static DataMap buildRecordData(ParentSchemas parentSchemas, NamedDataSchema schema, DataGenerationOptions spec) {
spec = preventRecursionIntoAlreadyTraversedSchemas(parentSchemas, spec, schema);
parentSchemas.incrementReferences(schema);
final DataMap data = new DataMap();
if (schema instanceof RecordDataSchema) {
for (RecordDataSchema.Field field : ((RecordDataSchema) schema).getFields()) {
if (!(spec.isRequiredFieldsOnly() && field.getOptional())) {
final Object value;
if (spec.isUseDefaults() && field.getDefault() != null) {
value = field.getDefault();
} else {
value = buildData(parentSchemas, field.getType(), field.getName(), spec);
}
// null is returned for NULL Pegasus type (used in unions, primarily)
if (value == null) {
data.remove(field.getName());
} else {
data.put(field.getName(), value);
}
}
}
} else if (schema instanceof TyperefDataSchema) {
data.put("ref", buildData(parentSchemas, schema.getDereferencedDataSchema(), spec));
} else {
data.put("value", buildData(parentSchemas, schema, spec));
}
parentSchemas.decrementReferences(schema);
return data;
}
use of com.linkedin.data.schema.RecordDataSchema in project rest.li by linkedin.
the class TestDataSchemaResolver method testClasspathResourceDataSchemaResolver.
@Test
public void testClasspathResourceDataSchemaResolver() {
final ClasspathResourceDataSchemaResolver resolver = new ClasspathResourceDataSchemaResolver(SchemaParserFactory.instance());
final PegasusSchemaParser parser = new SchemaParser(resolver);
final String existingSchemaName = "com.linkedin.data.schema.ValidationDemo";
final String nonExistSchemaName = "Non-Existing Schema";
final DataSchema existSchema = parser.lookupName(existingSchemaName);
assertNotNull(existSchema);
assertTrue(existSchema instanceof RecordDataSchema);
assertEquals(((RecordDataSchema) existSchema).getFullName(), existingSchemaName);
final DataSchema nonExistSchema = parser.lookupName(nonExistSchemaName);
assertNull(nonExistSchema);
assertTrue(parser.errorMessage().contains(nonExistSchemaName));
}
use of com.linkedin.data.schema.RecordDataSchema in project rest.li by linkedin.
the class TestDataSchemaResolver method testClassNameDataSchemaResolver.
@Test
public void testClassNameDataSchemaResolver() {
@SuppressWarnings("deprecation") final ClassNameDataSchemaResolver resolver = new ClassNameDataSchemaResolver();
final PegasusSchemaParser parser = new SchemaParser(resolver);
final Class<? extends RecordTemplate> testClass = ClassNameFooRecord.class;
final String nonExistSchemaName = "Non-Existing Schema";
final DataSchema existSchema = parser.lookupName(testClass.getName());
assertNotNull(existSchema);
assertTrue(existSchema instanceof RecordDataSchema);
assertEquals(((RecordDataSchema) existSchema).getFullName(), testClass.getCanonicalName());
assertFalse(resolver.isBadLocation(new ClassNameDataSchemaLocation(testClass.getName())));
final DataSchema nonExistSchema = parser.lookupName(nonExistSchemaName);
assertNull(nonExistSchema);
assertTrue(parser.errorMessage().contains(nonExistSchemaName));
assertTrue(resolver.isBadLocation(new ClassNameDataSchemaLocation(nonExistSchemaName)));
}
use of com.linkedin.data.schema.RecordDataSchema in project rest.li by linkedin.
the class TestDataElement method testDataElement.
@Test(dataProvider = "dataElementFactories")
public void testDataElement(DataElementFactory factory) throws IOException {
RecordDataSchema fooSchema = (RecordDataSchema) TestUtil.dataSchemaFromString(fooSchemaText);
ArrayDataSchema arraySchema = (ArrayDataSchema) fooSchema.getField("array").getType();
String fooText = "{\n" + " \"int\" : 34,\n" + " \"string\" : \"abc\",\n" + " \"array\" : [\n" + " { \"int\" : 56 },\n" + " { \"string\" : \"xyz\" },\n" + " { \"array\" : [\n" + " { \"int\" : 78 }\n" + " ] }\n" + " ]\n" + "}\n";
DataMap foo = TestUtil.dataMapFromString(fooText);
DataElement root = factory.create(foo, DataElement.ROOT_NAME, fooSchema, null);
DataElement int1 = factory.create(foo.get("int"), "int", fooSchema.getField("int").getType(), root);
DataElement string1 = factory.create(foo.get("string"), "string", fooSchema.getField("string").getType(), root);
DataElement array1 = factory.create(foo.get("array"), "array", fooSchema.getField("array").getType(), root);
DataElement foo20 = factory.create(array1.getChild(0), 0, arraySchema.getItems(), array1);
DataElement foo21 = factory.create(array1.getChild(1), 1, arraySchema.getItems(), array1);
DataElement foo22 = factory.create(array1.getChild(2), 2, arraySchema.getItems(), array1);
DataElement int20 = factory.create(foo20.getChild("int"), "int", fooSchema.getField("int").getType(), foo20);
DataElement string21 = factory.create(foo21.getChild("string"), "string", fooSchema.getField("string").getType(), foo21);
DataElement array22 = factory.create(foo22.getChild("array"), "array", fooSchema.getField("array").getType(), foo22);
DataElement foo30 = factory.create(array22.getChild(0), 0, arraySchema.getItems(), array22);
DataElement int30 = factory.create(foo30.getChild("int"), "int", fooSchema.getField("int").getType(), foo30);
// test path
Object[][] testPathInput = { { root, foo, fooSchema, new Object[] {} }, { int1, foo.get("int"), DataSchemaConstants.INTEGER_DATA_SCHEMA, new Object[] { "int" } }, { string1, foo.get("string"), DataSchemaConstants.STRING_DATA_SCHEMA, new Object[] { "string" } }, { array1, foo.get("array"), arraySchema, new Object[] { "array" } }, { foo20, ((DataList) foo.get("array")).get(0), fooSchema, new Object[] { "array", 0 } }, { foo21, ((DataList) foo.get("array")).get(1), fooSchema, new Object[] { "array", 1 } }, { foo22, ((DataList) foo.get("array")).get(2), fooSchema, new Object[] { "array", 2 } }, { int20, ((DataMap) ((DataList) foo.get("array")).get(0)).get("int"), DataSchemaConstants.INTEGER_DATA_SCHEMA, new Object[] { "array", 0, "int" } }, { string21, ((DataMap) ((DataList) foo.get("array")).get(1)).get("string"), DataSchemaConstants.STRING_DATA_SCHEMA, new Object[] { "array", 1, "string" } }, { array22, ((DataMap) ((DataList) foo.get("array")).get(2)).get("array"), arraySchema, new Object[] { "array", 2, "array" } }, { foo30, ((DataList) ((DataMap) ((DataList) foo.get("array")).get(2)).get("array")).get(0), fooSchema, new Object[] { "array", 2, "array", 0 } }, { int30, ((DataMap) ((DataList) ((DataMap) ((DataList) foo.get("array")).get(2)).get("array")).get(0)).get("int"), DataSchemaConstants.INTEGER_DATA_SCHEMA, new Object[] { "array", 2, "array", 0, "int" } } };
ArrayList<Object> pathAsList = new ArrayList<Object>();
for (Object[] row : testPathInput) {
DataElement element = (DataElement) row[0];
// test value
Object expectedValue = row[1];
assertSame(expectedValue, element.getValue());
// test schema
DataSchema expectedSchema = (DataSchema) row[2];
assertSame(expectedSchema, element.getSchema());
// test name
Object[] expectedPath = (Object[]) row[3];
Object expectedName = expectedPath.length == 0 ? DataElement.ROOT_NAME : expectedPath[expectedPath.length - 1];
assertEquals(expectedName, element.getName());
// test path
Object[] path = element.path();
element.pathAsList(pathAsList);
StringBuilder builder = new StringBuilder();
StringBuilder builder2 = new StringBuilder();
assertEquals(expectedPath.length, path.length);
assertEquals(expectedPath.length, pathAsList.size());
for (int i = 0; i < expectedPath.length; i++) {
assertEquals(path[i], expectedPath[i]);
assertEquals(pathAsList.get(i), expectedPath[i]);
builder.append('*').append(expectedPath[i]);
builder2.append(DataElement.SEPARATOR).append(expectedPath[i]);
}
assertEquals(builder.toString(), element.pathAsString('*'));
assertEquals(builder2.toString(), element.pathAsString());
// test copyChain
DataElement copy = element.copyChain();
assertElementChainEquals(copy, element.copyChain(), null);
// test DataElementUtil.element
DataElement elementFromUtil = DataElementUtil.element(root, path);
assertElementChainEquals(elementFromUtil, element, root);
elementFromUtil = DataElementUtil.element(root, pathAsList);
assertElementChainEquals(elementFromUtil, element, root);
elementFromUtil = DataElementUtil.element(root, element.pathAsString());
assertElementChainEquals(elementFromUtil, element, root);
elementFromUtil = DataElementUtil.element(root, element.pathAsString('*'), '*');
assertElementChainEquals(elementFromUtil, element, root);
elementFromUtil = DataElementUtil.element(root.getValue(), root.getSchema(), path);
assertElementChainEquals(elementFromUtil, element, root);
elementFromUtil = DataElementUtil.element(root.getValue(), root.getSchema(), pathAsList);
assertElementChainEquals(elementFromUtil, element, root);
elementFromUtil = DataElementUtil.element(root.getValue(), root.getSchema(), element.pathAsString());
assertElementChainEquals(elementFromUtil, element, root);
elementFromUtil = DataElementUtil.element(root.getValue(), root.getSchema(), element.pathAsString('*'), '*');
assertElementChainEquals(elementFromUtil, element, root);
}
}
Aggregations