From 8018584e755a15442406033b48a56b4c878b71bb Mon Sep 17 00:00:00 2001 From: Sergey Starosek Date: Tue, 5 Apr 2022 10:03:47 +0300 Subject: [PATCH] Add `sslMode` option lowercase alias For easier JDBC URL adaption [resolves #486] --- .../PostgresqlConnectionFactoryProvider.java | 18 +++++++++++++++++- ...esqlConnectionFactoryProviderUnitTests.java | 15 +++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/r2dbc/postgresql/PostgresqlConnectionFactoryProvider.java b/src/main/java/io/r2dbc/postgresql/PostgresqlConnectionFactoryProvider.java index 6c163bb5..b2578bae 100644 --- a/src/main/java/io/r2dbc/postgresql/PostgresqlConnectionFactoryProvider.java +++ b/src/main/java/io/r2dbc/postgresql/PostgresqlConnectionFactoryProvider.java @@ -180,6 +180,11 @@ public final class PostgresqlConnectionFactoryProvider implements ConnectionFact */ public static final Option SSL_MODE = Option.valueOf("sslMode"); + /** + * Ssl mode alias (JDBC style). Default: disabled + */ + public static final Option SSL_MODE_ALIAS = Option.valueOf("sslmode"); + /** * SSL key password */ @@ -301,7 +306,18 @@ private static void setupSsl(PostgresqlConnectionConfiguration.Builder builder, return (SSLMode) it; - }).to(builder::sslMode); + }).to(builder::sslMode).otherwise(() -> { + + mapper.from(SSL_MODE_ALIAS).map(it -> { + + if (it instanceof String) { + return SSLMode.fromValue(it.toString()); + } + + return (SSLMode) it; + + }).to(builder::sslMode); + }); mapper.fromTyped(SSL_CERT).to(builder::sslCert); mapper.fromTyped(SSL_CONTEXT_BUILDER_CUSTOMIZER).to(builder::sslContextBuilderCustomizer); diff --git a/src/test/java/io/r2dbc/postgresql/PostgresqlConnectionFactoryProviderUnitTests.java b/src/test/java/io/r2dbc/postgresql/PostgresqlConnectionFactoryProviderUnitTests.java index 0e35becd..3ccaeb91 100644 --- a/src/test/java/io/r2dbc/postgresql/PostgresqlConnectionFactoryProviderUnitTests.java +++ b/src/test/java/io/r2dbc/postgresql/PostgresqlConnectionFactoryProviderUnitTests.java @@ -225,6 +225,21 @@ void supportsSslMode() { assertThat(sslConfig.getSslMode()).isEqualTo(SSLMode.DISABLE); } + @Test + void supportsSslModeAlias() { + PostgresqlConnectionFactory factory = this.provider.create(builder() + .option(DRIVER, POSTGRESQL_DRIVER) + .option(HOST, "test-host") + .option(PASSWORD, "test-password") + .option(USER, "test-user") + .option(Option.valueOf("sslmode"), "require") + .build()); + + SSLConfig sslConfig = factory.getConfiguration().getSslConfig(); + + assertThat(sslConfig.getSslMode()).isEqualTo(SSLMode.REQUIRE); + } + @Test void shouldCreateConnectionFactoryWithoutPassword() { assertThat(this.provider.create(ConnectionFactoryOptions.builder()