Search in sources :

Example 31 with RecordDataSchema

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());
    }
}
Also used : RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) TestUtil.dataMapFromString(com.linkedin.data.TestUtil.dataMapFromString) ByteString(com.linkedin.data.ByteString) TestUtil.dataSchemaFromString(com.linkedin.data.TestUtil.dataSchemaFromString) DataMap(com.linkedin.data.DataMap)

Example 32 with RecordDataSchema

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));
}
Also used : FieldDef(com.linkedin.data.template.FieldDef) ActionResponseDecoder(com.linkedin.restli.internal.client.ActionResponseDecoder) DynamicRecordTemplate(com.linkedin.data.template.DynamicRecordTemplate) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema)

Example 33 with RecordDataSchema

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;
}
Also used : UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) DataMap(com.linkedin.data.DataMap) Name(com.linkedin.data.schema.Name)

Example 34 with RecordDataSchema

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

Example 35 with RecordDataSchema

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

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