Skip to content

Commit a33414c

Browse files
committed
Properly implement InetAddressCodec for inet type
InetAddressCodec now only considers the inet type for encoding and decoding values. Previously, the codec served also BPCHAR and VARCHAR types which caused decoding exceptions. [resolves #184]
1 parent 16f508c commit a33414c

File tree

5 files changed

+37
-19
lines changed

5 files changed

+37
-19
lines changed

src/main/java/io/r2dbc/postgresql/codec/InetAddressCodec.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929
import java.net.UnknownHostException;
3030

3131
import static io.r2dbc.postgresql.message.Format.FORMAT_TEXT;
32-
import static io.r2dbc.postgresql.type.PostgresqlObjectId.BPCHAR;
33-
import static io.r2dbc.postgresql.type.PostgresqlObjectId.VARCHAR;
32+
import static io.r2dbc.postgresql.type.PostgresqlObjectId.INET;
3433

3534
final class InetAddressCodec extends AbstractCodec<InetAddress> {
3635

@@ -43,23 +42,37 @@ final class InetAddressCodec extends AbstractCodec<InetAddress> {
4342

4443
@Override
4544
public Parameter encodeNull() {
46-
return createNull(VARCHAR, FORMAT_TEXT);
45+
return createNull(INET, FORMAT_TEXT);
4746
}
4847

4948
@Override
5049
boolean doCanDecode(PostgresqlObjectId type, Format format) {
5150
Assert.requireNonNull(format, "format must not be null");
5251
Assert.requireNonNull(type, "type must not be null");
5352

54-
return BPCHAR == type || VARCHAR == type;
53+
return INET == type;
5554
}
5655

5756
@Override
5857
InetAddress doDecode(ByteBuf buffer, PostgresqlObjectId dataType, @Nullable Format format, @Nullable Class<? extends InetAddress> type) {
5958
Assert.requireNonNull(buffer, "byteBuf must not be null");
6059

6160
try {
62-
return InetAddress.getByName(ByteBufUtils.decode(buffer).trim());
61+
if (format == Format.FORMAT_BINARY) {
62+
63+
int readableBytes = buffer.readableBytes();
64+
if (readableBytes == 8) {
65+
// addr + cidr
66+
buffer.skipBytes(4);
67+
byte[] addr = new byte[4];
68+
buffer.readBytes(addr);
69+
return InetAddress.getByAddress(addr);
70+
}
71+
72+
throw new IllegalArgumentException("Cannot decode InetAddress. Available bytes: " + readableBytes);
73+
}
74+
75+
return InetAddress.getByName(ByteBufUtils.decode(buffer));
6376
} catch (UnknownHostException e) {
6477
throw new IllegalArgumentException(e);
6578
}
@@ -69,7 +82,7 @@ InetAddress doDecode(ByteBuf buffer, PostgresqlObjectId dataType, @Nullable Form
6982
Parameter doEncode(InetAddress value) {
7083
Assert.requireNonNull(value, "value must not be null");
7184

72-
return create(VARCHAR, FORMAT_TEXT, () -> ByteBufUtils.encode(this.byteBufAllocator, value.getHostAddress()));
85+
return create(INET, FORMAT_TEXT, () -> ByteBufUtils.encode(this.byteBufAllocator, value.getHostAddress()));
7386
}
7487

7588
}

src/main/java/io/r2dbc/postgresql/type/PostgresqlObjectId.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ public enum PostgresqlObjectId {
108108
*/
109109
FLOAT8_ARRAY(1022),
110110

111+
/**
112+
* The inet object id.
113+
*/
114+
INET(869),
115+
116+
/**
117+
* The inet array object id.
118+
*/
119+
INET_ARRAY(1041),
120+
111121
/**
112122
* The int2 object id.
113123
*/
@@ -157,7 +167,7 @@ public enum PostgresqlObjectId {
157167
* The JSON array object id.
158168
*/
159169
JSON_ARRAY(199),
160-
170+
161171
/**
162172
* The JSONB array object id.
163173
*/

src/test/java/io/r2dbc/postgresql/AbstractCodecIntegrationTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ void floatPrimitive() {
119119

120120
@Test
121121
void inetAddress() throws UnknownHostException {
122-
testCodec(InetAddress.class, InetAddress.getLocalHost(), "BPCHAR(128)");
123-
testCodec(InetAddress.class, InetAddress.getLocalHost(), "VARCHAR(128)");
122+
testCodec(InetAddress.class, InetAddress.getLocalHost(), "INET");
124123
}
125124

126125
@Test

src/test/java/io/r2dbc/postgresql/codec/CodecIntegrationTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,7 @@ void floatPrimitive() {
157157

158158
@Test
159159
void inetAddress() throws UnknownHostException {
160-
testCodec(InetAddress.class, InetAddress.getLocalHost(), "BPCHAR(128)");
161-
testCodec(InetAddress.class, InetAddress.getLocalHost(), "VARCHAR(128)");
160+
testCodec(InetAddress.class, InetAddress.getLocalHost(), "INET");
162161
}
163162

164163
@Test

src/test/java/io/r2dbc/postgresql/codec/InetAddressCodecTest.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626
import static io.r2dbc.postgresql.client.ParameterAssert.assertThat;
2727
import static io.r2dbc.postgresql.message.Format.FORMAT_BINARY;
2828
import static io.r2dbc.postgresql.message.Format.FORMAT_TEXT;
29-
import static io.r2dbc.postgresql.type.PostgresqlObjectId.BPCHAR;
30-
import static io.r2dbc.postgresql.type.PostgresqlObjectId.FLOAT4;
29+
import static io.r2dbc.postgresql.type.PostgresqlObjectId.INET;
3130
import static io.r2dbc.postgresql.type.PostgresqlObjectId.MONEY;
3231
import static io.r2dbc.postgresql.type.PostgresqlObjectId.VARCHAR;
3332
import static io.r2dbc.postgresql.util.ByteBufUtils.encode;
@@ -37,7 +36,7 @@
3736

3837
final class InetAddressCodecTest {
3938

40-
private static final int dataType = VARCHAR.getObjectId();
39+
private static final int dataType = INET.getObjectId();
4140

4241
@Test
4342
void constructorNoByteBufAllocator() {
@@ -62,10 +61,8 @@ void decodeNoByteBuf() {
6261
void doCanDecode() {
6362
InetAddressCodec codec = new InetAddressCodec(TEST);
6463

65-
assertThat(codec.doCanDecode(VARCHAR, FORMAT_BINARY)).isTrue();
64+
assertThat(codec.doCanDecode(INET, FORMAT_BINARY)).isTrue();
6665
assertThat(codec.doCanDecode(MONEY, FORMAT_TEXT)).isFalse();
67-
assertThat(codec.doCanDecode(BPCHAR, FORMAT_TEXT)).isTrue();
68-
assertThat(codec.doCanDecode(VARCHAR, FORMAT_TEXT)).isTrue();
6966
}
7067

7168
@Test
@@ -86,7 +83,7 @@ void doEncode() throws UnknownHostException {
8683

8784
assertThat(new InetAddressCodec(TEST).doEncode(inetAddress))
8885
.hasFormat(FORMAT_TEXT)
89-
.hasType(VARCHAR.getObjectId())
86+
.hasType(INET.getObjectId())
9087
.hasValue(encode(TEST, inetAddress.getHostAddress()));
9188
}
9289

@@ -99,7 +96,7 @@ void doEncodeNoValue() {
9996
@Test
10097
void encodeNull() {
10198
assertThat(new InetAddressCodec(TEST).encodeNull())
102-
.isEqualTo(new Parameter(FORMAT_TEXT, VARCHAR.getObjectId(), NULL_VALUE));
99+
.isEqualTo(new Parameter(FORMAT_TEXT, INET.getObjectId(), NULL_VALUE));
103100
}
104101

105102
}

0 commit comments

Comments
 (0)