Skip to content

Commit

Permalink
Handle null arguments when throwing DeserializationException
Browse files Browse the repository at this point in the history
Fixes #164
  • Loading branch information
oschwald committed Apr 4, 2024
1 parent 957c399 commit 2d3540e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
CHANGELOG
=========

3.1.1
------------------

* When handling a deserialization exception, the decoder now avoids
throwing a `NullPointerException` when one of the constructor arguments
is `null`. Reported by Keith Massey. GitHub #164.

3.1.0 (2023-12-05)
------------------

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/maxmind/db/Decoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,8 @@ private <T> Object decodeMapIntoObject(int size, Class<T> cls)
StringBuilder sbErrors = new StringBuilder();
for (String key : parameterIndexes.keySet()) {
int index = parameterIndexes.get(key);
if (!parameters[index].getClass().isAssignableFrom(parameterTypes[index])) {
if (parameters[index] != null &&
!parameters[index].getClass().isAssignableFrom(parameterTypes[index])) {
sbErrors.append(" argument type mismatch in " + key + " MMDB Type: "
+ parameters[index].getClass().getCanonicalName()
+ " Java Type: " + parameterTypes[index].getCanonicalName());
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/com/maxmind/db/ReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,31 @@ public void testDecodeWithDataTypeMismatchInModel() throws IOException {
assertThat(ex.getCause().getCause().getClass(), equalTo(ClassCastException.class));
}


static class TestConstructorMismatchModel {
@MaxMindDbConstructor
public TestConstructorMismatchModel(
@MaxMindDbParameter(name = "other")
String other,
@MaxMindDbParameter(name = "utf8_string")
double utf8StringField
) {
}
}

@Test
public void testDecodeWithDataTypeMismatchInModelAndNullValue() throws IOException {
this.testReader = new Reader(getFile("MaxMind-DB-test-decoder.mmdb"));

DeserializationException ex = assertThrows(DeserializationException.class,
() -> this.testReader.get(
InetAddress.getByName("::1.1.1.0"),
TestConstructorMismatchModel.class));

assertThat(ex.getMessage(), containsString("Error creating object of type"));
assertThat(ex.getCause().getCause().getClass(), equalTo(IllegalArgumentException.class));
}

static class TestWrongModelSubdivisions {
List<TestWrongModelSubdivision> subdivisions;

Expand Down

0 comments on commit 2d3540e

Please sign in to comment.