use of com.linkedin.data.schema.RecordDataSchema in project rest.li by linkedin.
the class BatchFinderResponseBuilder method buildElements.
private List<AnyRecord> buildElements(CollectionResult<RecordTemplate, RecordTemplate> cr, ResourceContextImpl resourceContext) {
List<? extends RecordTemplate> elements = cr.getElements();
List<AnyRecord> response = new ArrayList<>(elements.size());
for (int j = 0; j < elements.size(); j++) {
if (resourceContext.isFillInDefaultsRequested()) {
RecordDataSchema schema = elements.get(j).schema();
DataMap dataWithDefault = (DataMap) ResponseUtils.fillInDataDefault(schema, elements.get(j).data());
response.add(new AnyRecord(RestUtils.projectFields(dataWithDefault, resourceContext)));
} else {
response.add(new AnyRecord(RestUtils.projectFields(elements.get(j).data(), resourceContext)));
}
}
return response;
}
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 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, Collections.emptySet(), resourceContext);
return new RoutingResult(resourceContext, methodDescriptor);
}
use of com.linkedin.data.schema.RecordDataSchema in project rest.li by linkedin.
the class RestLiDataValidator method buildRecordDataSchemaByProjection.
/**
* Build a new {@link RecordDataSchema} schema that contains only the masked fields.
*/
private static RecordDataSchema buildRecordDataSchemaByProjection(RecordDataSchema originalSchema, DataMap maskMap) {
RecordDataSchema newRecordSchema = new RecordDataSchema(new Name(originalSchema.getFullName()), RecordDataSchema.RecordType.RECORD);
List<RecordDataSchema.Field> newFields = new ArrayList<RecordDataSchema.Field>();
for (Map.Entry<String, Object> maskEntry : maskMap.entrySet()) {
RecordDataSchema.Field originalField = originalSchema.getField(maskEntry.getKey());
DataSchema fieldSchemaToUse = reuseOrBuildDataSchema(originalField.getType(), maskEntry.getValue());
RecordDataSchema.Field newField = buildRecordField(originalField, fieldSchemaToUse, newRecordSchema);
newFields.add(newField);
}
// Fields from 'include' are no difference from other fields from original schema,
// therefore, we are not calling newRecordSchema.setInclude() here.
// No errors are expected here, as the new schema is merely subset of the original
newRecordSchema.setFields(newFields, new StringBuilder());
if (originalSchema.getAliases() != null) {
newRecordSchema.setAliases(originalSchema.getAliases());
}
if (originalSchema.getDoc() != null) {
newRecordSchema.setDoc(originalSchema.getDoc());
}
if (originalSchema.getProperties() != null) {
newRecordSchema.setProperties(originalSchema.getProperties());
}
return newRecordSchema;
}
use of com.linkedin.data.schema.RecordDataSchema in project rest.li by linkedin.
the class TestComplexResourceKey method testKeySchema.
@Test
public void testKeySchema() {
RecordDataSchema schema = OmniRecord.schema;
TypeSpec<OmniRecord> keyType = new TypeSpec<OmniRecord>(OmniRecord.class, schema);
TypeSpec<OmniRecord> paramsType = new TypeSpec<OmniRecord>(OmniRecord.class, schema);
ComplexKeySpec<OmniRecord, OmniRecord> keySpec = new ComplexKeySpec<OmniRecord, OmniRecord>(keyType, paramsType);
DataMap data = new DataMap();
data.put("int", 1);
ComplexResourceKey<RecordTemplate, RecordTemplate> key = ComplexResourceKey.buildFromDataMap(data, keySpec);
Assert.assertEquals(key.getKey().schema(), schema);
Assert.assertEquals(key.getParams().schema(), schema);
}
use of com.linkedin.data.schema.RecordDataSchema in project rest.li by linkedin.
the class ActionResponseBuilder method buildRestLiResponseData.
@Override
public RestLiResponseData buildRestLiResponseData(RestRequest request, RoutingResult routingResult, Object result, Map<String, String> headers, List<HttpCookie> cookies) {
final Object value;
final HttpStatus status;
if (result instanceof ActionResult) {
final ActionResult<?> actionResult = (ActionResult<?>) result;
value = actionResult.getValue();
status = actionResult.getStatus();
if (status == null) {
throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null HttpStatus inside of an ActionResult returned by the resource method: " + routingResult.getResourceMethod());
}
} else {
value = result;
status = HttpStatus.S_200_OK;
}
RecordDataSchema actionReturnRecordDataSchema = routingResult.getResourceMethod().getActionReturnRecordDataSchema();
@SuppressWarnings("unchecked") FieldDef<Object> actionReturnFieldDef = (FieldDef<Object>) routingResult.getResourceMethod().getActionReturnFieldDef();
final ActionResponse<?> actionResponse = new ActionResponse<Object>(value, actionReturnFieldDef, actionReturnRecordDataSchema);
RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(status, headers, cookies);
responseData.setResponseEnvelope(new ActionResponseEnvelope(actionResponse, responseData));
return responseData;
}
Aggregations