Search in sources :

Example 1 with ExtensionSchemaAnnotation

use of com.linkedin.restli.common.ExtensionSchemaAnnotation in project rest.li by linkedin.

the class ExtensionSchemaValidationCmdLineApp method checkExtensionSchemaFields.

private static void checkExtensionSchemaFields(List<RecordDataSchema.Field> extensionSchemaFields) throws InvalidExtensionSchemaException {
    for (RecordDataSchema.Field field : extensionSchemaFields) {
        // check extension schema field annotation
        Map<String, Object> properties = field.getProperties();
        if (properties.isEmpty() || properties.keySet().size() != 1 || !properties.containsKey(EXTENSION_ANNOTATION_NAMESPACE)) {
            throw new InvalidExtensionSchemaException("The field : " + field.getName() + " of extension schema must and only be annotated with 'extension'");
        }
        Object dataElement = properties.get(EXTENSION_ANNOTATION_NAMESPACE);
        ValidationOptions validationOptions = new ValidationOptions(RequiredMode.MUST_BE_PRESENT, CoercionMode.STRING_TO_PRIMITIVE, UnrecognizedFieldMode.DISALLOW);
        try {
            if (!(dataElement instanceof DataMap)) {
                throw new InvalidExtensionSchemaException("Extension schema annotation is not a datamap!");
            }
            DataSchema extensionSchemaAnnotationSchema = new ExtensionSchemaAnnotation().schema();
            ValidationResult result = ValidateDataAgainstSchema.validate(dataElement, extensionSchemaAnnotationSchema, validationOptions);
            if (!result.isValid()) {
                throw new InvalidExtensionSchemaException("Extension schema annotation is not valid: " + result.getMessages());
            }
        } catch (InvalidExtensionSchemaException e) {
            throw e;
        } catch (Exception e) {
            _logger.error("Error while checking extension schema field annotation: " + e.getMessage());
            System.exit(1);
        }
        checkExtensionSchemaFieldSchema(field.getType(), properties);
    }
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ExtensionSchemaAnnotation(com.linkedin.restli.common.ExtensionSchemaAnnotation) ValidationOptions(com.linkedin.data.schema.validation.ValidationOptions) ValidationResult(com.linkedin.data.schema.validation.ValidationResult) IOException(java.io.IOException) ParseException(org.apache.commons.cli.ParseException) DataMap(com.linkedin.data.DataMap)

Aggregations

DataMap (com.linkedin.data.DataMap)1 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)1 DataSchema (com.linkedin.data.schema.DataSchema)1 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)1 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)1 ValidationOptions (com.linkedin.data.schema.validation.ValidationOptions)1 ValidationResult (com.linkedin.data.schema.validation.ValidationResult)1 ExtensionSchemaAnnotation (com.linkedin.restli.common.ExtensionSchemaAnnotation)1 IOException (java.io.IOException)1 ParseException (org.apache.commons.cli.ParseException)1