Search in sources :

Example 1 with AvscIssue

use of com.linkedin.avroutil1.parser.avsc.AvscIssue in project avro-util by linkedin.

the class AvscParserTest method testParsingHorribleDefaultValues.

@Test
public void testParsingHorribleDefaultValues() throws Exception {
    String avsc = TestUtil.load("schemas/TestRecordWithHorribleDefaultValues.avsc");
    AvscParser parser = new AvscParser();
    AvscParseResult result = parser.parse(avsc);
    Assert.assertNull(result.getParseError());
    AvroRecordSchema schema = (AvroRecordSchema) result.getTopLevelSchema();
    Assert.assertNotNull(schema);
    for (AvroSchemaField field : schema.getFields()) {
        List<AvscIssue> issuesWithField = result.getIssues(field);
        Assert.assertFalse(issuesWithField.isEmpty(), "field " + field.getName() + " has no issues?!");
        Assert.assertFalse(issuesWithField.stream().noneMatch(issue -> issue.getMessage().contains("default value")));
        Assert.assertNull(field.getDefaultValue());
    }
}
Also used : Arrays(java.util.Arrays) TestUtil(com.linkedin.avroutil1.testcommon.TestUtil) UnresolvedReferenceException(com.linkedin.avroutil1.parser.exceptions.UnresolvedReferenceException) Test(org.testng.annotations.Test) SchemaOrRef(com.linkedin.avroutil1.model.SchemaOrRef) AvroSchemaField(com.linkedin.avroutil1.model.AvroSchemaField) JSONAssert(org.skyscreamer.jsonassert.JSONAssert) GenericData(org.apache.avro.generic.GenericData) AvroArraySchema(com.linkedin.avroutil1.model.AvroArraySchema) LinkedHashMap(java.util.LinkedHashMap) BigDecimal(java.math.BigDecimal) AvroFixedSchema(com.linkedin.avroutil1.model.AvroFixedSchema) Assert(org.testng.Assert) JsonPropertiesContainer(com.linkedin.avroutil1.model.JsonPropertiesContainer) AvroLogicalType(com.linkedin.avroutil1.model.AvroLogicalType) AvroUnionSchema(com.linkedin.avroutil1.model.AvroUnionSchema) AvroMapSchema(com.linkedin.avroutil1.model.AvroMapSchema) Schema(org.apache.avro.Schema) AvroEnumSchema(com.linkedin.avroutil1.model.AvroEnumSchema) AvroPrimitiveSchema(com.linkedin.avroutil1.model.AvroPrimitiveSchema) IOException(java.io.IOException) AvroSchema(com.linkedin.avroutil1.model.AvroSchema) Collectors(java.util.stream.Collectors) AvroJavaStringRepresentation(com.linkedin.avroutil1.model.AvroJavaStringRepresentation) List(java.util.List) AvroRecordSchema(com.linkedin.avroutil1.model.AvroRecordSchema) JSONCompareMode(org.skyscreamer.jsonassert.JSONCompareMode) AvroSyntaxException(com.linkedin.avroutil1.parser.exceptions.AvroSyntaxException) AvroType(com.linkedin.avroutil1.model.AvroType) Collections(java.util.Collections) JsonParseException(com.linkedin.avroutil1.parser.exceptions.JsonParseException) AvroRecordSchema(com.linkedin.avroutil1.model.AvroRecordSchema) AvroSchemaField(com.linkedin.avroutil1.model.AvroSchemaField) Test(org.testng.annotations.Test)

Example 2 with AvscIssue

use of com.linkedin.avroutil1.parser.avsc.AvscIssue in project avro-util by linkedin.

the class AvscParserTest method testParseBadEnumDefault.

@Test
public void testParseBadEnumDefault() throws Exception {
    String avsc = TestUtil.load("schemas/TestBadEnumDefault.avsc");
    AvscParser parser = new AvscParser();
    AvscParseResult result = parser.parse(avsc);
    AvroRecordSchema recordSchema = (AvroRecordSchema) result.getTopLevelSchema();
    List<AvscIssue> issues = result.getIssues(recordSchema);
    Assert.assertFalse(issues.stream().noneMatch(issue -> issue.getMessage().contains("default value")));
    AvroEnumSchema enumSchema = (AvroEnumSchema) recordSchema.getField("enumField").getSchema();
    issues = result.getIssues(enumSchema);
    Assert.assertFalse(issues.stream().noneMatch(issue -> issue.getMessage().contains("default value")));
}
Also used : Arrays(java.util.Arrays) TestUtil(com.linkedin.avroutil1.testcommon.TestUtil) UnresolvedReferenceException(com.linkedin.avroutil1.parser.exceptions.UnresolvedReferenceException) Test(org.testng.annotations.Test) SchemaOrRef(com.linkedin.avroutil1.model.SchemaOrRef) AvroSchemaField(com.linkedin.avroutil1.model.AvroSchemaField) JSONAssert(org.skyscreamer.jsonassert.JSONAssert) GenericData(org.apache.avro.generic.GenericData) AvroArraySchema(com.linkedin.avroutil1.model.AvroArraySchema) LinkedHashMap(java.util.LinkedHashMap) BigDecimal(java.math.BigDecimal) AvroFixedSchema(com.linkedin.avroutil1.model.AvroFixedSchema) Assert(org.testng.Assert) JsonPropertiesContainer(com.linkedin.avroutil1.model.JsonPropertiesContainer) AvroLogicalType(com.linkedin.avroutil1.model.AvroLogicalType) AvroUnionSchema(com.linkedin.avroutil1.model.AvroUnionSchema) AvroMapSchema(com.linkedin.avroutil1.model.AvroMapSchema) Schema(org.apache.avro.Schema) AvroEnumSchema(com.linkedin.avroutil1.model.AvroEnumSchema) AvroPrimitiveSchema(com.linkedin.avroutil1.model.AvroPrimitiveSchema) IOException(java.io.IOException) AvroSchema(com.linkedin.avroutil1.model.AvroSchema) Collectors(java.util.stream.Collectors) AvroJavaStringRepresentation(com.linkedin.avroutil1.model.AvroJavaStringRepresentation) List(java.util.List) AvroRecordSchema(com.linkedin.avroutil1.model.AvroRecordSchema) JSONCompareMode(org.skyscreamer.jsonassert.JSONCompareMode) AvroSyntaxException(com.linkedin.avroutil1.parser.exceptions.AvroSyntaxException) AvroType(com.linkedin.avroutil1.model.AvroType) Collections(java.util.Collections) JsonParseException(com.linkedin.avroutil1.parser.exceptions.JsonParseException) AvroEnumSchema(com.linkedin.avroutil1.model.AvroEnumSchema) AvroRecordSchema(com.linkedin.avroutil1.model.AvroRecordSchema) Test(org.testng.annotations.Test)

Example 3 with AvscIssue

use of com.linkedin.avroutil1.parser.avsc.AvscIssue in project avro-util by linkedin.

the class AvscSchemaWriterTest method testParsingCycle.

/**
 * given an avsc, parses and re-prints it using our code
 * and compares the result to vanilla avro.
 * @param avsc
 */
private void testParsingCycle(String avsc) {
    Schema reference = Schema.parse(avsc);
    AvscParser parser = new AvscParser();
    AvscParseResult parseResults = parser.parse(avsc);
    List<AvscIssue> parseIssues = parseResults.getIssues();
    Assert.assertTrue(parseIssues == null || parseIssues.isEmpty(), "parse issues: " + parseIssues);
    AvroSchema parsed = parseResults.getTopLevelSchema();
    Assert.assertNotNull(parsed);
    AvscSchemaWriter writer = new AvscSchemaWriter();
    AvscFile file = writer.writeSingle(parsed);
    Assert.assertNotNull(file);
    if (HelperConsts.NAMED_TYPES.contains(reference.getType())) {
        // for named schemas the file path is determined by schema name
        Assert.assertNotNull(file.getPathFromRoot());
        String expectedFileName = reference.getFullName().replaceAll("\\.", Matcher.quoteReplacement(File.separator)) + ".avsc";
        Assert.assertEquals(file.getPathFromRoot().toString(), expectedFileName);
    } else {
        // cant auto-name files containing other schema types
        Assert.assertNull(file.getPathFromRoot());
    }
    String avsc2 = file.getContents();
    Schema afterCycle = Schema.parse(avsc2);
    Assert.assertEquals(reference, afterCycle);
}
Also used : AvroSchema(com.linkedin.avroutil1.model.AvroSchema) AvscParser(com.linkedin.avroutil1.parser.avsc.AvscParser) AvscIssue(com.linkedin.avroutil1.parser.avsc.AvscIssue) Schema(org.apache.avro.Schema) AvroSchema(com.linkedin.avroutil1.model.AvroSchema) AvscParseResult(com.linkedin.avroutil1.parser.avsc.AvscParseResult)

Example 4 with AvscIssue

use of com.linkedin.avroutil1.parser.avsc.AvscIssue in project avro-util by linkedin.

the class AvroParseContext method resolveReferences.

public void resolveReferences() {
    sealed = true;
    // build up an index of FQCNs (also find dups)
    knownNamedSchemas = new HashMap<>(individualResults.size());
    duplicates = new HashMap<>(1);
    for (AvscParseResult singleFile : individualResults) {
        Throwable error = singleFile.getParseError();
        if (error != null) {
            // dont touch files with outright failures
            continue;
        }
        Map<String, AvroSchema> namedInFile = singleFile.getDefinedNamedSchemas();
        namedInFile.forEach((fqcn, schema) -> {
            AvscParseResult firstDefinition = knownNamedSchemas.putIfAbsent(fqcn, singleFile);
            if (firstDefinition != null) {
                // TODO - find dups in aliases as well ?
                // this is a dup
                duplicates.compute(fqcn, (k, dups) -> {
                    if (dups == null) {
                        dups = new ArrayList<>(2);
                        dups.add(firstDefinition);
                    }
                    dups.add(singleFile);
                    return dups;
                });
            }
        });
    }
    // TODO - add context-level issues for dups
    // resolve any unresolved references in individual file results from other files
    externalReferences = new ArrayList<>();
    for (AvscParseResult singleFile : individualResults) {
        List<SchemaOrRef> externalRefs = singleFile.getExternalReferences();
        for (SchemaOrRef ref : externalRefs) {
            String simpleName = ref.getRef();
            AvscParseResult simpleNameResolution = knownNamedSchemas.get(simpleName);
            AvscParseResult inheritedNameResolution = null;
            String inheritedName = ref.getInheritedName();
            if (inheritedName != null) {
                inheritedNameResolution = knownNamedSchemas.get(inheritedName);
            }
            // the inherited namespace).
            if (inheritedNameResolution != null) {
                ref.setResolvedTo(inheritedNameResolution.getDefinedNamedSchemas().get(inheritedName));
                if (simpleNameResolution != null) {
                    String msg = "ERROR: Two different schemas found for reference " + simpleName + " with inherited name " + inheritedName + ". Only one should exist.";
                    singleFile.addIssue(new AvscIssue(ref.getCodeLocation(), IssueSeverity.WARNING, msg, new IllegalStateException(msg)));
                }
            } else if (simpleNameResolution != null) {
                ref.setResolvedTo(simpleNameResolution.getDefinedNamedSchemas().get(simpleName));
            } else {
                // fqcn is unresolved in this context
                externalReferences.add(ref);
            }
        }
    }
}
Also used : AvroSchema(com.linkedin.avroutil1.model.AvroSchema) SchemaOrRef(com.linkedin.avroutil1.model.SchemaOrRef)

Example 5 with AvscIssue

use of com.linkedin.avroutil1.parser.avsc.AvscIssue in project avro-util by linkedin.

the class AvscFileParseContext method resolveReferences.

private void resolveReferences(SchemaOrRef possiblyRef) {
    if (possiblyRef.isResolved()) {
        // either an already- resolved reference or an inline definition
        if (possiblyRef.getDecl() != null) {
            // recurse into inline definitions
            resolveReferences(possiblyRef.getDecl());
        }
    } else {
        // unresolved (and so must be a) reference
        String simpleName = possiblyRef.getRef();
        AvroSchema simpleNameResolution = definedNamedSchemas.get(simpleName);
        AvroSchema inheritedNameResolution = null;
        String inheritedName = possiblyRef.getInheritedName();
        if (inheritedName != null) {
            inheritedNameResolution = definedNamedSchemas.get(inheritedName);
        }
        if (inheritedNameResolution != null) {
            possiblyRef.setResolvedTo(inheritedNameResolution);
            if (simpleNameResolution != null) {
                String msg = "Two different schemas found for reference " + simpleName + " with inherited name " + inheritedName + ". Only one should exist.";
                AvscIssue issue = new AvscIssue(possiblyRef.getCodeLocation(), IssueSeverity.WARNING, msg, new IllegalStateException(msg));
                issues.add(issue);
            }
        } else if (simpleNameResolution != null) {
            possiblyRef.setResolvedTo(simpleNameResolution);
        } else {
            externalReferences.add(possiblyRef);
        }
    }
}
Also used : AvroSchema(com.linkedin.avroutil1.model.AvroSchema)

Aggregations

AvroSchema (com.linkedin.avroutil1.model.AvroSchema)6 AvroArraySchema (com.linkedin.avroutil1.model.AvroArraySchema)3 AvroEnumSchema (com.linkedin.avroutil1.model.AvroEnumSchema)3 AvroFixedSchema (com.linkedin.avroutil1.model.AvroFixedSchema)3 AvroPrimitiveSchema (com.linkedin.avroutil1.model.AvroPrimitiveSchema)3 AvroType (com.linkedin.avroutil1.model.AvroType)3 SchemaOrRef (com.linkedin.avroutil1.model.SchemaOrRef)3 BigDecimal (java.math.BigDecimal)3 Schema (org.apache.avro.Schema)3 AvroJavaStringRepresentation (com.linkedin.avroutil1.model.AvroJavaStringRepresentation)2 AvroLogicalType (com.linkedin.avroutil1.model.AvroLogicalType)2 AvroMapSchema (com.linkedin.avroutil1.model.AvroMapSchema)2 AvroRecordSchema (com.linkedin.avroutil1.model.AvroRecordSchema)2 AvroSchemaField (com.linkedin.avroutil1.model.AvroSchemaField)2 AvroUnionSchema (com.linkedin.avroutil1.model.AvroUnionSchema)2 JsonPropertiesContainer (com.linkedin.avroutil1.model.JsonPropertiesContainer)2 AvroSyntaxException (com.linkedin.avroutil1.parser.exceptions.AvroSyntaxException)2 JsonParseException (com.linkedin.avroutil1.parser.exceptions.JsonParseException)2 UnresolvedReferenceException (com.linkedin.avroutil1.parser.exceptions.UnresolvedReferenceException)2 TestUtil (com.linkedin.avroutil1.testcommon.TestUtil)2