This is an automated email from the ASF dual-hosted git repository. opwvhk pushed a commit to branch branch-1.12 in repository https://gitbox.apache.org/repos/asf/avro.git
commit 39d3ee9859e93323538a2857cc80a73862be8139 Author: Oscar Westra van Holthe - Kind <[email protected]> AuthorDate: Fri May 15 18:55:28 2026 +0200 AVRO-4176: Java parser allows field type to be object with custom type (#3772) * AVRO-4176: Disallow named type alteration The test cases for self reference unions contained a bug that seemed to make the parsing bug necessary. * AVRO-4176: Add test (cherry picked from commit 22f53c8c15e575165abdcd21f6f48efcdd4a33cd) --- .../avro/src/main/java/org/apache/avro/Schema.java | 5 +++-- .../src/test/java/org/apache/avro/TestSchema.java | 9 +++++++++ .../org/apache/avro/TestUnionSelfReference.java | 21 ++++++++------------- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/lang/java/avro/src/main/java/org/apache/avro/Schema.java b/lang/java/avro/src/main/java/org/apache/avro/Schema.java index f08afa23d1..d9b33a6e6e 100644 --- a/lang/java/avro/src/main/java/org/apache/avro/Schema.java +++ b/lang/java/avro/src/main/java/org/apache/avro/Schema.java @@ -1842,8 +1842,9 @@ public abstract class Schema extends JsonProperties implements Serializable { return parseMap(schema, context, currentNameSpace); } else if ("fixed".equals(type)) { // fixed return parseFixed(schema, context, currentNameSpace); - } else { // For unions with self reference - return context.find(type, currentNameSpace); + } else { + throw new SchemaParseException("A schema \"type\" MUST be a primitive type or one of" + + " \"enum\", \"fixed\", \"record\", \"error\", \"array\" or \"map\"."); } } else if (schema.isArray()) { // union return parseUnion(schema, context, currentNameSpace); diff --git a/lang/java/avro/src/test/java/org/apache/avro/TestSchema.java b/lang/java/avro/src/test/java/org/apache/avro/TestSchema.java index d8fb94f9f0..1be44bf2a9 100644 --- a/lang/java/avro/src/test/java/org/apache/avro/TestSchema.java +++ b/lang/java/avro/src/test/java/org/apache/avro/TestSchema.java @@ -648,6 +648,15 @@ public class TestSchema { new Schema.Parser((NameValidator) null).parse("{\"type\":\"record\",\"name\":\"\",\"fields\":[]}"); // Empty name } + @Test + void disallowTypeObjectForNamedType() { + String withTypeObjectWithNamedType = "{" + + "\"namespace\":\"tests\",\"type\":\"record\",\"name\":\"Invalid\",\"fields\":[" + + "{\"name\":\"good\",\"type\":{\"type\":\"fixed\",\"name\":\"Hash\",\"size\":16}}," + + "{\"name\":\"right\",\"type\":{\"type\":\"Hash\"}}]}"; + assertThrows(SchemaParseException.class, () -> new SchemaParser().parse(withTypeObjectWithNamedType).mainSchema()); + } + /** * Tests when a user tries to write a record with an invalid enum symbol value * that the exception returned is more descriptive than just a NPE or an diff --git a/lang/java/avro/src/test/java/org/apache/avro/TestUnionSelfReference.java b/lang/java/avro/src/test/java/org/apache/avro/TestUnionSelfReference.java index 90e19f63e5..d2ae05dfcf 100644 --- a/lang/java/avro/src/test/java/org/apache/avro/TestUnionSelfReference.java +++ b/lang/java/avro/src/test/java/org/apache/avro/TestUnionSelfReference.java @@ -31,20 +31,15 @@ public class TestUnionSelfReference { @SuppressWarnings("unused") private static final Logger LOG = LoggerFactory.getLogger(TestUnionSelfReference.class); - private static final String SIMPLE_BINARY_TREE = "{" + " \"namespace\": \"tree\"," + " \"type\": \"record\"," - + " \"name\": \"Node\"," + " \"fields\": [" + " {" + " \"name\": \"left\"," - + " \"type\": [" + " \"null\"," + " {" + " \"type\": \"Node\"" + " }" - + " ]," + " \"default\": null" + " }," + " {" + " \"name\": \"right\"," - + " \"type\": [" + " \"null\"," + " {" + " \"type\": \"Node\"" + " }" - + " ]," + " \"default\": null" + " }" + " ]" + " }"; + private static final String SIMPLE_BINARY_TREE = "{" + + "\"namespace\":\"tree\",\"type\":\"record\",\"name\":\"Node\",\"fields\":[" + + "{\"name\":\"left\",\"type\":[\"null\",\"Node\"],\"default\":null}," + + "{\"name\":\"right\",\"type\":[\"null\",\"Node\"],\"default\":null}]}"; - private static final String THREE_TYPE_UNION = "{" + " \"namespace\": \"tree\"," + " \"type\": \"record\"," - + " \"name\": \"Node\"," + " \"fields\": [" + " {" + " \"name\": \"left\"," - + " \"type\": [" + " \"null\"," + " \"string\"," + " {" - + " \"type\": \"Node\"" + " }" + " ]," + " \"default\": null" + " }," - + " {" + " \"name\": \"right\"," + " \"type\": [" + " \"null\"," - + " \"string\"," + " {" + " \"type\": \"Node\"" + " }" + " ]," - + " \"default\": null" + " }" + " ]" + " }"; + private static final String THREE_TYPE_UNION = "{" + + "\"namespace\":\"tree\",\"type\":\"record\",\"name\":\"Node\",\"fields\":[" + + "{\"name\":\"left\",\"type\":[\"null\",\"string\",\"Node\"],\"default\":null}," + + "{\"name\":\"right\",\"type\":[\"null\",\"string\",\"Node\"],\"default\":null}]}"; @Test void selfReferenceInUnion() {
