Skip to content

Commit f3c95dc

Browse files
authored
Fix #6185: bracket unresolved IPv6 host name in InetSocketAddress serialization (#6186)
1 parent 1901d66 commit f3c95dc

4 files changed

Lines changed: 33 additions & 1 deletion

File tree

‎release-notes/CREDITS-2.x‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Co-Authors (with only partial listings below):
1414

1515
----------------------------------------------------------------------------
1616

17-
Pascal GŽlinas:
17+
Pascal Gélinas:
1818
* Contributed fixes to 'MappingIterator' handling (Pull#58 and Pull#59)
1919
(2.1.0)
2020
* Reported #220: ContainerNode missing 'createNumber(BigInteger)'
@@ -1936,3 +1936,7 @@ Aysha Afrah Ziya (@aysha-afrah26)
19361936
* Reported #6156: Add `java.lang.Comparable` in set of "unsafe" polymorphic
19371937
base types
19381938
(2.18.10)
1939+
1940+
@renechoi
1941+
* Reported #6185: Bracket unresolved IPv6 host name in `InetSocketAddress` serialization
1942+
(2.18.11)

‎release-notes/VERSION-2.x‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ Project: jackson-databind
44
=== Releases ===
55
------------------------------------------------------------------------
66

7+
2.18.11 (Not yet released)
8+
9+
#6185: Bracket unresolved IPv6 host name in `InetSocketAddress` serialization
10+
(fix by @pjfanning, w/ Claude code)
11+
712
2.18.10 (15-Aug-2026)
813

914
#6099: Resolve classes without initialization in `TypeFactory.findClass()`

‎src/main/java/com/fasterxml/jackson/databind/ser/std/InetSocketAddressSerializer.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public void serialize(InetSocketAddress value, JsonGenerator g, SerializerProvid
3434
} else { // otherwise use name
3535
str = str.substring(0, ix);
3636
}
37+
} else if ((str.indexOf(':') >= 0) && !str.startsWith("[")) {
38+
// [databind#6185]: unresolved addresses have no InetAddress, but an IPv6
39+
// literal host name still needs bracketing so port remains unambiguous
40+
// (host names stored already-bracketed must not be bracketed twice)
41+
str = "[" + str + "]";
3742
}
3843

3944
g.writeString(str + ":" + value.getPort());

‎src/test/java/com/fasterxml/jackson/databind/ser/jdk/JDKTypeSerializationTest.java‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,24 @@ public void testInetSocketAddress() throws IOException
142142
MAPPER.writeValueAsString(new InetSocketAddress("2001:db8:85a3:8d3:1319:8a2e:370:7348", 443)));
143143
}
144144

145+
// [databind#6185]: unresolved IPv6 literal must be bracketed like resolved one
146+
@Test
147+
public void testInetSocketAddressIPv6Unresolved() throws IOException
148+
{
149+
assertEquals(q("[2001:db8::1]:443"),
150+
MAPPER.writeValueAsString(InetSocketAddress.createUnresolved("2001:db8::1", 443)));
151+
// if host name is stored with brackets, must not bracket twice
152+
assertEquals(q("[2001:db8::1]:443"),
153+
MAPPER.writeValueAsString(InetSocketAddress.createUnresolved("[2001:db8::1]", 443)));
154+
// and IPv6 value read by Jackson must survive a full round-trip with port intact
155+
InetSocketAddress addr = MAPPER.readValue(q("[2001:db8::1]:443"), InetSocketAddress.class);
156+
String json = MAPPER.writeValueAsString(addr);
157+
assertEquals(q("[2001:db8::1]:443"), json);
158+
InetSocketAddress result = MAPPER.readValue(json, InetSocketAddress.class);
159+
assertEquals(addr.getHostName(), result.getHostName());
160+
assertEquals(443, result.getPort());
161+
}
162+
145163
// [JACKSON-597]
146164
@Test
147165
public void testClass() throws IOException

0 commit comments

Comments
 (0)