Add nullability annotations to module/spring-boot-elasticsearch

See gh-46587
This commit is contained in:
Moritz Halbritter
2025-07-30 14:20:39 +02:00
parent b8f74b1a11
commit 5ac6dfcffb
11 changed files with 58 additions and 47 deletions
@@ -20,6 +20,8 @@ import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import org.jspecify.annotations.Nullable;
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails;
import org.springframework.boot.ssl.SslBundle;
@@ -43,7 +45,7 @@ public interface ElasticsearchConnectionDetails extends ConnectionDetails {
* Username for authentication with Elasticsearch.
* @return username for authentication with Elasticsearch or {@code null}
*/
default String getUsername() {
default @Nullable String getUsername() {
return null;
}
@@ -51,7 +53,7 @@ public interface ElasticsearchConnectionDetails extends ConnectionDetails {
* Password for authentication with Elasticsearch.
* @return password for authentication with Elasticsearch or {@code null}
*/
default String getPassword() {
default @Nullable String getPassword() {
return null;
}
@@ -60,7 +62,7 @@ public interface ElasticsearchConnectionDetails extends ConnectionDetails {
* @return prefix added to the path of every request sent to Elasticsearch or
* {@code null}
*/
default String getPathPrefix() {
default @Nullable String getPathPrefix() {
return null;
}
@@ -68,7 +70,7 @@ public interface ElasticsearchConnectionDetails extends ConnectionDetails {
* SSL bundle to use.
* @return the SSL bundle to use
*/
default SslBundle getSslBundle() {
default @Nullable SslBundle getSslBundle() {
return null;
}
@@ -81,7 +83,8 @@ public interface ElasticsearchConnectionDetails extends ConnectionDetails {
* @param username the username or {@code null}
* @param password the password or {@code null}
*/
record Node(String hostname, int port, Node.Protocol protocol, String username, String password) {
record Node(String hostname, int port, Node.Protocol protocol, @Nullable String username,
@Nullable String password) {
public Node(String host, int port, Node.Protocol protocol) {
this(host, port, protocol, null, null);
@@ -96,7 +99,7 @@ public interface ElasticsearchConnectionDetails extends ConnectionDetails {
}
}
private String userInfo() {
private @Nullable String userInfo() {
if (this.username == null) {
return null;
}
@@ -21,6 +21,8 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.jspecify.annotations.Nullable;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
@@ -40,12 +42,12 @@ public class ElasticsearchProperties {
/**
* Username for authentication with Elasticsearch.
*/
private String username;
private @Nullable String username;
/**
* Password for authentication with Elasticsearch.
*/
private String password;
private @Nullable String password;
/**
* Connection timeout used when communicating with Elasticsearch.
@@ -65,7 +67,7 @@ public class ElasticsearchProperties {
/**
* Prefix added to the path of every request sent to Elasticsearch.
*/
private String pathPrefix;
private @Nullable String pathPrefix;
private final Restclient restclient = new Restclient();
@@ -77,19 +79,19 @@ public class ElasticsearchProperties {
this.uris = uris;
}
public String getUsername() {
public @Nullable String getUsername() {
return this.username;
}
public void setUsername(String username) {
public void setUsername(@Nullable String username) {
this.username = username;
}
public String getPassword() {
public @Nullable String getPassword() {
return this.password;
}
public void setPassword(String password) {
public void setPassword(@Nullable String password) {
this.password = password;
}
@@ -117,11 +119,11 @@ public class ElasticsearchProperties {
this.socketKeepAlive = socketKeepAlive;
}
public String getPathPrefix() {
public @Nullable String getPathPrefix() {
return this.pathPrefix;
}
public void setPathPrefix(String pathPrefix) {
public void setPathPrefix(@Nullable String pathPrefix) {
this.pathPrefix = pathPrefix;
}
@@ -178,13 +180,13 @@ public class ElasticsearchProperties {
/**
* SSL bundle name.
*/
private String bundle;
private @Nullable String bundle;
public String getBundle() {
public @Nullable String getBundle() {
return this.bundle;
}
public void setBundle(String bundle) {
public void setBundle(@Nullable String bundle) {
this.bundle = bundle;
}
@@ -37,6 +37,7 @@ import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.sniff.Sniffer;
import org.elasticsearch.client.sniff.SnifferBuilder;
import org.jspecify.annotations.Nullable;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@@ -209,7 +210,7 @@ class ElasticsearchRestClientConfigurations {
return connectionDetails.getNodes().stream().map(Node::toUri);
}
private boolean hasUserInfo(URI uri) {
private boolean hasUserInfo(@Nullable URI uri) {
return uri != null && StringUtils.hasLength(uri.getUserInfo());
}
@@ -238,9 +239,9 @@ class ElasticsearchRestClientConfigurations {
private final ElasticsearchProperties properties;
private final SslBundles sslBundles;
private final @Nullable SslBundles sslBundles;
PropertiesElasticsearchConnectionDetails(ElasticsearchProperties properties, SslBundles sslBundles) {
PropertiesElasticsearchConnectionDetails(ElasticsearchProperties properties, @Nullable SslBundles sslBundles) {
this.properties = properties;
this.sslBundles = sslBundles;
}
@@ -251,22 +252,22 @@ class ElasticsearchRestClientConfigurations {
}
@Override
public String getUsername() {
public @Nullable String getUsername() {
return this.properties.getUsername();
}
@Override
public String getPassword() {
public @Nullable String getPassword() {
return this.properties.getPassword();
}
@Override
public String getPathPrefix() {
public @Nullable String getPathPrefix() {
return this.properties.getPathPrefix();
}
@Override
public SslBundle getSslBundle() {
public @Nullable SslBundle getSslBundle() {
Ssl ssl = this.properties.getRestclient().getSsl();
if (StringUtils.hasLength(ssl.getBundle())) {
Assert.notNull(this.sslBundles, "SSL bundle name has been set but no SSL bundles found in context");
@@ -17,4 +17,7 @@
/**
* Auto-configuration for Elasticsearch health.
*/
@NullMarked
package org.springframework.boot.elasticsearch.autoconfigure.health;
import org.jspecify.annotations.NullMarked;
@@ -17,4 +17,7 @@
/**
* Auto-configuration for Elasticsearch client.
*/
@NullMarked
package org.springframework.boot.elasticsearch.autoconfigure;
import org.jspecify.annotations.NullMarked;
@@ -18,6 +18,8 @@ package org.springframework.boot.elasticsearch.docker.compose;
import java.util.List;
import org.jspecify.annotations.Nullable;
import org.springframework.boot.docker.compose.core.RunningService;
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionDetailsFactory;
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionSource;
@@ -73,7 +75,7 @@ class ElasticsearchDockerComposeConnectionDetailsFactory
}
@Override
public String getPassword() {
public @Nullable String getPassword() {
return this.environment.getPassword();
}
@@ -18,6 +18,8 @@ package org.springframework.boot.elasticsearch.docker.compose;
import java.util.Map;
import org.jspecify.annotations.Nullable;
import org.springframework.util.Assert;
/**
@@ -29,14 +31,14 @@ import org.springframework.util.Assert;
*/
class ElasticsearchEnvironment {
private final String password;
private final @Nullable String password;
ElasticsearchEnvironment(Map<String, String> env) {
Assert.state(!env.containsKey("ELASTIC_PASSWORD_FILE"), "ELASTIC_PASSWORD_FILE is not supported");
this.password = env.get("ELASTIC_PASSWORD");
}
String getPassword() {
@Nullable String getPassword() {
return this.password;
}
@@ -17,4 +17,7 @@
/**
* Support for Docker Compose Elasticsearch service connections.
*/
@NullMarked
package org.springframework.boot.elasticsearch.docker.compose;
import org.jspecify.annotations.NullMarked;
@@ -17,4 +17,7 @@
/**
* Health integration for Elasticsearch.
*/
@NullMarked
package org.springframework.boot.elasticsearch.health;
import org.jspecify.annotations.NullMarked;
@@ -26,6 +26,7 @@ import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.util.List;
import org.jspecify.annotations.Nullable;
import org.testcontainers.elasticsearch.ElasticsearchContainer;
import org.springframework.boot.elasticsearch.autoconfigure.ElasticsearchConnectionDetails;
@@ -64,7 +65,7 @@ class ElasticsearchContainerConnectionDetailsFactory
private static final class ElasticsearchContainerConnectionDetails
extends ContainerConnectionDetails<ElasticsearchContainer> implements ElasticsearchConnectionDetails {
private volatile SslBundle sslBundle;
private volatile @Nullable SslBundle sslBundle;
private ElasticsearchContainerConnectionDetails(ContainerConnectionSource<ElasticsearchContainer> source) {
super(source);
@@ -76,7 +77,7 @@ class ElasticsearchContainerConnectionDetailsFactory
}
@Override
public String getPassword() {
public @Nullable String getPassword() {
return getContainer().getEnvMap().get("ELASTIC_PASSWORD");
}
@@ -89,7 +90,7 @@ class ElasticsearchContainerConnectionDetailsFactory
}
@Override
public SslBundle getSslBundle() {
public @Nullable SslBundle getSslBundle() {
if (this.sslBundle != null) {
return this.sslBundle;
}
@@ -111,22 +112,7 @@ class ElasticsearchContainerConnectionDetailsFactory
}
private SslBundle createSslBundleWithTrustStore(KeyStore trustStore) {
return SslBundle.of(new SslStoreBundle() {
@Override
public KeyStore getKeyStore() {
return null;
}
@Override
public String getKeyStorePassword() {
return null;
}
@Override
public KeyStore getTrustStore() {
return trustStore;
}
});
return SslBundle.of(SslStoreBundle.of(null, null, trustStore));
}
private KeyStore createTrustStore(byte[] caCertificate) {
@@ -17,4 +17,7 @@
/**
* Support for testcontainers Elasticsearch service connections.
*/
@NullMarked
package org.springframework.boot.elasticsearch.testcontainers;
import org.jspecify.annotations.NullMarked;