Search in sources :

Example 81 with DataSchema

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

the class TestValidation method testTrimUnrecognizedFieldsWithAvroUnionDefault.

@Test
public void testTrimUnrecognizedFieldsWithAvroUnionDefault() throws IOException {
    ValidationOptions options = new ValidationOptions(RequiredMode.CAN_BE_ABSENT_IF_HAS_DEFAULT, CoercionMode.NORMAL, UnrecognizedFieldMode.TRIM);
    options.setAvroUnionMode(true);
    String schemaText = "{\n" + "  \"name\" : \"Foo\",\n" + "  \"type\" : \"record\",\n" + "  \"fields\" : [\n" + "    { \"name\" : \"primitive\", \"type\" : \"int\", \"optional\" : true },\n" + "    { \"name\" : \"unionField\", \"type\" : [ \"Foo\", \"int\", \"string\" ], \"optional\" : true }\n" + "  ]\n" + "}\n";
    DataSchema schema = dataSchemaFromString(schemaText);
    String dataString = "{\n" + "  \"unionField\" : { \"primitive\": 4, \"unrecognizedInMap\": -4 }\n" + "}";
    String trimmedDataString = "{\n" + "  \"unionField\" : { \"primitive\": 4 }\n" + "}";
    DataMap toValidate = dataMapFromString(dataString);
    ValidationResult result = validate(toValidate, schema, options);
    Assert.assertTrue(result.isValid(), MessageUtil.messagesToString(result.getMessages()));
    Assert.assertEquals(result.getFixed(), dataMapFromString(trimmedDataString));
    Assert.assertSame(toValidate, result.getFixed());
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) 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) Test(org.testng.annotations.Test)

Example 82 with DataSchema

use of com.linkedin.data.schema.DataSchema 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 83 with DataSchema

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

the class RestLiDataValidator method validateOutputEntity.

private ValidationResult validateOutputEntity(RecordTemplate entity, MaskTree projectionMask) {
    try {
        // Value class from resource model is the only source of truth for record schema.
        // Schema from the record template itself should not be used.
        DataSchema originalSchema = DataTemplateUtil.getSchema(_valueClass);
        // When a projection is defined, we only need to validate the projected fields.
        DataSchema validatingSchema = (projectionMask != null) ? buildSchemaByProjection(originalSchema, projectionMask.getDataMap()) : originalSchema;
        DataSchemaAnnotationValidator validator = new DataSchemaAnnotationValidator(validatingSchema);
        return ValidateDataAgainstSchema.validate(entity.data(), validatingSchema, new ValidationOptions(), validator);
    } catch (TemplateRuntimeException e) {
        return validationResultWithErrorMessage(TEMPLATE_RUNTIME_ERROR);
    }
}
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) TemplateRuntimeException(com.linkedin.data.template.TemplateRuntimeException) DataSchemaAnnotationValidator(com.linkedin.data.schema.validator.DataSchemaAnnotationValidator) ValidationOptions(com.linkedin.data.schema.validation.ValidationOptions)

Example 84 with DataSchema

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

the class RestLiDataValidator method buildTyperefDataSchemaByProjection.

/**
   * Build a new {@link TyperefDataSchema} schema that contains only the masked fields.
   */
private static TyperefDataSchema buildTyperefDataSchemaByProjection(TyperefDataSchema originalSchema, DataMap maskMap) {
    TyperefDataSchema newSchema = new TyperefDataSchema(new Name(originalSchema.getFullName()));
    if (originalSchema.getProperties() != null) {
        newSchema.setProperties(originalSchema.getProperties());
    }
    if (originalSchema.getDoc() != null) {
        newSchema.setDoc(originalSchema.getDoc());
    }
    if (originalSchema.getAliases() != null) {
        newSchema.setAliases(originalSchema.getAliases());
    }
    DataSchema newRefSchema = buildSchemaByProjection(originalSchema.getRef(), maskMap);
    newSchema.setReferencedType(newRefSchema);
    return newSchema;
}
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) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) Name(com.linkedin.data.schema.Name)

Example 85 with DataSchema

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

the class RestLiDataValidator method buildUnionDataSchemaByProjection.

/**
   * Build a new {@link UnionDataSchema} schema that contains only the masked fields.
   */
private static UnionDataSchema buildUnionDataSchemaByProjection(UnionDataSchema originalSchema, DataMap maskMap) {
    List<DataSchema> newUnionTypeSchemas = new ArrayList<>();
    for (Map.Entry<String, Object> maskEntry : maskMap.entrySet()) {
        DataSchema originalTypeSchema = originalSchema.getType(maskEntry.getKey());
        DataSchema typeSchemaToUse = reuseOrBuildDataSchema(originalTypeSchema, maskEntry.getValue());
        newUnionTypeSchemas.add(typeSchemaToUse);
    }
    UnionDataSchema newSchema = new UnionDataSchema();
    // No errors are expected here, as the new schema is merely subset of the orignal
    newSchema.setTypes(newUnionTypeSchemas, new StringBuilder());
    if (originalSchema.getProperties() != null) {
        newSchema.setProperties(originalSchema.getProperties());
    }
    return newSchema;
}
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) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) DataMap(com.linkedin.data.DataMap)

Aggregations

DataSchema (com.linkedin.data.schema.DataSchema)131 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)82 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)53 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)48 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)44 DataMap (com.linkedin.data.DataMap)43 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)41 MapDataSchema (com.linkedin.data.schema.MapDataSchema)40 Test (org.testng.annotations.Test)37 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)36 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)24 ByteString (com.linkedin.data.ByteString)15 TestUtil.dataMapFromString (com.linkedin.data.TestUtil.dataMapFromString)15 TestUtil.dataSchemaFromString (com.linkedin.data.TestUtil.dataSchemaFromString)14 PrimitiveDataSchema (com.linkedin.data.schema.PrimitiveDataSchema)14 ArrayList (java.util.ArrayList)12 DataList (com.linkedin.data.DataList)11 ComplexDataSchema (com.linkedin.data.schema.ComplexDataSchema)9 SchemaParser (com.linkedin.data.schema.SchemaParser)9 ValidationOptions (com.linkedin.data.schema.validation.ValidationOptions)9