Skip to content

Commit

Permalink
AVRO-4076: Parse Fields before adding to the context.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamespfaulkner committed Dec 6, 2024
1 parent 77540f6 commit 6d4ba4a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 8 deletions.
7 changes: 5 additions & 2 deletions lang/java/avro/src/main/java/org/apache/avro/Protocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -402,9 +402,12 @@ public boolean equals(Object o) {
if (!(o instanceof Protocol))
return false;
Protocol that = (Protocol) o;

List<Schema> resolvedSchemas = this.context.resolveAllSchemas();
List<Schema> thatResolvedSchemas = that.context.resolveAllSchemas();
return Objects.equals(this.name, that.name) && Objects.equals(this.namespace, that.namespace)
&& Objects.equals(this.context.resolveAllSchemas(), that.context.resolveAllSchemas())
&& Objects.equals(this.messages, that.messages) && this.propsEqual(that);
&& Objects.equals(this.messages, that.messages) && this.propsEqual(that)
&& resolvedSchemas.size() == thatResolvedSchemas.size() && resolvedSchemas.containsAll(thatResolvedSchemas);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion lang/java/avro/src/main/java/org/apache/avro/Schema.java
Original file line number Diff line number Diff line change
Expand Up @@ -1862,7 +1862,6 @@ private static Schema parseRecord(JsonNode schema, ParseContext context, String
Name name = parseName(schema, currentNameSpace);
String doc = parseDoc(schema);
Schema result = new RecordSchema(name, doc, isTypeError);
context.put(result);

JsonNode fieldsNode = schema.get("fields");
if (fieldsNode == null || !fieldsNode.isArray())
Expand All @@ -1877,6 +1876,7 @@ private static Schema parseRecord(JsonNode schema, ParseContext context, String
name, f.name(), getOptionalText(field, "logicalType"));
}
result.setFields(fields);
context.put(result);
parsePropertiesAndLogicalType(schema, result, SCHEMA_RESERVED);
parseAliases(schema, result);
return result;
Expand Down
15 changes: 10 additions & 5 deletions lang/java/avro/src/test/java/org/apache/avro/TestSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -599,25 +599,30 @@ void testParseMultipleFile() throws IOException {
File f1 = new File(directory.getPath(), "ApplicationEvent.avsc");
File f2 = new File(directory.getPath(), "DocumentInfo.avsc");
File f3 = new File(directory.getPath(), "MyResponse.avsc");
File f4 = new File(directory.getPath(), "EmbeddedRecord.avsc");
Assertions.assertTrue(f1.exists(), "File not exist for test " + f1.getPath());
Assertions.assertTrue(f2.exists(), "File not exist for test " + f2.getPath());
Assertions.assertTrue(f3.exists(), "File not exist for test " + f3.getPath());
Assertions.assertTrue(f4.exists(), "File not exist for test " + f4.getPath());
SchemaParser parser = new SchemaParser();
parser.parse(f1);
parser.parse(f2);
parser.parse(f3);
parser.parse(f4);
final List<Schema> schemas = parser.getParsedNamedSchemas();
Assertions.assertEquals(3, schemas.size());
Schema schemaAppEvent = schemas.get(0);
Schema schemaDocInfo = schemas.get(1);
Schema schemaResponse = schemas.get(2);
Assertions.assertEquals(4, schemas.size());
Schema eventHeaderEvent = schemas.get(0);
Schema schemaAppEvent = schemas.get(1);
Schema schemaDocInfo = schemas.get(2);
Schema schemaResponse = schemas.get(3);
Assertions.assertNotNull(schemaAppEvent);
Assertions.assertEquals(3, schemaAppEvent.getFields().size());
Assertions.assertEquals(4, schemaAppEvent.getFields().size());
Field documents = schemaAppEvent.getField("documents");
Schema docSchema = documents.schema().getTypes().get(1).getElementType();
Assertions.assertEquals(docSchema, schemaDocInfo);
Assertions.assertNotNull(schemaDocInfo);
Assertions.assertNotNull(schemaResponse);
Assertions.assertNotNull(eventHeaderEvent);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@
"doc": "",
"name": "ApplicationEvent",
"fields": [
{
"name": "embedded_record",
"type": {
"type": "record",
"name": "EmbeddedRecord",
"namespace": "model",
"fields": [
{
"name": "type",
"type": "string"
}
]
}
},
{
"name": "applicationId",
"type": "string",
Expand Down
11 changes: 11 additions & 0 deletions lang/java/avro/src/test/resources/multipleFile/EmbeddedRecord.avsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"namespace": "model",
"type" : "record",
"name" : "EmbeddedRecord",
"fields" : [
{
"name" : "type",
"type" : "string"
}
]
}

0 comments on commit 6d4ba4a

Please sign in to comment.