Skip to content

Commit 3672c30

Browse files
committed
Skip EnumCodec registration if the builder has no mappings.
Also, improve PostgresTypes safety if the collection of type names is empty. [resolves #515] Signed-off-by: Mark Paluch <[email protected]>
1 parent 829231f commit 3672c30

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,10 @@ public CodecRegistrar build() {
212212

213213
Map<String, Class<? extends Enum<?>>> mapping = new LinkedHashMap<>(this.mapping);
214214

215+
if (mapping.isEmpty()) {
216+
return (connection, allocator, registry) -> Mono.empty();
217+
}
218+
215219
return (connection, allocator, registry) -> {
216220

217221
List<String> missing = new ArrayList<>(mapping.keySet());

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

+7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import java.util.Objects;
2727
import java.util.StringJoiner;
28+
import java.util.concurrent.atomic.AtomicBoolean;
2829
import java.util.regex.Pattern;
2930

3031
/**
@@ -85,16 +86,22 @@ public Mono<PostgresType> lookupType(String typeName) {
8586
public Flux<PostgresType> lookupTypes(Iterable<String> typeNames) {
8687

8788
StringJoiner joiner = new StringJoiner(",", "(", ")");
89+
AtomicBoolean hasType = new AtomicBoolean();
8890

8991
typeNames.forEach(typeName -> {
9092

9193
if (!TYPENAME.matcher(Assert.requireNonNull(typeName, "typeName must not be null")).matches()) {
9294
throw new IllegalArgumentException(String.format("Invalid typename %s", typeName));
9395
}
9496

97+
hasType.set(true);
9598
joiner.add("'" + typeName + "'");
9699
});
97100

101+
if (!hasType.get()) {
102+
return Flux.empty();
103+
}
104+
98105
return this.connection.createStatement(String.format(SELECT_PG_TYPE, "IN", joiner, "")).execute()
99106
.flatMap(it -> it.map((row, rowMetadata) -> {
100107

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

+18
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,24 @@ protected void customize(PostgresqlConnectionConfiguration.Builder builder) {
5454
builder.codecRegistrar(EnumCodec.builder().withEnum("my_enum_with_codec", MyEnum.class).build());
5555
}
5656

57+
@Test
58+
void shouldNotRegisterIfEmpty() {
59+
60+
PostgresqlConnectionConfiguration configuration = PostgresqlConnectionConfiguration.builder()
61+
.database(SERVER.getDatabase())
62+
.host(SERVER.getHost())
63+
.port(SERVER.getPort())
64+
.password(SERVER.getPassword())
65+
.username(SERVER.getUsername())
66+
.codecRegistrar(EnumCodec.builder().build())
67+
.build();
68+
69+
PostgresqlConnectionFactory connectionFactory = new PostgresqlConnectionFactory(configuration);
70+
connectionFactory.create().flatMap(PostgresqlConnection::close).as(StepVerifier::create).verifyComplete();
71+
72+
// we cannot really assert logs so that's up to you.
73+
}
74+
5775
@Test
5876
void shouldReportUnresolvableTypes() {
5977

0 commit comments

Comments
 (0)