Skip to content

Commit be4fe85

Browse files
jbellassaimp911de
authored andcommitted
Fall back to the VARCHAR codec if decoding as a String
If there is no immediately suitable codec can be found and the request is to decode as a String, then fallback to using the VARCHAR codec. [#454][#429]
1 parent 285d6b7 commit be4fe85

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

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

+6
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,12 @@ public <T> T decode(@Nullable ByteBuf buffer, int dataType, Format format, Class
200200
Codec<T> codec = this.codecLookup.findDecodeCodec(dataType, format, type);
201201
if (codec != null) {
202202
return codec.decode(buffer, dataType, format, type);
203+
} else if (String.class == type) {
204+
int varcharType = PostgresqlObjectId.VARCHAR.getObjectId();
205+
codec = this.codecLookup.findDecodeCodec(varcharType, format, type);
206+
if (codec != null) {
207+
return codec.decode(buffer, varcharType, format, type);
208+
}
203209
}
204210

205211
throw new IllegalArgumentException(String.format("Cannot decode value of type %s with OID %d", type.getName(), dataType));

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

+10
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,16 @@ void decodeUnsupportedType() {
121121
.withMessage("Cannot decode value of type java.lang.Void with OID 23");
122122
}
123123

124+
@Test
125+
void decodeFallbackToVarcharCodec() {
126+
assertThat(this.codecs.decode(ByteBufUtils.encode(TEST, "2018-11-04 15:35:00.847108"), TIMESTAMP.getObjectId(), FORMAT_TEXT, String.class))
127+
.isEqualTo("2018-11-04 15:35:00.847108");
128+
assertThat(this.codecs.decode(ByteBufUtils.encode(TEST, "2018-11-05 00:35:43.048593+09"), TIMESTAMPTZ.getObjectId(), FORMAT_TEXT, String.class))
129+
.isEqualTo("2018-11-05 00:35:43.048593+09");
130+
assertThat(this.codecs.decode(ByteBufUtils.encode(TEST, "ENUM_VALUE"), 123456, FORMAT_TEXT, String.class))
131+
.isEqualTo("ENUM_VALUE");
132+
}
133+
124134
@Test
125135
void delegatePriority() {
126136
assertThat(this.codecs.decode(TEST.buffer(2).writeShort((byte) 100), INT2.getObjectId(), FORMAT_BINARY, Object.class)).isInstanceOf(Short.class);

0 commit comments

Comments
 (0)