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 829231f commit 3672c30
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/main/java/io/r2dbc/postgresql/codec/EnumCodec.java
Expand Up @@ -212,6 +212,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
Expand Up @@ -25,6 +25,7 @@

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

/**
Expand Down Expand Up @@ -85,16 +86,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
Expand Up @@ -54,6 +54,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 3672c30

Please sign in to comment.