Search in sources :

Example 26 with RecordDataSchema

use of com.linkedin.data.schema.RecordDataSchema in project rest.li by linkedin.

the class SnapshotGenerator method recordType.

private void recordType(DataSchema schema, Map<String, NamedDataSchema> foundTypes, List<NamedDataSchema> typeOrder) {
    if (schema instanceof NamedDataSchema) {
        NamedDataSchema namedDataSchema = (NamedDataSchema) schema;
        if (!foundTypes.containsKey(namedDataSchema.getFullName())) {
            foundTypes.put(namedDataSchema.getFullName(), namedDataSchema);
            if (// recurse into record, record any contained types.
            schema instanceof RecordDataSchema) {
                RecordDataSchema recordDataSchema = (RecordDataSchema) schema;
                for (NamedDataSchema includedSchema : recordDataSchema.getInclude()) {
                    recordType(includedSchema, foundTypes, typeOrder);
                }
                for (RecordDataSchema.Field field : recordDataSchema.getFields()) {
                    recordType(field.getType(), foundTypes, typeOrder);
                }
            } else if (schema instanceof TyperefDataSchema) {
                recordType(schema.getDereferencedDataSchema(), foundTypes, typeOrder);
            }
            typeOrder.add(namedDataSchema);
        }
    } else if (schema instanceof ArrayDataSchema) {
        ArrayDataSchema arraySchema = (ArrayDataSchema) schema;
        recordType(arraySchema.getItems(), foundTypes, typeOrder);
    } else if (schema instanceof MapDataSchema) {
        MapDataSchema mapSchema = (MapDataSchema) schema;
        recordType(mapSchema.getValues(), foundTypes, typeOrder);
    } else if (schema instanceof UnionDataSchema) {
        UnionDataSchema unionSchema = (UnionDataSchema) schema;
        for (DataSchema type : unionSchema.getTypes()) {
            recordType(type, foundTypes, typeOrder);
        }
    }
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema)

Example 27 with RecordDataSchema

use of com.linkedin.data.schema.RecordDataSchema in project rest.li by linkedin.

the class TestMockActionResponseFactory method testInference.

@Test
public void testInference() {
    final RecordTemplateWithDefaultValue record = new RecordTemplateWithDefaultValue();
    record.setId(42L);
    record.setMessage("Lorem ipsum");
    final ActionResponse<RecordTemplateWithDefaultValue> response = MockActionResponseFactory.create(RecordTemplateWithDefaultValue.class, record);
    Assert.assertEquals(response.getValue(), record);
    final RecordDataSchema schema = response.schema();
    Assert.assertEquals(schema.getName(), ActionResponse.class.getSimpleName());
    Assert.assertEquals(schema.getField(ActionResponse.VALUE_NAME).getType(), DataTemplateUtil.getSchema(RecordTemplateWithDefaultValue.class));
}
Also used : RecordTemplateWithDefaultValue(com.linkedin.restli.test.RecordTemplateWithDefaultValue) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ActionResponse(com.linkedin.restli.common.ActionResponse) Test(org.testng.annotations.Test)

Example 28 with RecordDataSchema

use of com.linkedin.data.schema.RecordDataSchema 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);
    model.addResourceMethodDescriptor(ResourceMethodDescriptor.createForAction(method, parameters, actionName, getActionResourceLevel(actionAnno, model), returnFieldDef, actionReturnRecordDataSchema, recordDataSchema, getInterfaceType(method), annotationsMap));
}
Also used : FieldDef(com.linkedin.data.template.FieldDef) Action(com.linkedin.restli.server.annotations.Action) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException) DataMap(com.linkedin.data.DataMap)

Example 29 with RecordDataSchema

use of com.linkedin.data.schema.RecordDataSchema in project rest.li by linkedin.

the class TestRestLiResponseHandler method buildRoutingResultAction.

/**
   * Creates a RoutingResult for an Action with the given returnType.
   *
   * @param actionReturnType the return type of the action.
   * @return a RoutingResult
   */
private final RoutingResult buildRoutingResultAction(Class<?> actionReturnType, RestRequest request, Map<String, String> headers) throws NoSuchMethodException, RestLiSyntaxException, URISyntaxException {
    if (actionReturnType == Void.class) {
        actionReturnType = Void.TYPE;
    }
    // actual method passed in is irrelevant, since we are constructing a ResourceMethodDescriptor by hand.
    Method method = ProjectionTestFixture.class.getMethod("batchGet", Set.class);
    ResourceModel model = RestLiTestHelper.buildResourceModel(StatusCollectionResource.class);
    String actionName = "return" + actionReturnType.getSimpleName();
    List<Parameter<?>> parameters = Collections.<Parameter<?>>emptyList();
    RecordDataSchema actionReturnRecordDataSchema;
    FieldDef<?> returnFieldDef;
    if (actionReturnType != Void.TYPE) {
        @SuppressWarnings({ "unchecked", "rawtypes" }) FieldDef<?> nonVoidFieldDef = new FieldDef(ActionResponse.VALUE_NAME, actionReturnType, DataTemplateUtil.getSchema(actionReturnType));
        returnFieldDef = nonVoidFieldDef;
        actionReturnRecordDataSchema = DynamicRecordMetadata.buildSchema(actionName, Collections.singleton(returnFieldDef));
    } else {
        returnFieldDef = null;
        actionReturnRecordDataSchema = DynamicRecordMetadata.buildSchema(actionName, Collections.<FieldDef<?>>emptyList());
    }
    ResourceMethodDescriptor methodDescriptor = ResourceMethodDescriptor.createForAction(method, parameters, actionName, ResourceLevel.COLLECTION, returnFieldDef, actionReturnRecordDataSchema, DynamicRecordMetadata.buildSchema(actionName, parameters), InterfaceType.SYNC, new DataMap());
    model.addResourceMethodDescriptor(methodDescriptor);
    ServerResourceContext resourceContext = new ResourceContextImpl(new PathKeysImpl(), request, new RequestContext());
    RestUtils.validateRequestHeadersAndUpdateResourceContext(headers, resourceContext);
    return new RoutingResult(resourceContext, methodDescriptor);
}
Also used : PathKeysImpl(com.linkedin.restli.internal.server.PathKeysImpl) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ResourceMethod(com.linkedin.restli.common.ResourceMethod) Method(java.lang.reflect.Method) ByteString(com.linkedin.data.ByteString) DataMap(com.linkedin.data.DataMap) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) FieldDef(com.linkedin.data.template.FieldDef) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RestLiTestHelper.buildResourceModel(com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel) Parameter(com.linkedin.restli.internal.server.model.Parameter) RequestContext(com.linkedin.r2.message.RequestContext) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl)

Example 30 with RecordDataSchema

use of com.linkedin.data.schema.RecordDataSchema in project rest.li by linkedin.

the class CompatibilityChecker method check.

private void check(DataSchema older, DataSchema newer) {
    Checked toCheck = new Checked(older, newer);
    if (_checked.contains(toCheck)) {
        return;
    }
    _checked.add(toCheck);
    if (older == newer) {
        return;
    }
    int pathCount = 1;
    if (_options.getMode() == CompatibilityOptions.Mode.DATA) {
        older = older.getDereferencedDataSchema();
        while (newer.getType() == DataSchema.Type.TYPEREF) {
            TyperefDataSchema typerefDataSchema = ((TyperefDataSchema) newer);
            _path.addLast(typerefDataSchema.getFullName());
            _path.addLast(DataSchemaConstants.REF_KEY);
            pathCount++;
            newer = typerefDataSchema.getRef();
        }
    }
    if (newer.getType() == DataSchema.Type.TYPEREF) {
        _path.addLast(((TyperefDataSchema) newer).getFullName());
    } else {
        _path.addLast(newer.getUnionMemberKey());
    }
    switch(newer.getType()) {
        case TYPEREF:
            if (isSameType(older, newer))
                checkTyperef((TyperefDataSchema) older, (TyperefDataSchema) newer);
            break;
        case RECORD:
            if (isSameType(older, newer))
                checkRecord((RecordDataSchema) older, (RecordDataSchema) newer);
            break;
        case ARRAY:
            if (isSameType(older, newer))
                checkArray((ArrayDataSchema) older, (ArrayDataSchema) newer);
            break;
        case MAP:
            if (isSameType(older, newer))
                checkMap((MapDataSchema) older, (MapDataSchema) newer);
            break;
        case ENUM:
            if (isSameType(older, newer))
                checkEnum((EnumDataSchema) older, (EnumDataSchema) newer);
            break;
        case FIXED:
            if (isSameType(older, newer))
                checkFixed((FixedDataSchema) older, (FixedDataSchema) newer);
            break;
        case UNION:
            if (isSameType(older, newer))
                checkUnion((UnionDataSchema) older, (UnionDataSchema) newer);
            break;
        default:
            if (newer instanceof PrimitiveDataSchema)
                checkPrimitive(older, newer);
            else
                throw new IllegalStateException("Unknown schema type " + newer.getType() + ", checking old schema " + older + ", new schema " + newer);
            break;
    }
    for (; pathCount > 0; pathCount--) {
        _path.removeLast();
    }
    return;
}
Also used : ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) PrimitiveDataSchema(com.linkedin.data.schema.PrimitiveDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) FixedDataSchema(com.linkedin.data.schema.FixedDataSchema)

Aggregations

RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)63 DataMap (com.linkedin.data.DataMap)26 DataSchema (com.linkedin.data.schema.DataSchema)25 Test (org.testng.annotations.Test)24 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)15 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)14 MapDataSchema (com.linkedin.data.schema.MapDataSchema)12 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)12 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)10 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)8 Name (com.linkedin.data.schema.Name)8 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)7 ArrayList (java.util.ArrayList)7 FieldDef (com.linkedin.data.template.FieldDef)6 Schema (org.apache.avro.Schema)6 DataList (com.linkedin.data.DataList)5 ActionResponse (com.linkedin.restli.common.ActionResponse)5 GenericRecord (org.apache.avro.generic.GenericRecord)5 SchemaParser (com.linkedin.data.schema.SchemaParser)4 TyperefTest (com.linkedin.pegasus.generator.test.TyperefTest)4