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;
}
use of com.linkedin.data.schema.RecordDataSchema in project rest.li by linkedin.
the class DataSchemaAnnotationValidator method buildSchemaValidators.
/**
* Build a cache of {@link Validator}s declared for the specified schema.
*
* @param schema to cache {@link Validator}s for.
* @return the cache if successful.
*/
private IdentityHashMap<Object, List<Validator>> buildSchemaValidators(DataSchema schema) {
final IdentityHashMap<Object, List<Validator>> map = new IdentityHashMap<Object, List<Validator>>();
DataSchemaTraverse traverse = new DataSchemaTraverse();
traverse.traverse(schema, new DataSchemaTraverse.Callback() {
@Override
public void callback(List<String> path, DataSchema schema) {
List<Validator> validatorList = map.get(schema);
if (validatorList == null) {
Object validateObject = schema.getProperties().get(VALIDATE);
if (validateObject == null) {
validatorList = NO_VALIDATORS;
} else {
validatorList = buildValidatorList(validateObject, path, schema);
}
map.put(schema, validatorList);
if (schema.getType() == DataSchema.Type.RECORD) {
RecordDataSchema recordDataSchema = (RecordDataSchema) schema;
for (RecordDataSchema.Field field : recordDataSchema.getFields()) {
validateObject = field.getProperties().get(VALIDATE);
if (validateObject == null) {
validatorList = NO_VALIDATORS;
} else {
path.add(field.getName());
validatorList = buildValidatorList(validateObject, path, field);
path.remove(path.size() - 1);
}
map.put(field, validatorList);
}
}
}
}
});
return map;
}
use of com.linkedin.data.schema.RecordDataSchema in project rest.li by linkedin.
the class DynamicRecordMetadata method buildSchema.
/**
* Build the schema of a {@link DynamicRecordTemplate}.
*
* @param name the name of the record.
* @param fieldDefs the fields of the record.
* @throws IllegalArgumentException if the {@link com.linkedin.data.schema.RecordDataSchema.Field} of the fieldDefs
* are already set.
*/
public static RecordDataSchema buildSchema(String name, Collection<? extends FieldDef<?>> fieldDefs) {
StringBuilder errorMessageBuilder = new StringBuilder();
RecordDataSchema schema = new RecordDataSchema(new Name(name, errorMessageBuilder), RecordDataSchema.RecordType.RECORD);
List<RecordDataSchema.Field> fields = new ArrayList<RecordDataSchema.Field>(fieldDefs.size());
for (FieldDef<?> fieldDef : fieldDefs) {
RecordDataSchema.Field paramField = fieldDef.getField();
if (paramField.getRecord() != null) {
throw new IllegalArgumentException("Attempt to assign field " + fieldDef.getName() + " to record " + schema.getName() + "failed: " + "Record of field is already set to " + paramField.getRecord().getName());
}
paramField.setRecord(schema);
fields.add(paramField);
}
schema.setFields(fields, errorMessageBuilder);
return schema;
}
use of com.linkedin.data.schema.RecordDataSchema in project rest.li by linkedin.
the class TestLongStringLiteral method testSchema.
@Test
public void testSchema() {
DataSchema schema = DataTemplateUtil.getSchema(LongStringLiteral.class);
String schemaText = schema.toString();
assertTrue(schemaText.length() > 65536);
RecordDataSchema recordDataSchema = (RecordDataSchema) schema;
RecordDataSchema.Field field = recordDataSchema.getField("text");
DataList defaultValue = (DataList) field.getDefault();
assertEquals(defaultValue.size(), 400);
for (Object s : defaultValue) {
assertEquals(s, LOREM);
}
}
Aggregations