From a6b4400de7c17053ce133c2063cd6b5b79758cdd Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Wed, 10 Sep 2025 11:59:20 +0200 Subject: [PATCH] Improve null-safety of core/spring-boot-docker-compose See gh-46926 --- .../compose/core/DockerCliInspectResponse.java | 2 +- .../boot/docker/compose/core/DockerEnv.java | 4 ++-- .../boot/docker/compose/core/DockerHost.java | 6 +++--- .../lifecycle/DockerComposeLifecycleManager.java | 9 ++++----- .../compose/lifecycle/DockerComposeListener.java | 3 +-- .../DockerComposeLifecycleManagerTests.java | 12 ++++++------ 6 files changed, 17 insertions(+), 19 deletions(-) diff --git a/core/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DockerCliInspectResponse.java b/core/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DockerCliInspectResponse.java index d26220ec83b..67fe6664b5b 100644 --- a/core/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DockerCliInspectResponse.java +++ b/core/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DockerCliInspectResponse.java @@ -33,7 +33,7 @@ import org.jspecify.annotations.Nullable; * @author Phillip Webb */ record DockerCliInspectResponse(String id, DockerCliInspectResponse.Config config, - DockerCliInspectResponse.NetworkSettings networkSettings, + DockerCliInspectResponse.@Nullable NetworkSettings networkSettings, DockerCliInspectResponse.@Nullable HostConfig hostConfig) { /** diff --git a/core/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DockerEnv.java b/core/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DockerEnv.java index 457d81a1694..ffd93cc0d7b 100644 --- a/core/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DockerEnv.java +++ b/core/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DockerEnv.java @@ -40,11 +40,11 @@ class DockerEnv { * Create a new {@link DockerEnv} instance. * @param env a list of env entries in the form {@code name=value} or {@code name}. */ - DockerEnv(List env) { + DockerEnv(@Nullable List env) { this.map = parse(env); } - private Map parse(List env) { + private Map parse(@Nullable List env) { if (CollectionUtils.isEmpty(env)) { return Collections.emptyMap(); } diff --git a/core/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DockerHost.java b/core/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DockerHost.java index 1ede8a5e493..893aa600750 100644 --- a/core/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DockerHost.java +++ b/core/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DockerHost.java @@ -83,7 +83,7 @@ final class DockerHost { * {@link DockerCliContextResponse} * @return a new docker host instance */ - static DockerHost get(@Nullable String host, Function systemEnv, + static DockerHost get(@Nullable String host, Function systemEnv, Supplier> contextsSupplier) { host = (StringUtils.hasText(host)) ? host : fromServicesHostEnv(systemEnv); host = (StringUtils.hasText(host)) ? host : fromDockerHostEnv(systemEnv); @@ -92,11 +92,11 @@ final class DockerHost { return new DockerHost(host); } - private static String fromServicesHostEnv(Function systemEnv) { + private static @Nullable String fromServicesHostEnv(Function systemEnv) { return systemEnv.apply("SERVICES_HOST"); } - private static @Nullable String fromDockerHostEnv(Function systemEnv) { + private static @Nullable String fromDockerHostEnv(Function systemEnv) { return fromEndpoint(systemEnv.apply("DOCKER_HOST")); } diff --git a/core/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/lifecycle/DockerComposeLifecycleManager.java b/core/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/lifecycle/DockerComposeLifecycleManager.java index 430b9afbc59..d21f0ed37a4 100644 --- a/core/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/lifecycle/DockerComposeLifecycleManager.java +++ b/core/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/lifecycle/DockerComposeLifecycleManager.java @@ -27,7 +27,6 @@ import org.jspecify.annotations.Nullable; import org.springframework.aot.AotDetector; import org.springframework.boot.SpringApplicationShutdownHandlers; -import org.springframework.boot.context.properties.bind.Binder; import org.springframework.boot.docker.compose.core.DockerCompose; import org.springframework.boot.docker.compose.core.DockerComposeFile; import org.springframework.boot.docker.compose.core.RunningService; @@ -74,14 +73,14 @@ class DockerComposeLifecycleManager { private final ServiceReadinessChecks serviceReadinessChecks; - DockerComposeLifecycleManager(ApplicationContext applicationContext, Binder binder, + DockerComposeLifecycleManager(ApplicationContext applicationContext, SpringApplicationShutdownHandlers shutdownHandlers, DockerComposeProperties properties, Set> eventListeners) { - this(null, applicationContext, binder, shutdownHandlers, properties, eventListeners, - new DockerComposeSkipCheck(), null); + this(null, applicationContext, shutdownHandlers, properties, eventListeners, new DockerComposeSkipCheck(), + null); } - DockerComposeLifecycleManager(@Nullable File workingDirectory, ApplicationContext applicationContext, Binder binder, + DockerComposeLifecycleManager(@Nullable File workingDirectory, ApplicationContext applicationContext, SpringApplicationShutdownHandlers shutdownHandlers, DockerComposeProperties properties, Set> eventListeners, DockerComposeSkipCheck skipCheck, @Nullable ServiceReadinessChecks serviceReadinessChecks) { diff --git a/core/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/lifecycle/DockerComposeListener.java b/core/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/lifecycle/DockerComposeListener.java index 4a1d4d59ecb..302ac1e118c 100644 --- a/core/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/lifecycle/DockerComposeListener.java +++ b/core/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/lifecycle/DockerComposeListener.java @@ -56,8 +56,7 @@ class DockerComposeListener implements ApplicationListener> eventListeners) { - return new DockerComposeLifecycleManager(applicationContext, binder, this.shutdownHandlers, properties, - eventListeners); + return new DockerComposeLifecycleManager(applicationContext, this.shutdownHandlers, properties, eventListeners); } } diff --git a/core/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/lifecycle/DockerComposeLifecycleManagerTests.java b/core/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/lifecycle/DockerComposeLifecycleManagerTests.java index 2fef8d74969..6207a664498 100644 --- a/core/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/lifecycle/DockerComposeLifecycleManagerTests.java +++ b/core/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/lifecycle/DockerComposeLifecycleManagerTests.java @@ -109,7 +109,7 @@ class DockerComposeLifecycleManagerTests { this.eventListeners = new LinkedHashSet<>(); this.skipCheck = mock(DockerComposeSkipCheck.class); this.serviceReadinessChecks = mock(ServiceReadinessChecks.class); - this.lifecycleManager = new TestDockerComposeLifecycleManager(workingDirectory, this.applicationContext, binder, + this.lifecycleManager = new TestDockerComposeLifecycleManager(workingDirectory, this.applicationContext, this.shutdownHandlers, this.properties, this.eventListeners, this.skipCheck, this.serviceReadinessChecks); } @@ -152,15 +152,15 @@ class DockerComposeLifecycleManagerTests { @Test void startWhenComposeFileNotFoundThrowsException() { DockerComposeLifecycleManager manager = new DockerComposeLifecycleManager(new File("."), - this.applicationContext, null, this.shutdownHandlers, this.properties, this.eventListeners, - this.skipCheck, this.serviceReadinessChecks); + this.applicationContext, this.shutdownHandlers, this.properties, this.eventListeners, this.skipCheck, + this.serviceReadinessChecks); assertThatIllegalStateException().isThrownBy(manager::start) .withMessageContaining(Paths.get(".").toAbsolutePath().toString()); } @Test void startWhenComposeFileNotFoundAndWorkingDirectoryNullThrowsException() { - DockerComposeLifecycleManager manager = new DockerComposeLifecycleManager(null, this.applicationContext, null, + DockerComposeLifecycleManager manager = new DockerComposeLifecycleManager(null, this.applicationContext, this.shutdownHandlers, this.properties, this.eventListeners, this.skipCheck, this.serviceReadinessChecks); assertThatIllegalStateException().isThrownBy(manager::start) @@ -516,11 +516,11 @@ class DockerComposeLifecycleManagerTests { */ class TestDockerComposeLifecycleManager extends DockerComposeLifecycleManager { - TestDockerComposeLifecycleManager(File workingDirectory, ApplicationContext applicationContext, Binder binder, + TestDockerComposeLifecycleManager(File workingDirectory, ApplicationContext applicationContext, SpringApplicationShutdownHandlers shutdownHandlers, DockerComposeProperties properties, Set> eventListeners, DockerComposeSkipCheck skipCheck, ServiceReadinessChecks serviceReadinessChecks) { - super(workingDirectory, applicationContext, binder, shutdownHandlers, properties, eventListeners, skipCheck, + super(workingDirectory, applicationContext, shutdownHandlers, properties, eventListeners, skipCheck, serviceReadinessChecks); }