Skip to content

Commit 940c37a

Browse files
author
thoricha
committed
- When decoding a map into a map, catch ClassCastException and transform into DeserializationException
- Introduced a new unit test in ReaderTest to exercise handling a datatype mismatch between the schema in the MMDB file and the provided class to populate
1 parent 470eadb commit 940c37a

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,12 @@ private <T, V> Map<String, V> decodeMapIntoMap(
372372
for (int i = 0; i < size; i++) {
373373
String key = (String) this.decode(String.class, null).getValue();
374374
Object value = this.decode(valueClass, null).getValue();
375-
map.put(key, valueClass.cast(value));
375+
try {
376+
map.put(key, valueClass.cast(value));
377+
} catch (ClassCastException e) {
378+
throw new DeserializationException(
379+
"Error creating map entry for '" + key + "': " + e.getMessage(), e);
380+
}
376381
}
377382

378383
return map;

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.maxmind.db;
22

33
import static org.hamcrest.CoreMatchers.containsString;
4+
import static org.hamcrest.CoreMatchers.equalTo;
45
import static org.hamcrest.MatcherAssert.assertThat;
56
import static org.junit.Assert.assertArrayEquals;
67
import static org.junit.Assert.assertEquals;
@@ -620,6 +621,17 @@ public void testDecodeWrongTypeWithWrongArguments() throws IOException {
620621
assertThat(ex.getMessage(), containsString("Error getting record for IP"));
621622
}
622623

624+
@Test
625+
public void testDecodeWithDataTypeMismatchInModel() throws IOException {
626+
this.testReader = new Reader(getFile("GeoIP2-City-Test.mmdb"));
627+
DeserializationException ex = assertThrows(DeserializationException.class,
628+
() -> this.testReader.get(InetAddress.getByName("2.125.160.216"),
629+
TestDataTypeMismatchInModel.class));
630+
assertThat(ex.getMessage(), containsString("Error getting record for IP"));
631+
assertThat(ex.getMessage(), containsString("Error creating map entry for"));
632+
assertThat(ex.getCause().getCause().getClass(), equalTo(ClassCastException.class));
633+
}
634+
623635
static class TestWrongModelSubdivisions {
624636
List<TestWrongModelSubdivision> subdivisions;
625637

@@ -644,6 +656,18 @@ public TestWrongModelSubdivision(
644656
}
645657
}
646658

659+
static class TestDataTypeMismatchInModel {
660+
Map<String, Float> location;
661+
662+
@MaxMindDbConstructor
663+
public TestDataTypeMismatchInModel(
664+
@MaxMindDbParameter(name = "location")
665+
Map<String, Float> location
666+
) {
667+
this.location = location;
668+
}
669+
}
670+
647671
@Test
648672
public void testDecodeConcurrentHashMap() throws IOException {
649673
this.testReader = new Reader(getFile("GeoIP2-City-Test.mmdb"));

0 commit comments

Comments
 (0)