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());
}
}
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")));
}
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);
}
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);
}
}
}
}
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);
}
}
}
Aggregations