Skip to content

Commit 43c14fa

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 730063e commit 43c14fa

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
@@ -175,6 +175,12 @@ public <T> T decode(@Nullable ByteBuf buffer, int dataType, Format format, Class
175175
Codec<T> codec = this.codecLookup.findDecodeCodec(dataType, format, type);
176176
if (codec != null) {
177177
return codec.decode(buffer, dataType, format, type);
178+
} else if (String.class == type) {
179+
int varcharType = PostgresqlObjectId.VARCHAR.getObjectId();
180+
codec = this.codecLookup.findDecodeCodec(varcharType, format, type);
181+
if (codec != null) {
182+
return codec.decode(buffer, varcharType, format, type);
183+
}
178184
}
179185

180186
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
@@ -113,6 +113,16 @@ void decodeUnsupportedType() {
113113
.withMessage("Cannot decode value of type java.lang.Void with OID 23");
114114
}
115115

116+
@Test
117+
void decodeFallbackToVarcharCodec() {
118+
assertThat(this.codecs.decode(ByteBufUtils.encode(TEST, "2018-11-04 15:35:00.847108"), TIMESTAMP.getObjectId(), FORMAT_TEXT, String.class))
119+
.isEqualTo("2018-11-04 15:35:00.847108");
120+
assertThat(this.codecs.decode(ByteBufUtils.encode(TEST, "2018-11-05 00:35:43.048593+09"), TIMESTAMPTZ.getObjectId(), FORMAT_TEXT, String.class))
121+
.isEqualTo("2018-11-05 00:35:43.048593+09");
122+
assertThat(this.codecs.decode(ByteBufUtils.encode(TEST, "ENUM_VALUE"), 123456, FORMAT_TEXT, String.class))
123+
.isEqualTo("ENUM_VALUE");
124+
}
125+
116126
@Test
117127
void delegatePriority() {
118128
assertThat(codecs.decode(TEST.buffer(2).writeShort((byte) 100), INT2.getObjectId(), FORMAT_BINARY, Object.class)).isInstanceOf(Short.class);

0 commit comments

Comments
 (0)