use of com.linkedin.data.schema.TyperefDataSchema 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.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(), 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 checkParameterHasTyperefSchema.
private static boolean checkParameterHasTyperefSchema(Parameter<?> parameter) {
boolean result = false;
DataSchema dataSchema = parameter.getDataSchema();
Class<?> dataType = parameter.getType();
if (dataType.isArray()) {
if (dataSchema instanceof ArrayDataSchema) {
dataSchema = ((ArrayDataSchema) dataSchema).getItems();
} else {
throw new ResourceConfigException("Array typed parameter " + parameter.getName() + " must have an array schema.");
}
}
if (dataSchema instanceof TyperefDataSchema) {
result = true;
}
return result;
}
use of com.linkedin.data.schema.TyperefDataSchema in project rest.li by linkedin.
the class RestLiAnnotationReader method checkTyperefSchema.
private static String checkTyperefSchema(final Class<?> type, final DataSchema dataSchema) {
if (type.isArray() && dataSchema instanceof ArrayDataSchema) {
registerCoercerForPrimitiveTypeRefArray((ArrayDataSchema) dataSchema);
}
if (!(dataSchema instanceof TyperefDataSchema)) {
return null;
}
TyperefDataSchema typerefSchema = (TyperefDataSchema) dataSchema;
boolean ok;
DataSchema.Type schemaType = typerefSchema.getDereferencedType();
Class<?>[] validTypes = RestModelConstants.PRIMITIVE_DATA_SCHEMA_TYPE_ALLOWED_TYPES.get(schemaType);
if (validTypes != null) {
String javaClassNameFromSchema = CustomTypeUtil.getJavaCustomTypeClassNameFromSchema(typerefSchema);
if (javaClassNameFromSchema != null) {
registerCoercer(typerefSchema);
ok = type.getName().equals(javaClassNameFromSchema) || (type.isArray() && (type.getComponentType().getName()).equals(javaClassNameFromSchema));
} else {
ok = checkParameterType(type, validTypes);
}
} else {
try {
DataSchema inferredSchema = DataTemplateUtil.getSchema(type);
DataSchema derefSchema = typerefSchema.getDereferencedDataSchema();
if (inferredSchema.equals(derefSchema)) {
return null;
}
return "typeref " + typerefSchema + " is not compatible with (" + type + ") with schema " + derefSchema;
} catch (TemplateRuntimeException e) {
}
ok = false;
}
if (!ok) {
return "typeref " + typerefSchema + " is not compatible with (" + type + ")";
}
return null;
}
use of com.linkedin.data.schema.TyperefDataSchema in project rest.li by linkedin.
the class RestLiAnnotationReader method addActionResourceMethod.
/**
* Add the given action method to the given resource model, validating the method is a action before adding.
* @param model provides the model to add the method to.
* @param method provides the method to add to the model.
* @throws ResourceConfigException on validation errors.
*/
private static void addActionResourceMethod(final ResourceModel model, final Method method) {
Action actionAnno = method.getAnnotation(Action.class);
if (actionAnno == null) {
return;
}
String actionName = actionAnno.name();
List<Parameter<?>> parameters = getParameters(model, method, ResourceMethod.ACTION);
Class<?> returnClass = getActionReturnClass(model, method, actionAnno, actionName);
TyperefDataSchema returnTyperefSchema = getActionTyperefDataSchema(model, actionAnno, actionName);
validateActionReturnType(model, method, returnClass, returnTyperefSchema);
if (!Modifier.isPublic(method.getModifiers())) {
throw new ResourceConfigException(String.format("Resource '%s' contains non-public action method '%s'.", model.getName(), method.getName()));
}
RecordDataSchema recordDataSchema = DynamicRecordMetadata.buildSchema(method.getName(), parameters);
RecordDataSchema actionReturnRecordDataSchema;
FieldDef<?> returnFieldDef;
if (returnClass != Void.TYPE) {
@SuppressWarnings({ "unchecked", "rawtypes" }) FieldDef<?> nonVoidFieldDef = new FieldDef(ActionResponse.VALUE_NAME, returnClass, getDataSchema(returnClass, returnTyperefSchema));
returnFieldDef = nonVoidFieldDef;
actionReturnRecordDataSchema = DynamicRecordMetadata.buildSchema(ActionResponse.class.getName(), Collections.singleton((returnFieldDef)));
} else {
returnFieldDef = null;
actionReturnRecordDataSchema = DynamicRecordMetadata.buildSchema(ActionResponse.class.getName(), Collections.<FieldDef<?>>emptyList());
}
if (model.getResourceLevel() == ResourceLevel.ENTITY && actionAnno.resourceLevel() == ResourceLevel.COLLECTION) {
throw new ResourceConfigException(String.format("Resource '%s' is a simple resource, it cannot contain actions at resource level \"COLLECTION\".", model.getName()));
}
DataMap annotationsMap = ResourceModelAnnotation.getAnnotationsMap(method.getAnnotations());
addDeprecatedAnnotation(annotationsMap, method);
ResourceMethodDescriptor resourceMethodDescriptor = ResourceMethodDescriptor.createForAction(method, parameters, actionName, getActionResourceLevel(actionAnno, model), returnFieldDef, actionReturnRecordDataSchema, actionAnno.readOnly(), recordDataSchema, getInterfaceType(method), annotationsMap);
addServiceErrors(resourceMethodDescriptor, method);
addSuccessStatuses(resourceMethodDescriptor, method);
model.addResourceMethodDescriptor(resourceMethodDescriptor);
}
Aggregations