From 642e2705f7481e9bfa3c7598b70e17a3d9adcd8a Mon Sep 17 00:00:00 2001 From: Heewon Lee <94441510+pingpingy1@users.noreply.github.com> Date: Wed, 21 Feb 2024 18:17:07 +0900 Subject: [PATCH] AVRO-3938: [Java] Add null guards for Schema.Parser (#2738) * AVRO-3938: Add null guards for Schema.Parser The `Schema.Parser` class has a private final property `validate`. This means that the value provided to its constructor is saved and cannot be changed at a later time. However, if `null` is passed as `validate`, then it is bound to throw a `NullPointerException` at an arbitrary time. This commit adds a fail-fast guard against `null` to prevent this. * Apply suggestions from code review Co-authored-by: Martin Grigorov * Modify test case --------- Co-authored-by: Martin Grigorov --- lang/java/avro/src/main/java/org/apache/avro/Schema.java | 2 +- .../avro/src/test/java/org/apache/avro/TestSchema.java | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) 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 f312ecfb6f2..5e62bd1100b 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 @@ -1497,7 +1497,7 @@ public Parser() { } public Parser(final NameValidator validate) { - this.validate = validate; + this.validate = validate != null ? validate : NameValidator.NO_VALIDATION; } /** 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 805f2b80b2a..41f0474c9da 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 @@ -621,4 +621,13 @@ void add_types() { assertNotNull(f1); assertEquals(schemaRecord1, f1.schema()); } + + /** + * Tests the behavior of Schema.Parser if its validation option is set to + * `null`. This is then set to the default option `NO_VALIDATION`. + */ + @Test + void testParserNullValidate() { + new Schema.Parser(null).parse("{\"type\":\"record\",\"name\":\"\",\"fields\":[]}"); // Empty name + } }