use of com.linkedin.data.schema.TyperefDataSchema in project rest.li by linkedin.
the class JavaRequestBuilderGenerator method getJavaBindingType.
private JavaBinding getJavaBindingType(DataSchema schema, JDefinedClass enclosingClass) {
final JavaBinding binding = new JavaBinding();
if (_generateDataTemplates || schema instanceof ArrayDataSchema) {
final ClassTemplateSpec classSpec = generateClassSpec(schema, enclosingClass);
binding.schemaClass = _javaDataTemplateGenerator.generate(classSpec);
{
_generatedArrayClasses.add(binding.schemaClass);
}
}
binding.schemaClass = getClassRefForSchema(schema, enclosingClass);
binding.valueClass = getClassRefForSchema(schema, enclosingClass);
if (schema instanceof TyperefDataSchema) {
final TyperefDataSchema typerefDataSchema = (TyperefDataSchema) schema;
if (typerefDataSchema.getDereferencedDataSchema().getType() != DataSchema.Type.UNION) {
final String javaClassNameFromSchema = CustomTypeUtil.getJavaCustomTypeClassNameFromSchema(typerefDataSchema);
if (javaClassNameFromSchema != null) {
binding.valueClass = getCodeModel().directClass(javaClassNameFromSchema);
} else {
binding.valueClass = getJavaBindingType(typerefDataSchema.getRef(), enclosingClass).valueClass;
}
}
}
return binding;
}
use of com.linkedin.data.schema.TyperefDataSchema in project rest.li by linkedin.
the class TestCopySchemaUtil method testBuildSkeletonSchema.
@Test
public void testBuildSkeletonSchema() throws Exception {
DataSchema oldSchema = null;
RecordDataSchema fooSchema = (RecordDataSchema) TestUtil.dataSchemaFromString(fooSchemaText);
// Test Record
RecordDataSchema newRecordSchema = (RecordDataSchema) CopySchemaUtil.buildSkeletonSchema(fooSchema);
assert ((newRecordSchema.getFields().size() == 0) && Objects.equals(newRecordSchema.getDoc(), fooSchema.getDoc()) && Objects.equals(newRecordSchema.getProperties(), fooSchema.getProperties()) && Objects.equals(newRecordSchema.getAliases(), fooSchema.getAliases()));
// Test TypeRef
oldSchema = fooSchema.getField("typeRefField").getType();
TyperefDataSchema newTypeRefDataSchema = (TyperefDataSchema) CopySchemaUtil.buildSkeletonSchema(oldSchema);
assert (Objects.equals(newTypeRefDataSchema.getDoc(), ((TyperefDataSchema) oldSchema).getDoc()) && Objects.equals(newTypeRefDataSchema.getProperties(), oldSchema.getProperties()) && Objects.equals(newTypeRefDataSchema.getAliases(), ((TyperefDataSchema) oldSchema).getAliases()));
// Test Union
oldSchema = fooSchema.getField("unionField").getType();
UnionDataSchema newUnionDataSchema = (UnionDataSchema) CopySchemaUtil.buildSkeletonSchema(oldSchema);
assert (newUnionDataSchema.getMembers().size() == 0 && Objects.equals(newUnionDataSchema.getProperties(), oldSchema.getProperties()));
// Test map
oldSchema = fooSchema.getField("mapField").getType();
MapDataSchema mapDataSchema = (MapDataSchema) CopySchemaUtil.buildSkeletonSchema(oldSchema);
assert (Objects.equals(mapDataSchema.getProperties(), oldSchema.getProperties()) && Objects.equals(mapDataSchema.getValues(), DataSchemaConstants.NULL_DATA_SCHEMA));
// Test array
oldSchema = fooSchema.getField("arrayField").getType();
ArrayDataSchema arrayDataSchema = (ArrayDataSchema) CopySchemaUtil.buildSkeletonSchema(oldSchema);
assert (Objects.equals(arrayDataSchema.getProperties(), oldSchema.getProperties()) && Objects.equals(arrayDataSchema.getItems(), DataSchemaConstants.NULL_DATA_SCHEMA));
// Test ENUM
oldSchema = fooSchema.getField("enumField").getType();
EnumDataSchema enumDataSchema = (EnumDataSchema) CopySchemaUtil.buildSkeletonSchema(oldSchema);
Assert.assertEquals(enumDataSchema, oldSchema);
// Test FIXED
oldSchema = fooSchema.getField("fixedField").getType();
FixedDataSchema fixedDataSchema = (FixedDataSchema) CopySchemaUtil.buildSkeletonSchema(oldSchema);
Assert.assertEquals(fixedDataSchema, oldSchema);
// Test primitive
oldSchema = fooSchema.getField("intField").getType();
PrimitiveDataSchema primitiveDataSchema = (PrimitiveDataSchema) CopySchemaUtil.buildSkeletonSchema(oldSchema);
Assert.assertEquals(primitiveDataSchema, oldSchema);
}
use of com.linkedin.data.schema.TyperefDataSchema in project rest.li by linkedin.
the class SchemaToAvroJsonEncoder method produceFieldProperties.
@SuppressWarnings("unchecked")
static /* package private */
Map<String, ?> produceFieldProperties(RecordDataSchema.Field field, DataToAvroSchemaTranslationOptions options) {
Stream<Map.Entry<String, Object>> toBeFiltered = field.getProperties().entrySet().stream();
// and merge with record field's properties.
if (field.getType().getType() == DataSchema.Type.TYPEREF) {
toBeFiltered = Stream.concat(toBeFiltered, ((TyperefDataSchema) field.getType()).getMergedTyperefProperties().entrySet().stream()).filter(entry -> !options.getTyperefPropertiesExcludeSet().contains(entry.getKey()));
}
// Property merge rule:
// For property content inherited from TypeRef that appears to be have same property name as the record field:
// if the two property contents are Map type, they will be merged at this level,
// otherwise Typeref field property content will be overridden by record field property's content.
BinaryOperator<Object> propertyMergeLogic = (originalPropertyContent, inheritedPropertyContent) -> {
if (originalPropertyContent instanceof Map && inheritedPropertyContent instanceof Map) {
Map<String, Object> mergedMap = new DataMap((Map<String, Object>) originalPropertyContent);
((Map<String, Object>) inheritedPropertyContent).forEach(mergedMap::putIfAbsent);
return mergedMap;
} else {
return originalPropertyContent;
}
};
return toBeFiltered.filter(entry -> !RESERVED_DATA_PROPERTIES.contains(entry.getKey())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, propertyMergeLogic));
}
use of com.linkedin.data.schema.TyperefDataSchema in project rest.li by linkedin.
the class TestTyperefUnion method testTyperefUnion.
@Test
public void testTyperefUnion() {
TyperefInfo typerefInfo = DataTemplateUtil.getTyperefInfo(Union.class);
assertNotNull(typerefInfo);
TyperefDataSchema typerefDataSchema = typerefInfo.getSchema();
Union union = new Union();
assertTrue(union instanceof HasTyperefInfo);
TyperefInfo typerefInfoFromInstance = union.typerefInfo();
assertNotNull(typerefInfoFromInstance);
TyperefDataSchema typerefDataSchemaFromInstance = typerefInfo.getSchema();
assertSame(typerefDataSchemaFromInstance, typerefDataSchema);
assertSame(typerefInfoFromInstance, typerefInfo);
assertEquals(typerefDataSchema.getFullName(), "com.linkedin.pegasus.generator.testpackage.Union");
assertEquals(typerefDataSchema.getBindingName(), Union.class.getName());
assertEquals(typerefDataSchema.getRef(), DataTemplateUtil.getSchema(Union.class));
}
use of com.linkedin.data.schema.TyperefDataSchema in project rest.li by linkedin.
the class RestLiAnnotationReader method getActionTyperefDataSchema.
private static TyperefDataSchema getActionTyperefDataSchema(ResourceModel model, Action actionAnno, String actionName) {
TyperefDataSchema returnTyperefSchema = null;
Class<? extends TyperefInfo> typerefInfoClass = actionAnno.returnTyperef();
try {
returnTyperefSchema = getSchemaFromTyperefInfo(typerefInfoClass);
} catch (Exception e) {
throw new ResourceConfigException("Typeref @Action method named '" + actionName + "' on class '" + model.getResourceClass().getName() + "' cannot be instantiated, " + e.getMessage());
}
return returnTyperefSchema;
}
Aggregations