Skip to content

Commit

Permalink
Add test and fix for stack overflow when decoding geometry
Browse files Browse the repository at this point in the history
Signed-off-by: Geir Sagberg <[email protected]>

[resolves #240]
  • Loading branch information
geirsagberg committed Apr 24, 2023
1 parent d54de8a commit 3531c15
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 30 deletions.
62 changes: 32 additions & 30 deletions src/main/java/io/r2dbc/h2/codecs/DefaultCodecs.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,39 +113,41 @@ public Class<?> preferredType(int dataType) {
*/
static List<Codec<?>> createCodecs(Client client, ClassLoader classLoader, Codecs codecs) {
return Stream.concat(
Stream.concat(
Stream.of(
new BigDecimalCodec(),
new BlobToByteBufferCodec(client),
new BlobCodec(client),
new BooleanCodec(),
new ByteCodec(),
new BytesCodec(),
new ClobToStringCodec(client),
new ClobCodec(client),
new DoubleCodec(),
new FloatCodec(),
new IntegerCodec(),
new JsonCodec(),
new LocalDateCodec(),
new LocalDateTimeCodec(client),
new LocalTimeCodec(),
new LongCodec(),
new OffsetDateTimeCodec(client),
new OffsetTimeCodec(client),
new ShortCodec(),
new StringCodec(),
new UuidCodec(),
new ZonedDateTimeCodec(client),
new InstantCodec(client),
new IntervalCodec(),
new PeriodCodec(),
new DurationCodec()
),
addOptionalCodecs(classLoader)),
Stream.of(
new BigDecimalCodec(),
new BlobToByteBufferCodec(client),
new BlobCodec(client),
new BooleanCodec(),
new ByteCodec(),
new BytesCodec(),
new ClobToStringCodec(client),
new ClobCodec(client),
new DoubleCodec(),
new FloatCodec(),
new IntegerCodec(),
new JsonCodec(),
new LocalDateCodec(),
new LocalDateTimeCodec(client),
new LocalTimeCodec(),
new LongCodec(),
new OffsetDateTimeCodec(client),
new OffsetTimeCodec(client),
new ShortCodec(),
new StringCodec(),
new UuidCodec(),
new ZonedDateTimeCodec(client),
new InstantCodec(client),
new IntervalCodec(),
new PeriodCodec(),
new DurationCodec(),

// De-prioritized codecs
// De-prioritized codecs, must be added after optional codecs to avoid stack overflow
new ArrayCodec(codecs),
new ParameterCodec(codecs)
),
addOptionalCodecs(classLoader)
)
).collect(Collectors.toList());
}

Expand Down
14 changes: 14 additions & 0 deletions src/test/java/io/r2dbc/h2/codecs/DefaultCodecsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import io.r2dbc.h2.client.Client;
import org.h2.value.Value;
import org.h2.value.ValueGeometry;
import org.h2.value.ValueInteger;
import org.h2.value.ValueNull;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -58,6 +59,19 @@ void addOptionalCodecsGeometryNotFound() throws Exception {
assertThat(result).isEqualTo(0L);
}

@Test
void canDecodeGeometry() throws Exception {
ClassLoader mockClassLoader = mock(ClassLoader.class);
willReturn(Object.class)
.given(mockClassLoader)
.loadClass(eq("org.locationtech.jts.geom.Geometry"));
ValueGeometry value = ValueGeometry.get("POINT(1 2)");

DefaultCodecs defaultCodecs = new DefaultCodecs(mock(Client.class));

assertThat(defaultCodecs.decode(value, value.getValueType(), Object.class)).isEqualTo(value.getGeometry());
}

@Test
void createCodecsWithNonOptionalCodecsAndNoDuplicates() throws Exception {
ClassLoader mockClassLoader = mock(ClassLoader.class);
Expand Down

0 comments on commit 3531c15

Please sign in to comment.