Search in sources :

Example 11 with DataSchemaResolver

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

the class AnyRecordValidator method schemaFromName.

protected DataSchema schemaFromName(ValidatorContext context, String schemaName) {
    StringBuilder sb = new StringBuilder();
    Parameter parameter = getParameter(context.validationOptions());
    DataSchemaResolver resolver = parameter.resolver();
    NamedDataSchema schema;
    if (resolver == null) {
        schema = null;
        context.addResult(new Message(context.dataElement().path(schemaName), parameter.isValidSchema(), "%1$s cannot obtain schema for \"%2$s\", no resolver", AnyRecordValidator.class.getName(), schemaName));
    } else {
        schema = resolver.findDataSchema(schemaName, sb);
        if (schema == null) {
            context.addResult(new Message(context.dataElement().path(schemaName), parameter.isValidSchema(), "%1$s cannot obtain schema for \"%2$s\" (%3$s)", AnyRecordValidator.class.getName(), schemaName, sb.toString()));
        }
    }
    return schema;
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) Message(com.linkedin.data.message.Message) DataSchemaResolver(com.linkedin.data.schema.DataSchemaResolver)

Example 12 with DataSchemaResolver

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

the class RestLiResourceModelCompatibilityChecker method check.

/**
 * Check backwards compatibility between two idl (.restspec.json) files.
 *
 * @param prevRestspecPath previously existing idl file
 * @param currRestspecPath current idl file
 * @param compatLevel compatibility level which affects the return value
 * @return true if the check result conforms the compatibility level requirement
 *         e.g. false if backwards compatible changes are found but the level is equivalent
 */
public boolean check(String prevRestspecPath, String currRestspecPath, CompatibilityLevel compatLevel) {
    _prevRestspecPath = prevRestspecPath;
    _currRestspecPath = currRestspecPath;
    Stack<Object> path = new Stack<>();
    path.push("");
    ResourceSchema prevRec = null;
    ResourceSchema currRec = null;
    try {
        prevRec = _codec.readResourceSchema(new FileInputStream(prevRestspecPath));
    } catch (FileNotFoundException e) {
        _infoMap.addRestSpecInfo(CompatibilityInfo.Type.RESOURCE_NEW, path, currRestspecPath);
    } catch (IOException e) {
        _infoMap.addRestSpecInfo(CompatibilityInfo.Type.OTHER_ERROR, path, e.getMessage());
    }
    try {
        currRec = _codec.readResourceSchema(new FileInputStream(currRestspecPath));
    } catch (FileNotFoundException e) {
        _infoMap.addRestSpecInfo(CompatibilityInfo.Type.RESOURCE_MISSING, path, prevRestspecPath);
    } catch (Exception e) {
        _infoMap.addRestSpecInfo(CompatibilityInfo.Type.OTHER_ERROR, path, e.getMessage());
    }
    if (prevRec == null || currRec == null) {
        return _infoMap.isCompatible(compatLevel);
    }
    final DataSchemaResolver resolver;
    if (_resolverPath == null) {
        resolver = new DefaultDataSchemaResolver();
    } else {
        resolver = MultiFormatDataSchemaResolver.withBuiltinFormats(_resolverPath);
    }
    ResourceCompatibilityChecker checker = new ResourceCompatibilityChecker(prevRec, resolver, currRec, resolver);
    boolean check = checker.check(compatLevel);
    _infoMap.addAll(checker.getInfoMap());
    return check;
}
Also used : ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) DataSchemaResolver(com.linkedin.data.schema.DataSchemaResolver) MultiFormatDataSchemaResolver(com.linkedin.data.schema.resolver.MultiFormatDataSchemaResolver) DefaultDataSchemaResolver(com.linkedin.data.schema.resolver.DefaultDataSchemaResolver) FileNotFoundException(java.io.FileNotFoundException) DefaultDataSchemaResolver(com.linkedin.data.schema.resolver.DefaultDataSchemaResolver) ResourceCompatibilityChecker(com.linkedin.restli.tools.compatibility.ResourceCompatibilityChecker) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ParseException(org.apache.commons.cli.ParseException) Stack(java.util.Stack)

Example 13 with DataSchemaResolver

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

the class RestLiSnapshotCompatibilityChecker method createResolverFromSnapshot.

private static DataSchemaResolver createResolverFromSnapshot(AbstractSnapshot snapshot, String resolverPath) {
    final DataSchemaResolver resolver = CompatibilityUtil.getDataSchemaResolver(resolverPath);
    for (Map.Entry<String, NamedDataSchema> entry : snapshot.getModels().entrySet()) {
        Name name = new Name(entry.getKey());
        NamedDataSchema schema = entry.getValue();
        resolver.bindNameToSchema(name, schema, DataSchemaLocation.NO_LOCATION);
    }
    return resolver;
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) DataSchemaResolver(com.linkedin.data.schema.DataSchemaResolver) CompatibilityInfoMap(com.linkedin.restli.tools.compatibility.CompatibilityInfoMap) Map(java.util.Map) Name(com.linkedin.data.schema.Name)

Example 14 with DataSchemaResolver

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

the class ExtensionSchemaValidationCmdLineApp method parseAndValidateExtensionSchemas.

static void parseAndValidateExtensionSchemas(String resolverPath, File inputDir) throws IOException, InvalidExtensionSchemaException {
    // Parse each extension schema and validate it
    Iterator<File> iterator = FileUtils.iterateFiles(inputDir, new String[] { PDL }, true);
    DataSchemaResolver resolver = MultiFormatDataSchemaResolver.withBuiltinFormats(resolverPath);
    while (iterator.hasNext()) {
        File inputFile = iterator.next();
        PdlSchemaParser parser = new PdlSchemaParser(resolver);
        parser.parse(new FileInputStream(inputFile));
        if (parser.hasError()) {
            throw new InvalidExtensionSchemaException(parser.errorMessage());
        }
        List<DataSchema> topLevelDataSchemas = parser.topLevelDataSchemas();
        if (topLevelDataSchemas == null || topLevelDataSchemas.isEmpty() || topLevelDataSchemas.size() > 1) {
            throw new InvalidExtensionSchemaException("Could not parse extension schema : " + inputFile.getAbsolutePath());
        }
        DataSchema topLevelDataSchema = topLevelDataSchemas.get(0);
        if (!(topLevelDataSchema instanceof NamedDataSchema)) {
            throw new InvalidExtensionSchemaException("Invalid extension schema : " + inputFile.getAbsolutePath() + ", the schema is not a named schema.");
        }
        if (!((NamedDataSchema) topLevelDataSchema).getName().endsWith(EXTENSIONS_SUFFIX)) {
            throw new InvalidExtensionSchemaException("Invalid extension schema name: '" + ((NamedDataSchema) topLevelDataSchema).getName() + "'. The name of the extension schema must be <baseSchemaName> + 'Extensions'");
        }
        List<NamedDataSchema> includes = ((RecordDataSchema) topLevelDataSchema).getInclude();
        if (includes.size() != 1) {
            throw new InvalidExtensionSchemaException("The extension schema: '" + ((NamedDataSchema) topLevelDataSchema).getName() + "' should include and only include the base schema");
        }
        NamedDataSchema includeSchema = includes.get(0);
        if (!((NamedDataSchema) topLevelDataSchema).getName().startsWith(includeSchema.getName())) {
            throw new InvalidExtensionSchemaException("Invalid extension schema name: '" + ((NamedDataSchema) topLevelDataSchema).getName() + "'. The name of the extension schema must be baseSchemaName: '" + includeSchema.getName() + "' + 'Extensions");
        }
        List<RecordDataSchema.Field> extensionSchemaFields = ((RecordDataSchema) topLevelDataSchema).getFields().stream().filter(f -> !((RecordDataSchema) topLevelDataSchema).isFieldFromIncludes(f)).collect(Collectors.toList());
        checkExtensionSchemaFields(extensionSchemaFields);
    }
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) Options(org.apache.commons.cli.Options) LoggerFactory(org.slf4j.LoggerFactory) DataSchemaResolver(com.linkedin.data.schema.DataSchemaResolver) HelpFormatter(org.apache.commons.cli.HelpFormatter) ValidationOptions(com.linkedin.data.schema.validation.ValidationOptions) GnuParser(org.apache.commons.cli.GnuParser) DataMap(com.linkedin.data.DataMap) RequiredMode(com.linkedin.data.schema.validation.RequiredMode) CoercionMode(com.linkedin.data.schema.validation.CoercionMode) ExtensionSchemaAnnotation(com.linkedin.restli.common.ExtensionSchemaAnnotation) Map(java.util.Map) OptionBuilder(org.apache.commons.cli.OptionBuilder) CommandLine(org.apache.commons.cli.CommandLine) EXTENSION_ANNOTATION_NAMESPACE(com.linkedin.data.schema.annotation.ExtensionSchemaAnnotationHandler.EXTENSION_ANNOTATION_NAMESPACE) ValidateDataAgainstSchema(com.linkedin.data.schema.validation.ValidateDataAgainstSchema) MultiFormatDataSchemaResolver(com.linkedin.data.schema.resolver.MultiFormatDataSchemaResolver) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) CommandLineParser(org.apache.commons.cli.CommandLineParser) DataList(com.linkedin.data.DataList) UnrecognizedFieldMode(com.linkedin.data.schema.validation.UnrecognizedFieldMode) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) FileInputStream(java.io.FileInputStream) PdlSchemaParser(com.linkedin.data.schema.grammar.PdlSchemaParser) Collectors(java.util.stream.Collectors) File(java.io.File) List(java.util.List) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) RestLiToolsUtils(com.linkedin.restli.internal.tools.RestLiToolsUtils) ParseException(org.apache.commons.cli.ParseException) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) ValidationResult(com.linkedin.data.schema.validation.ValidationResult) FileInputStream(java.io.FileInputStream) DataSchema(com.linkedin.data.schema.DataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) DataSchemaResolver(com.linkedin.data.schema.DataSchemaResolver) MultiFormatDataSchemaResolver(com.linkedin.data.schema.resolver.MultiFormatDataSchemaResolver) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) File(java.io.File) PdlSchemaParser(com.linkedin.data.schema.grammar.PdlSchemaParser)

Example 15 with DataSchemaResolver

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

the class PdlEncoderTest method parseSchema.

private DataSchema parseSchema(File file) throws IOException {
    DataSchemaResolver resolver = MultiFormatDataSchemaResolver.withBuiltinFormats(pegasusSrcDir.getAbsolutePath());
    AbstractSchemaParser parser = new PdlSchemaParser(resolver);
    parser.parse(new FileInputStream(file));
    return extractSchema(parser, file.getAbsolutePath());
}
Also used : MultiFormatDataSchemaResolver(com.linkedin.data.schema.resolver.MultiFormatDataSchemaResolver) DataSchemaResolver(com.linkedin.data.schema.DataSchemaResolver) AbstractSchemaParser(com.linkedin.data.schema.AbstractSchemaParser) PdlSchemaParser(com.linkedin.data.schema.grammar.PdlSchemaParser) FileInputStream(java.io.FileInputStream)

Aggregations

DataSchemaResolver (com.linkedin.data.schema.DataSchemaResolver)16 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)5 MultiFormatDataSchemaResolver (com.linkedin.data.schema.resolver.MultiFormatDataSchemaResolver)5 IOException (java.io.IOException)5 DataMap (com.linkedin.data.DataMap)4 FileInputStream (java.io.FileInputStream)4 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)3 PdlSchemaParser (com.linkedin.data.schema.grammar.PdlSchemaParser)3 ClasspathResourceDataSchemaResolver (com.linkedin.data.schema.resolver.ClasspathResourceDataSchemaResolver)3 ValidationOptions (com.linkedin.data.schema.validation.ValidationOptions)3 ResourceSchema (com.linkedin.restli.restspec.ResourceSchema)3 File (java.io.File)3 Map (java.util.Map)3 Test (org.testng.annotations.Test)3 AbstractSchemaParser (com.linkedin.data.schema.AbstractSchemaParser)2 DataSchema (com.linkedin.data.schema.DataSchema)2 DefaultDataSchemaResolver (com.linkedin.data.schema.resolver.DefaultDataSchemaResolver)2 ValidationResult (com.linkedin.data.schema.validation.ValidationResult)2 CompatibilityInfoMap (com.linkedin.restli.tools.compatibility.CompatibilityInfoMap)2 ResourceCompatibilityChecker (com.linkedin.restli.tools.compatibility.ResourceCompatibilityChecker)2