Skip to content

Commit 22f53c8

Browse files
authored
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
1 parent 34bdcf9 commit 22f53c8

3 files changed

Lines changed: 20 additions & 15 deletions

File tree

lang/java/avro/src/main/java/org/apache/avro/Schema.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1842,8 +1842,9 @@ static Schema parse(JsonNode schema, ParseContext context, String currentNameSpa
18421842
return parseMap(schema, context, currentNameSpace);
18431843
} else if ("fixed".equals(type)) { // fixed
18441844
return parseFixed(schema, context, currentNameSpace);
1845-
} else { // For unions with self reference
1846-
return context.find(type, currentNameSpace);
1845+
} else {
1846+
throw new SchemaParseException("A schema \"type\" MUST be a primitive type or one of"
1847+
+ " \"enum\", \"fixed\", \"record\", \"error\", \"array\" or \"map\".");
18471848
}
18481849
} else if (schema.isArray()) { // union
18491850
return parseUnion(schema, context, currentNameSpace);

lang/java/avro/src/test/java/org/apache/avro/TestSchema.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,15 @@ void testParserNullValidate() {
648648
new Schema.Parser((NameValidator) null).parse("{\"type\":\"record\",\"name\":\"\",\"fields\":[]}"); // Empty name
649649
}
650650

651+
@Test
652+
void disallowTypeObjectForNamedType() {
653+
String withTypeObjectWithNamedType = "{"
654+
+ "\"namespace\":\"tests\",\"type\":\"record\",\"name\":\"Invalid\",\"fields\":["
655+
+ "{\"name\":\"good\",\"type\":{\"type\":\"fixed\",\"name\":\"Hash\",\"size\":16}},"
656+
+ "{\"name\":\"right\",\"type\":{\"type\":\"Hash\"}}]}";
657+
assertThrows(SchemaParseException.class, () -> new SchemaParser().parse(withTypeObjectWithNamedType).mainSchema());
658+
}
659+
651660
/**
652661
* Tests when a user tries to write a record with an invalid enum symbol value
653662
* that the exception returned is more descriptive than just a NPE or an

lang/java/avro/src/test/java/org/apache/avro/TestUnionSelfReference.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,15 @@ public class TestUnionSelfReference {
3131
@SuppressWarnings("unused")
3232
private static final Logger LOG = LoggerFactory.getLogger(TestUnionSelfReference.class);
3333

34-
private static final String SIMPLE_BINARY_TREE = "{" + " \"namespace\": \"tree\"," + " \"type\": \"record\","
35-
+ " \"name\": \"Node\"," + " \"fields\": [" + " {" + " \"name\": \"left\","
36-
+ " \"type\": [" + " \"null\"," + " {" + " \"type\": \"Node\"" + " }"
37-
+ " ]," + " \"default\": null" + " }," + " {" + " \"name\": \"right\","
38-
+ " \"type\": [" + " \"null\"," + " {" + " \"type\": \"Node\"" + " }"
39-
+ " ]," + " \"default\": null" + " }" + " ]" + " }";
34+
private static final String SIMPLE_BINARY_TREE = "{"
35+
+ "\"namespace\":\"tree\",\"type\":\"record\",\"name\":\"Node\",\"fields\":["
36+
+ "{\"name\":\"left\",\"type\":[\"null\",\"Node\"],\"default\":null},"
37+
+ "{\"name\":\"right\",\"type\":[\"null\",\"Node\"],\"default\":null}]}";
4038

41-
private static final String THREE_TYPE_UNION = "{" + " \"namespace\": \"tree\"," + " \"type\": \"record\","
42-
+ " \"name\": \"Node\"," + " \"fields\": [" + " {" + " \"name\": \"left\","
43-
+ " \"type\": [" + " \"null\"," + " \"string\"," + " {"
44-
+ " \"type\": \"Node\"" + " }" + " ]," + " \"default\": null" + " },"
45-
+ " {" + " \"name\": \"right\"," + " \"type\": [" + " \"null\","
46-
+ " \"string\"," + " {" + " \"type\": \"Node\"" + " }" + " ],"
47-
+ " \"default\": null" + " }" + " ]" + " }";
39+
private static final String THREE_TYPE_UNION = "{"
40+
+ "\"namespace\":\"tree\",\"type\":\"record\",\"name\":\"Node\",\"fields\":["
41+
+ "{\"name\":\"left\",\"type\":[\"null\",\"string\",\"Node\"],\"default\":null},"
42+
+ "{\"name\":\"right\",\"type\":[\"null\",\"string\",\"Node\"],\"default\":null}]}";
4843

4944
@Test
5045
void selfReferenceInUnion() {

0 commit comments

Comments
 (0)