github-advanced-security[bot] commented on code in PR #2642:
URL: https://github.com/apache/avro/pull/2642#discussion_r1459347034
##########
lang/java/compiler/src/test/java/org/apache/avro/compiler/schema/TestSchemas.java:
##########
@@ -265,20 +267,21 @@
+ "{\"name\": \"f1\", \"type\": {\"type\": \"record\", \"name\":
\"c2\", \"fields\": "
+ "[{\"name\": \"f11\", \"type\": \"int\"},{\"name\": \"f12\",
\"type\": \"double\"}" + "]}},"
+ "{\"name\": \"f2\", \"type\": \"long\"}" + "]}";
- assertEquals("c1.c2.\"int\".!\"long\".!", Schemas.visit(new
Schema.Parser().parse(s11), new TestVisitor() {
- public SchemaVisitorAction visitTerminal(Schema terminal) {
- sb.append(terminal).append('.');
- return SchemaVisitorAction.SKIP_SIBLINGS;
- }
- }));
+ assertEquals("c1.c2.\"int\".!\"long\".!",
+ Schemas.visit(new SchemaParser().parse(s11).mainSchema(), new
TestVisitor() {
+ public SchemaVisitorAction visitTerminal(Schema terminal) {
Review Comment:
## Missing Override annotation
This method overrides [TestVisitor.visitTerminal](1); it is advisable to add
an Override annotation.
[Show more
details](https://github.com/apache/avro/security/code-scanning/3164)
##########
lang/java/avro/src/main/java/org/apache/avro/ParseContext.java:
##########
@@ -216,12 +252,94 @@
newSchemas.clear();
}
+ public SchemaParser.ParseResult commit(Schema mainSchema) {
+ Collection<Schema> parsedNamedSchemas = newSchemas.values();
+ SchemaParser.ParseResult parseResult = new SchemaParser.ParseResult() {
+ @Override
+ public Schema mainSchema() {
+ return mainSchema == null ? null : resolve(mainSchema);
+ }
+
+ @Override
+ public List<Schema> parsedNamedSchemas() {
+ return
parsedNamedSchemas.stream().map(ParseContext.this::resolve).collect(Collectors.toList());
+ }
+ };
+ commit();
+ return parseResult;
+ }
+
public void rollback() {
newSchemas.clear();
}
/**
- * Return all known types by their fullname.
+ * Resolve all (named) schemas that were parsed. This resolves all forward
+ * references, even if parsed from different files. Note: the context must be
+ * committed for this method to work.
+ *
+ * @return all parsed schemas, in the order they were parsed
+ * @throws AvroTypeException if a schema reference cannot be resolved
+ */
+ public List<Schema> resolveAllSchemas() {
+ ensureSchemasAreResolved();
+
+ return new ArrayList<>(oldSchemas.values());
+ }
+
+ private void ensureSchemasAreResolved() {
+ if (hasNewSchemas()) {
+ throw new IllegalStateException("Schemas cannot be resolved unless the
ParseContext is committed.");
+ }
+ if (resolvingVisitor == null) {
+ NameValidator saved = Schema.getNameValidator();
+ try {
+ // Ensure we use the same validation when copying schemas as when they
were
+ // defined.
+ Schema.setNameValidator(nameValidator);
Review Comment:
## Deprecated method or constructor invocation
Invoking [Schema.setNameValidator](1) should be avoided because it has been
deprecated.
[Show more
details](https://github.com/apache/avro/security/code-scanning/3168)
##########
lang/java/avro/src/main/java/org/apache/avro/JsonSchemaParser.java:
##########
@@ -57,26 +57,32 @@
for (String fragment : fragments) {
buffer.append(fragment);
}
- return new JsonSchemaParser().parse(new
ParseContext(NameValidator.NO_VALIDATION), buffer, null);
+
+ boolean saved = Schema.getValidateDefaults();
Review Comment:
## Deprecated method or constructor invocation
Invoking [Schema.getValidateDefaults](1) should be avoided because it has
been deprecated.
[Show more
details](https://github.com/apache/avro/security/code-scanning/3165)
##########
lang/java/idl/src/main/java/org/apache/avro/idl/IdlReader.java:
##########
@@ -404,6 +390,14 @@
@Override
public void
exitMainSchemaDeclaration(IdlParser.MainSchemaDeclarationContext ctx) {
mainSchema = typeStack.pop();
+ switch (mainSchema.getType()) {
Review Comment:
## Missing enum case in switch
Switch statement does not have a case for [NULL](1).
Switch statement does not have a case for [BOOLEAN](2).
Switch statement does not have a case for [DOUBLE](3).
Switch statement does not have a case for [FLOAT](4).
Switch statement does not have a case for [LONG](5).
Switch statement does not have a case for [INT](6).
Switch statement does not have a case for [BYTES](7).
Switch statement does not have a case for [STRING](8).
Switch statement does not have a case for [UNION](9).
Switch statement does not have a case for [MAP](10).
Switch statement does not have a case for [ARRAY](11).
[Show more
details](https://github.com/apache/avro/security/code-scanning/3171)
##########
lang/java/avro/src/main/java/org/apache/avro/ParseContext.java:
##########
@@ -216,12 +252,94 @@
newSchemas.clear();
}
+ public SchemaParser.ParseResult commit(Schema mainSchema) {
+ Collection<Schema> parsedNamedSchemas = newSchemas.values();
+ SchemaParser.ParseResult parseResult = new SchemaParser.ParseResult() {
+ @Override
+ public Schema mainSchema() {
+ return mainSchema == null ? null : resolve(mainSchema);
+ }
+
+ @Override
+ public List<Schema> parsedNamedSchemas() {
+ return
parsedNamedSchemas.stream().map(ParseContext.this::resolve).collect(Collectors.toList());
+ }
+ };
+ commit();
+ return parseResult;
+ }
+
public void rollback() {
newSchemas.clear();
}
/**
- * Return all known types by their fullname.
+ * Resolve all (named) schemas that were parsed. This resolves all forward
+ * references, even if parsed from different files. Note: the context must be
+ * committed for this method to work.
+ *
+ * @return all parsed schemas, in the order they were parsed
+ * @throws AvroTypeException if a schema reference cannot be resolved
+ */
+ public List<Schema> resolveAllSchemas() {
+ ensureSchemasAreResolved();
+
+ return new ArrayList<>(oldSchemas.values());
+ }
+
+ private void ensureSchemasAreResolved() {
+ if (hasNewSchemas()) {
+ throw new IllegalStateException("Schemas cannot be resolved unless the
ParseContext is committed.");
+ }
+ if (resolvingVisitor == null) {
+ NameValidator saved = Schema.getNameValidator();
+ try {
+ // Ensure we use the same validation when copying schemas as when they
were
+ // defined.
+ Schema.setNameValidator(nameValidator);
+ SchemaResolver.ResolvingVisitor visitor = new
SchemaResolver.ResolvingVisitor(oldSchemas::get);
+ oldSchemas.values().forEach(schema -> Schemas.visit(schema, visitor));
+ // Before this point is where we can get exceptions due to resolving
failures.
+ for (Map.Entry<String, Schema> entry : oldSchemas.entrySet()) {
+ entry.setValue(visitor.getResolved(entry.getValue()));
+ }
+ resolvingVisitor = visitor;
+ } finally {
+ Schema.setNameValidator(saved);
Review Comment:
## Deprecated method or constructor invocation
Invoking [Schema.setNameValidator](1) should be avoided because it has been
deprecated.
[Show more
details](https://github.com/apache/avro/security/code-scanning/3169)
##########
lang/java/compiler/src/main/java/org/apache/avro/compiler/idl/SchemaResolver.java:
##########
@@ -108,7 +108,7 @@
*/
static Protocol resolve(final Protocol protocol) {
Protocol result = new Protocol(protocol.getName(), protocol.getDoc(),
protocol.getNamespace());
- final Collection<Schema> types = protocol.getTypes();
+ final Collection<Schema> types = protocol.getUnresolvedTypes();
Review Comment:
## Deprecated method or constructor invocation
Invoking [Protocol.getUnresolvedTypes](1) should be avoided because it has
been deprecated.
[Show more
details](https://github.com/apache/avro/security/code-scanning/3170)
##########
lang/java/avro/src/main/java/org/apache/avro/JsonSchemaParser.java:
##########
@@ -57,26 +57,32 @@
for (String fragment : fragments) {
buffer.append(fragment);
}
- return new JsonSchemaParser().parse(new
ParseContext(NameValidator.NO_VALIDATION), buffer, null);
+
+ boolean saved = Schema.getValidateDefaults();
+ try {
+ Schema.setValidateDefaults(false);
Review Comment:
## Deprecated method or constructor invocation
Invoking [Schema.setValidateDefaults](1) should be avoided because it has
been deprecated.
[Show more
details](https://github.com/apache/avro/security/code-scanning/3166)
##########
lang/java/avro/src/main/java/org/apache/avro/ParseContext.java:
##########
@@ -216,12 +252,94 @@
newSchemas.clear();
}
+ public SchemaParser.ParseResult commit(Schema mainSchema) {
+ Collection<Schema> parsedNamedSchemas = newSchemas.values();
+ SchemaParser.ParseResult parseResult = new SchemaParser.ParseResult() {
+ @Override
+ public Schema mainSchema() {
+ return mainSchema == null ? null : resolve(mainSchema);
+ }
+
+ @Override
+ public List<Schema> parsedNamedSchemas() {
+ return
parsedNamedSchemas.stream().map(ParseContext.this::resolve).collect(Collectors.toList());
+ }
+ };
+ commit();
+ return parseResult;
+ }
+
public void rollback() {
newSchemas.clear();
}
/**
- * Return all known types by their fullname.
+ * Resolve all (named) schemas that were parsed. This resolves all forward
+ * references, even if parsed from different files. Note: the context must be
+ * committed for this method to work.
+ *
+ * @return all parsed schemas, in the order they were parsed
+ * @throws AvroTypeException if a schema reference cannot be resolved
+ */
+ public List<Schema> resolveAllSchemas() {
+ ensureSchemasAreResolved();
+
+ return new ArrayList<>(oldSchemas.values());
+ }
+
+ private void ensureSchemasAreResolved() {
+ if (hasNewSchemas()) {
+ throw new IllegalStateException("Schemas cannot be resolved unless the
ParseContext is committed.");
+ }
+ if (resolvingVisitor == null) {
+ NameValidator saved = Schema.getNameValidator();
Review Comment:
## Deprecated method or constructor invocation
Invoking [Schema.getNameValidator](1) should be avoided because it has been
deprecated.
[Show more
details](https://github.com/apache/avro/security/code-scanning/3167)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]