Skip to content

Commit

Permalink
AVRO-3938: [Java] Add null guards for Schema.Parser (#2738)
Browse files Browse the repository at this point in the history
* 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 <martin-g@users.noreply.github.com>

* Modify test case

---------

Co-authored-by: Martin Grigorov <martin-g@users.noreply.github.com>
  • Loading branch information
pingpingy1 and martin-g committed Feb 21, 2024
1 parent d0d1ed5 commit 642e270
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
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 @@ -1497,7 +1497,7 @@ public Parser() {
}

public Parser(final NameValidator validate) {
this.validate = validate;
this.validate = validate != null ? validate : NameValidator.NO_VALIDATION;
}

/**
Expand Down
9 changes: 9 additions & 0 deletions lang/java/avro/src/test/java/org/apache/avro/TestSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 642e270

Please sign in to comment.