use of com.linkedin.data.schema.RecordDataSchema in project rest.li by linkedin.
the class TestValidation method testCoercionValidation.
public void testCoercionValidation(String schemaText, String key, Object[][] inputs, Object[] badObjects, CoercionMode coercionMode) throws IOException {
Assert.assertTrue(coercionMode != CoercionMode.OFF);
ValidationOptions options = normalCoercionValidationOption();
options.setCoercionMode(coercionMode);
RecordDataSchema schema = (RecordDataSchema) dataSchemaFromString(schemaText);
Assert.assertTrue(schema != null);
DataMap map = new DataMap();
for (Object[] row : inputs) {
map.put(key, row[0]);
ValidationResult result = validate(map, schema, options);
Assert.assertTrue(result.isValid());
if (result.hasFix()) {
DataMap fixedMap = (DataMap) result.getFixed();
Assert.assertSame(fixedMap.getClass(), DataMap.class);
Object fixed = fixedMap.get(key);
Assert.assertTrue(fixed != null);
Class<?> fixedClass = fixed.getClass();
Class<?> goodClass = row[0].getClass();
switch(schema.getField(key).getType().getDereferencedType()) {
case BYTES:
case FIXED:
// String to ByteString conversion check
Assert.assertNotSame(goodClass, fixedClass);
Assert.assertSame(goodClass, String.class);
Assert.assertSame(fixedClass, ByteString.class);
Assert.assertEquals(((ByteString) fixed).asAvroString(), row[0]);
break;
case INT:
// convert numbers to Integer
Assert.assertNotSame(goodClass, fixedClass);
assertAllowedClass(coercionMode, goodClass);
Assert.assertSame(fixedClass, Integer.class);
break;
case LONG:
// convert numbers to Long
Assert.assertNotSame(goodClass, fixedClass);
assertAllowedClass(coercionMode, goodClass);
Assert.assertSame(fixedClass, Long.class);
break;
case FLOAT:
// convert numbers to Float
Assert.assertNotSame(goodClass, fixedClass);
assertAllowedClass(coercionMode, goodClass);
Assert.assertSame(fixedClass, Float.class);
break;
case DOUBLE:
// convert numbers to Double
Assert.assertNotSame(goodClass, fixedClass);
assertAllowedClass(coercionMode, goodClass);
Assert.assertSame(fixedClass, Double.class);
break;
case BOOLEAN:
if (coercionMode == CoercionMode.STRING_TO_PRIMITIVE) {
Assert.assertNotSame(goodClass, fixedClass);
Assert.assertTrue(goodClass == String.class);
Assert.assertSame(fixedClass, Boolean.class);
}
break;
case RECORD:
case ARRAY:
case MAP:
case UNION:
Assert.assertSame(goodClass, fixedClass);
break;
default:
throw new IllegalStateException("unknown conversion");
}
Assert.assertEquals(fixed, row[1]);
} else {
Assert.assertSame(map, result.getFixed());
}
}
for (Object bad : badObjects) {
map.put(key, bad);
ValidationResult result = validate(map, schema, options);
Assert.assertFalse(result.isValid());
Assert.assertSame(map, result.getFixed());
}
}
use of com.linkedin.data.schema.RecordDataSchema in project rest.li by linkedin.
the class ActionRequestBuilder method build.
@SuppressWarnings("unchecked")
@Override
public ActionRequest<V> build() {
if (_name == null) {
throw new IllegalStateException("name required to build action request");
}
RecordDataSchema requestDataSchema;
RecordDataSchema actionResponseDataSchema;
FieldDef<V> responseFieldDef;
if (// old builder code in use
_resourceSpec.getRequestMetadata(_name) == null) {
requestDataSchema = DynamicRecordMetadata.buildSchema(_name, _actionParams.keySet());
Collection<FieldDef<?>> responseFieldDefCollection;
if (_elementType.getType() == Void.class) {
responseFieldDef = null;
responseFieldDefCollection = Collections.emptyList();
} else {
responseFieldDef = new FieldDef<V>(ActionResponse.VALUE_NAME, _elementType.getType(), _elementType.getSchema());
responseFieldDefCollection = Collections.<FieldDef<?>>singleton(responseFieldDef);
}
actionResponseDataSchema = DynamicRecordMetadata.buildSchema(_name, responseFieldDefCollection);
} else {
requestDataSchema = _resourceSpec.getRequestMetadata(_name).getRecordDataSchema();
actionResponseDataSchema = _resourceSpec.getActionResponseMetadata(_name).getRecordDataSchema();
responseFieldDef = (FieldDef<V>) _resourceSpec.getActionResponseMetadata(_name).getFieldDef(ActionResponse.VALUE_NAME);
}
@SuppressWarnings("unchecked") ActionResponseDecoder<V> actionResponseDecoder = new ActionResponseDecoder<V>(responseFieldDef, actionResponseDataSchema);
DynamicRecordTemplate inputParameters = new DynamicRecordTemplate(requestDataSchema, buildReadOnlyActionParameters());
inputParameters.data().setReadOnly();
return new ActionRequest<V>(inputParameters, buildReadOnlyHeaders(), buildReadOnlyCookies(), actionResponseDecoder, _resourceSpec, buildReadOnlyQueryParameters(), getQueryParamClasses(), _name, getBaseUriTemplate(), buildReadOnlyPathKeys(), getRequestOptions(), buildReadOnlyId(), _streamingAttachments == null ? null : Collections.unmodifiableList(_streamingAttachments));
}
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 BatchCreateIdEntityResponse method generateSchema.
private static RecordDataSchema generateSchema() {
StringBuilder errorMessageBuilder = new StringBuilder(10);
ArrayDataSchema arraySchema = new ArrayDataSchema(new RecordDataSchema(new Name(CreateStatus.class.getSimpleName()), RecordDataSchema.RecordType.RECORD));
RecordDataSchema.Field arrayField = new RecordDataSchema.Field(arraySchema);
arrayField.setName(CollectionResponse.ELEMENTS, errorMessageBuilder);
RecordDataSchema schema = new RecordDataSchema(new Name(BatchCreateIdEntityResponse.class.getSimpleName()), RecordDataSchema.RecordType.RECORD);
schema.setFields(Arrays.asList(arrayField), errorMessageBuilder);
return schema;
}
use of com.linkedin.data.schema.RecordDataSchema in project rest.li by linkedin.
the class BatchCreateIdResponse method generateSchema.
private static RecordDataSchema generateSchema() {
StringBuilder errorMessageBuilder = new StringBuilder(10);
ArrayDataSchema arraySchema = new ArrayDataSchema(new RecordDataSchema(new Name(CreateStatus.class.getSimpleName()), RecordDataSchema.RecordType.RECORD));
RecordDataSchema.Field arrayField = new RecordDataSchema.Field(arraySchema);
arrayField.setName(CollectionResponse.ELEMENTS, errorMessageBuilder);
RecordDataSchema schema = new RecordDataSchema(new Name(BatchCreateIdResponse.class.getSimpleName()), RecordDataSchema.RecordType.RECORD);
schema.setFields(Arrays.asList(arrayField), errorMessageBuilder);
return schema;
}
Aggregations