Skip to content

Commit 7e16a1d

Browse files
committed
Handle null arguments when throwing DeserializationException
Fixes #164
1 parent 957c399 commit 7e16a1d

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
CHANGELOG
22
=========
33

4+
3.1.1
5+
------------------
6+
7+
* When handling a deserialization exception, the decoder now avoids
8+
throwing a `NullPointerException` when one of the constructor arguments
9+
is `null`. Reported by Keith Massey. GitHub #164.
10+
411
3.1.0 (2023-12-05)
512
------------------
613

src/main/java/com/maxmind/db/Decoder.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,8 @@ private <T> Object decodeMapIntoObject(int size, Class<T> cls)
447447
StringBuilder sbErrors = new StringBuilder();
448448
for (String key : parameterIndexes.keySet()) {
449449
int index = parameterIndexes.get(key);
450-
if (!parameters[index].getClass().isAssignableFrom(parameterTypes[index])) {
450+
if (parameters[index] != null
451+
&& !parameters[index].getClass().isAssignableFrom(parameterTypes[index])) {
451452
sbErrors.append(" argument type mismatch in " + key + " MMDB Type: "
452453
+ parameters[index].getClass().getCanonicalName()
453454
+ " Java Type: " + parameterTypes[index].getCanonicalName());

src/test/java/com/maxmind/db/ReaderTest.java

+25
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,31 @@ public void testDecodeWithDataTypeMismatchInModel() throws IOException {
944944
assertThat(ex.getCause().getCause().getClass(), equalTo(ClassCastException.class));
945945
}
946946

947+
948+
static class TestConstructorMismatchModel {
949+
@MaxMindDbConstructor
950+
public TestConstructorMismatchModel(
951+
@MaxMindDbParameter(name = "other")
952+
String other,
953+
@MaxMindDbParameter(name = "utf8_string")
954+
double utf8StringField
955+
) {
956+
}
957+
}
958+
959+
@Test
960+
public void testDecodeWithDataTypeMismatchInModelAndNullValue() throws IOException {
961+
this.testReader = new Reader(getFile("MaxMind-DB-test-decoder.mmdb"));
962+
963+
DeserializationException ex = assertThrows(DeserializationException.class,
964+
() -> this.testReader.get(
965+
InetAddress.getByName("::1.1.1.0"),
966+
TestConstructorMismatchModel.class));
967+
968+
assertThat(ex.getMessage(), containsString("Error creating object of type"));
969+
assertThat(ex.getCause().getCause().getClass(), equalTo(IllegalArgumentException.class));
970+
}
971+
947972
static class TestWrongModelSubdivisions {
948973
List<TestWrongModelSubdivision> subdivisions;
949974

0 commit comments

Comments
 (0)