Skip to content

Commit bf594e9

Browse files
authored
Merge pull request #645 from maxmind/greg/eng-3610
Fix IpRiskResponse decoding when ip_risk field is missing
2 parents 425f818 + 4472884 commit bf594e9

File tree

4 files changed

+62
-3
lines changed

4 files changed

+62
-3
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
CHANGELOG
22
=========
33

4+
5.0.2 (unreleased)
5+
------------------
6+
7+
* Fixed an issue where decoding `IpRiskResponse` from the IP Risk database would
8+
fail when the `ip_risk` field was not present in the database record. The
9+
`ipRisk` field now defaults to 0.0 when not present. A value of 0.0 indicates
10+
that the risk score was not set in the database. In a future major release,
11+
this field may be changed to a nullable `Double` to better distinguish between
12+
"no data" and "zero risk". Reported by Fabrice Bacchella. GitHub #644.
13+
414
5.0.1 (2025-12-02)
515
------------------
616

src/main/java/com/maxmind/geoip2/model/IpRiskResponse.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,14 @@
2828
* @param isTorExitNode Whether the IP address is a Tor exit node.
2929
* @param network The network associated with the record. In particular, this is the largest
3030
* network where all the fields besides IP address have the same value.
31-
* @param ipRisk The IP risk of a model.
31+
* @param ipRisk The IP risk score for the IP address. A value of 0.0 indicates that the
32+
* risk score was not set in the database. This is a limitation of primitive
33+
* types in Java - the value cannot be null. In a future major version, this
34+
* field may be changed to a nullable {@code Double} to distinguish between
35+
* "no data" and "zero risk".
3236
*/
37+
// TODO: In the next major version (6.0.0), consider changing ipRisk to Double
38+
// to allow null values, distinguishing "no data" from "zero risk".
3339
public record IpRiskResponse(
3440
@JsonProperty("ip_address")
3541
@MaxMindDbIpAddress
@@ -65,7 +71,7 @@ public record IpRiskResponse(
6571
Network network,
6672

6773
@JsonProperty("ip_risk")
68-
@MaxMindDbParameter(name = "ip_risk")
74+
@MaxMindDbParameter(name = "ip_risk", useDefault = true)
6975
double ipRisk
7076
) implements JsonSerializable {
7177

src/test/java/com/maxmind/geoip2/DatabaseReaderTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.maxmind.geoip2.model.CountryResponse;
2020
import com.maxmind.geoip2.model.DomainResponse;
2121
import com.maxmind.geoip2.model.EnterpriseResponse;
22+
import com.maxmind.geoip2.model.IpRiskResponse;
2223
import com.maxmind.geoip2.model.IspResponse;
2324
import java.io.File;
2425
import java.io.IOException;
@@ -427,6 +428,48 @@ public void testIsp() throws Exception {
427428
}
428429
}
429430

431+
@Test
432+
public void testIpRisk() throws Exception {
433+
try (DatabaseReader reader = new DatabaseReader.Builder(
434+
this.getFile("GeoIP2-IP-Risk-Test.mmdb")).build()
435+
) {
436+
InetAddress ipAddress = InetAddress.getByName("214.2.3.0");
437+
IpRiskResponse response = reader.ipRisk(ipAddress);
438+
assertEquals(25.0, response.ipRisk());
439+
assertTrue(response.isAnonymous());
440+
assertTrue(response.isAnonymousVpn());
441+
assertFalse(response.isHostingProvider());
442+
assertFalse(response.isPublicProxy());
443+
assertFalse(response.isResidentialProxy());
444+
assertFalse(response.isTorExitNode());
445+
assertEquals(ipAddress.getHostAddress(), response.ipAddress().getHostAddress());
446+
assertEquals("214.2.3.0/30", response.network().toString());
447+
448+
IpRiskResponse tryResponse = reader.tryIpRisk(ipAddress).get();
449+
assertEquals(response.toJson(), tryResponse.toJson());
450+
}
451+
}
452+
453+
@Test
454+
public void testIpRiskWithoutIpRiskField() throws Exception {
455+
// Test that records without ip_risk field default to 0.0.
456+
// A value of 0.0 indicates that the risk score was not set in the database.
457+
try (DatabaseReader reader = new DatabaseReader.Builder(
458+
this.getFile("GeoIP2-IP-Risk-Test.mmdb")).build()
459+
) {
460+
InetAddress ipAddress = InetAddress.getByName("11.1.2.3");
461+
IpRiskResponse response = reader.ipRisk(ipAddress);
462+
assertEquals(0.0, response.ipRisk());
463+
assertTrue(response.isAnonymous());
464+
assertFalse(response.isAnonymousVpn());
465+
assertFalse(response.isHostingProvider());
466+
assertTrue(response.isPublicProxy());
467+
assertFalse(response.isResidentialProxy());
468+
assertFalse(response.isTorExitNode());
469+
assertEquals(ipAddress.getHostAddress(), response.ipAddress().getHostAddress());
470+
}
471+
}
472+
430473
private File getFile(String filename) throws URISyntaxException {
431474
URL resource = DatabaseReaderTest.class
432475
.getResource("/maxmind-db/test-data/" + filename);

src/test/resources/maxmind-db

Submodule maxmind-db updated 47 files

0 commit comments

Comments
 (0)