Skip to content

Commit

Permalink
Skip EnumCodec registration if the builder has no mappings.
Browse files Browse the repository at this point in the history
Also, improve PostgresTypes safety if the collection of type names is empty.

[resolves #515]

Signed-off-by: Mark Paluch <mpaluch@vmware.com>
  • Loading branch information
mp911de committed May 24, 2022
1 parent a3611bc commit 50e3dad
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/main/java/io/r2dbc/postgresql/codec/EnumCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import io.r2dbc.postgresql.client.Parameter;
import io.r2dbc.postgresql.extension.CodecRegistrar;
import io.r2dbc.postgresql.message.Format;
import io.r2dbc.postgresql.type.PostgresqlObjectId;
import io.r2dbc.postgresql.util.Assert;
import io.r2dbc.postgresql.util.ByteBufUtils;
import reactor.core.publisher.Mono;
Expand All @@ -30,7 +29,6 @@
import reactor.util.annotation.Nullable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -168,6 +166,10 @@ public CodecRegistrar build() {

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

if (mapping.isEmpty()) {
return (connection, allocator, registry) -> Mono.empty();
}

return (connection, allocator, registry) -> {

List<String> missing = new ArrayList<>(mapping.keySet());
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/io/r2dbc/postgresql/codec/PostgresTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import java.util.Objects;
import java.util.StringJoiner;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Pattern;

/**
Expand Down Expand Up @@ -82,16 +83,22 @@ public Mono<PostgresType> lookupType(String typeName) {
public Flux<PostgresType> lookupTypes(Iterable<String> typeNames) {

StringJoiner joiner = new StringJoiner(",", "(", ")");
AtomicBoolean hasType = new AtomicBoolean();

typeNames.forEach(typeName -> {

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

hasType.set(true);
joiner.add("'" + typeName + "'");
});

if (!hasType.get()) {
return Flux.empty();
}

return this.connection.createStatement(String.format(SELECT_PG_TYPE, "IN", joiner, "")).execute()
.flatMap(it -> it.map((row, rowMetadata) -> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,24 @@ protected void customize(PostgresqlConnectionConfiguration.Builder builder) {
builder.codecRegistrar(EnumCodec.builder().withEnum("my_enum_with_codec", MyEnum.class).build());
}

@Test
void shouldNotRegisterIfEmpty() {

PostgresqlConnectionConfiguration configuration = PostgresqlConnectionConfiguration.builder()
.database(SERVER.getDatabase())
.host(SERVER.getHost())
.port(SERVER.getPort())
.password(SERVER.getPassword())
.username(SERVER.getUsername())
.codecRegistrar(EnumCodec.builder().build())
.build();

PostgresqlConnectionFactory connectionFactory = new PostgresqlConnectionFactory(configuration);
connectionFactory.create().flatMap(PostgresqlConnection::close).as(StepVerifier::create).verifyComplete();

// we cannot really assert logs so that's up to you.
}

@Test
void shouldReportUnresolvableTypes() {

Expand Down

0 comments on commit 50e3dad

Please sign in to comment.