Search in sources :

Example 16 with TyperefDataSchema

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());
    }
}
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 17 with TyperefDataSchema

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));
}
Also used : TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) HasTyperefInfo(com.linkedin.data.template.HasTyperefInfo) TyperefInfo(com.linkedin.data.template.TyperefInfo) HasTyperefInfo(com.linkedin.data.template.HasTyperefInfo) Test(org.testng.annotations.Test)

Example 18 with TyperefDataSchema

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;
}
Also used : TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException)

Example 19 with TyperefDataSchema

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;
}
Also used : TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) TemplateRuntimeException(com.linkedin.data.template.TemplateRuntimeException)

Example 20 with TyperefDataSchema

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);
}
Also used : Action(com.linkedin.restli.server.annotations.Action) DataMap(com.linkedin.data.DataMap) FieldDef(com.linkedin.data.template.FieldDef) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException)

Aggregations

TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)35 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)26 DataSchema (com.linkedin.data.schema.DataSchema)23 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)21 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)18 MapDataSchema (com.linkedin.data.schema.MapDataSchema)14 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)9 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)9 DataMap (com.linkedin.data.DataMap)8 Test (org.testng.annotations.Test)8 PrimitiveDataSchema (com.linkedin.data.schema.PrimitiveDataSchema)7 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)6 Name (com.linkedin.data.schema.Name)6 ArrayList (java.util.ArrayList)4 ComplexDataSchema (com.linkedin.data.schema.ComplexDataSchema)3 StringDataSchema (com.linkedin.data.schema.StringDataSchema)3 ResourceConfigException (com.linkedin.restli.server.ResourceConfigException)3 DataList (com.linkedin.data.DataList)2 JsonBuilder (com.linkedin.data.schema.JsonBuilder)2 PathSpec (com.linkedin.data.schema.PathSpec)2