Skip to content

Commit 9429631

Browse files
oschwaldclaude
andcommitted
Change ipAddress from String to InetAddress in all models
Changed the ipAddress field type from String to InetAddress across all model and record classes. This provides better type safety and makes it clear that these fields contain IP addresses. Key changes: - Updated ipAddress field type to InetAddress in all response/record classes - Deprecated getIpAddress() methods now call ipAddress().getHostAddress() to maintain backward compatibility (still return String) - Created InetAddressSerializer to serialize InetAddress to String in JSON - Created InetAddressDeserializer to deserialize JSON strings to InetAddress - Created InetAddressModule and registered it globally in JsonSerializable - Updated all tests to call .getHostAddress() when comparing IP addresses The new ipAddress() accessor returns InetAddress, while the deprecated getIpAddress() continues to return String for backward compatibility. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 99b2ec2 commit 9429631

17 files changed

+124
-33
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.maxmind.geoip2;
2+
3+
import com.fasterxml.jackson.core.JsonParser;
4+
import com.fasterxml.jackson.databind.DeserializationContext;
5+
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
6+
import java.io.IOException;
7+
import java.net.InetAddress;
8+
import java.net.UnknownHostException;
9+
10+
/**
11+
* Deserializes a string to an InetAddress.
12+
*/
13+
public class InetAddressDeserializer extends StdDeserializer<InetAddress> {
14+
/**
15+
* Constructs an instance of {@code InetAddressDeserializer}.
16+
*/
17+
public InetAddressDeserializer() {
18+
super(InetAddress.class);
19+
}
20+
21+
@Override
22+
public InetAddress deserialize(JsonParser p, DeserializationContext ctxt)
23+
throws IOException {
24+
String value = p.getValueAsString();
25+
if (value == null || value.isEmpty()) {
26+
return null;
27+
}
28+
try {
29+
return InetAddress.getByName(value);
30+
} catch (UnknownHostException e) {
31+
throw new IOException("Invalid IP address: " + value, e);
32+
}
33+
}
34+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.maxmind.geoip2;
2+
3+
import com.fasterxml.jackson.databind.module.SimpleModule;
4+
import java.net.InetAddress;
5+
6+
/**
7+
* Jackson module for InetAddress serialization and deserialization.
8+
*/
9+
public class InetAddressModule extends SimpleModule {
10+
/**
11+
* Constructs an instance of {@code InetAddressModule}.
12+
*/
13+
public InetAddressModule() {
14+
super("InetAddressModule");
15+
addSerializer(InetAddress.class, new InetAddressSerializer());
16+
addDeserializer(InetAddress.class, new InetAddressDeserializer());
17+
}
18+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.maxmind.geoip2;
2+
3+
import com.fasterxml.jackson.core.JsonGenerator;
4+
import com.fasterxml.jackson.databind.SerializerProvider;
5+
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
6+
import java.io.IOException;
7+
import java.net.InetAddress;
8+
9+
/**
10+
* Serializes InetAddress to its host address string representation.
11+
*/
12+
public class InetAddressSerializer extends StdSerializer<InetAddress> {
13+
/**
14+
* Constructs an instance of {@code InetAddressSerializer}.
15+
*/
16+
public InetAddressSerializer() {
17+
super(InetAddress.class);
18+
}
19+
20+
@Override
21+
public void serialize(InetAddress value, JsonGenerator gen, SerializerProvider provider)
22+
throws IOException {
23+
if (value == null) {
24+
gen.writeNull();
25+
} else {
26+
gen.writeString(value.getHostAddress());
27+
}
28+
}
29+
}

src/main/java/com/maxmind/geoip2/JsonSerializable.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ default String toJson() throws IOException {
2121
JsonMapper mapper = JsonMapper.builder()
2222
.disable(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS)
2323
.addModule(new JavaTimeModule())
24+
.addModule(new InetAddressModule())
2425
.serializationInclusion(JsonInclude.Include.NON_NULL)
2526
.serializationInclusion(JsonInclude.Include.NON_EMPTY)
2627
.build();

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.maxmind.db.Network;
1212
import com.maxmind.geoip2.JsonSerializable;
1313
import com.maxmind.geoip2.NetworkDeserializer;
14+
import java.net.InetAddress;
1415

1516
/**
1617
* This class provides the GeoIP2 Anonymous IP model.
@@ -32,7 +33,7 @@
3233
public record AnonymousIpResponse(
3334
@JsonProperty("ip_address")
3435
@MaxMindDbIpAddress
35-
String ipAddress,
36+
InetAddress ipAddress,
3637

3738
@JsonProperty("is_anonymous")
3839
@MaxMindDbParameter(name = "is_anonymous", useDefault = true)
@@ -71,7 +72,7 @@ public record AnonymousIpResponse(
7172
@Deprecated(since = "5.0.0", forRemoval = true)
7273
@JsonProperty("ip_address")
7374
public String getIpAddress() {
74-
return ipAddress();
75+
return ipAddress().getHostAddress();
7576
}
7677

7778
/**

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.maxmind.db.Network;
1313
import com.maxmind.geoip2.JsonSerializable;
1414
import com.maxmind.geoip2.NetworkDeserializer;
15+
import java.net.InetAddress;
1516
import java.time.LocalDate;
1617

1718
/**
@@ -40,7 +41,7 @@
4041
public record AnonymousPlusResponse(
4142
@JsonProperty("ip_address")
4243
@MaxMindDbIpAddress
43-
String ipAddress,
44+
InetAddress ipAddress,
4445

4546
@JsonProperty("is_anonymous")
4647
@MaxMindDbParameter(name = "is_anonymous", useDefault = true)
@@ -102,7 +103,7 @@ public record AnonymousPlusResponse(
102103
*/
103104
@MaxMindDbConstructor
104105
public AnonymousPlusResponse(
105-
@MaxMindDbIpAddress String ipAddress,
106+
@MaxMindDbIpAddress InetAddress ipAddress,
106107
@MaxMindDbParameter(name = "is_anonymous", useDefault = true)
107108
boolean isAnonymous,
108109
@MaxMindDbParameter(name = "is_anonymous_vpn", useDefault = true)
@@ -142,7 +143,7 @@ public AnonymousPlusResponse(
142143
@Deprecated(since = "5.0.0", forRemoval = true)
143144
@JsonProperty("ip_address")
144145
public String getIpAddress() {
145-
return ipAddress();
146+
return ipAddress().getHostAddress();
146147
}
147148

148149
/**

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.maxmind.db.Network;
1212
import com.maxmind.geoip2.JsonSerializable;
1313
import com.maxmind.geoip2.NetworkDeserializer;
14+
import java.net.InetAddress;
1415

1516
/**
1617
* This class provides the GeoLite2 ASN model.
@@ -33,7 +34,7 @@ public record AsnResponse(
3334

3435
@JsonProperty("ip_address")
3536
@MaxMindDbIpAddress
36-
String ipAddress,
37+
InetAddress ipAddress,
3738

3839
@JsonProperty("network")
3940
@JsonDeserialize(using = NetworkDeserializer.class)
@@ -71,7 +72,7 @@ public String getAutonomousSystemOrganization() {
7172
@Deprecated(since = "5.0.0", forRemoval = true)
7273
@JsonProperty("ip_address")
7374
public String getIpAddress() {
74-
return ipAddress();
75+
return ipAddress().getHostAddress();
7576
}
7677

7778
/**

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.maxmind.db.Network;
1515
import com.maxmind.geoip2.JsonSerializable;
1616
import com.maxmind.geoip2.NetworkDeserializer;
17+
import java.net.InetAddress;
1718

1819
/**
1920
* This class provides the GeoIP2 Connection-Type model.
@@ -30,7 +31,7 @@ public record ConnectionTypeResponse(
3031

3132
@JsonProperty("ip_address")
3233
@MaxMindDbIpAddress
33-
String ipAddress,
34+
InetAddress ipAddress,
3435

3536
@JsonProperty("network")
3637
@JsonDeserialize(using = NetworkDeserializer.class)
@@ -105,7 +106,7 @@ public ConnectionType getConnectionType() {
105106
@Deprecated(since = "5.0.0", forRemoval = true)
106107
@JsonProperty("ip_address")
107108
public String getIpAddress() {
108-
return ipAddress();
109+
return ipAddress().getHostAddress();
109110
}
110111

111112
/**

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.maxmind.db.Network;
1212
import com.maxmind.geoip2.JsonSerializable;
1313
import com.maxmind.geoip2.NetworkDeserializer;
14+
import java.net.InetAddress;
1415

1516
/**
1617
* This class provides the GeoIP2 Domain model.
@@ -28,7 +29,7 @@ public record DomainResponse(
2829

2930
@JsonProperty("ip_address")
3031
@MaxMindDbIpAddress
31-
String ipAddress,
32+
InetAddress ipAddress,
3233

3334
@JsonProperty("network")
3435
@JsonDeserialize(using = NetworkDeserializer.class)
@@ -54,7 +55,7 @@ public String getDomain() {
5455
@Deprecated(since = "5.0.0", forRemoval = true)
5556
@JsonProperty("ip_address")
5657
public String getIpAddress() {
57-
return ipAddress();
58+
return ipAddress().getHostAddress();
5859
}
5960

6061
/**

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.maxmind.db.Network;
1212
import com.maxmind.geoip2.JsonSerializable;
1313
import com.maxmind.geoip2.NetworkDeserializer;
14+
import java.net.InetAddress;
1415

1516
/**
1617
* This class provides the GeoIP2 IP Risk model.
@@ -33,7 +34,7 @@
3334
public record IpRiskResponse(
3435
@JsonProperty("ip_address")
3536
@MaxMindDbIpAddress
36-
String ipAddress,
37+
InetAddress ipAddress,
3738

3839
@JsonProperty("is_anonymous")
3940
@MaxMindDbParameter(name = "is_anonymous", useDefault = true)
@@ -76,7 +77,7 @@ public record IpRiskResponse(
7677
@Deprecated(since = "5.0.0", forRemoval = true)
7778
@JsonProperty("ip_address")
7879
public String getIpAddress() {
79-
return ipAddress();
80+
return ipAddress().getHostAddress();
8081
}
8182

8283
/**

0 commit comments

Comments
 (0)