From cb58f10116994d973f9158fd68259d9e5926da62 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Wed, 5 Feb 2025 13:06:46 +0000 Subject: [PATCH] Allow default channel address to be carried through --- .../autoconfigure/client/GrpcClientProperties.java | 10 +++++++++- .../client/GrpcClientPropertiesTests.java | 13 +++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/client/GrpcClientProperties.java b/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/client/GrpcClientProperties.java index 19e062d..8770e81 100644 --- a/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/client/GrpcClientProperties.java +++ b/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/client/GrpcClientProperties.java @@ -83,7 +83,15 @@ public class GrpcClientProperties implements EnvironmentAware, VirtualTargets { channel = this.defaultChannel.copy(); String address = name; if (!name.contains(":/") && !name.startsWith("unix:")) { - address = "static://" + name; + if (name.contains(":")) { + address = "static://" + name; + } + else { + address = defaultChannel.getAddress(); + if (!address.contains(":/")) { + address = "static://" + address; + } + } } channel.setAddress(address); return channel; diff --git a/spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/autoconfigure/client/GrpcClientPropertiesTests.java b/spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/autoconfigure/client/GrpcClientPropertiesTests.java index 0a47b34..1e01319 100644 --- a/spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/autoconfigure/client/GrpcClientPropertiesTests.java +++ b/spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/autoconfigure/client/GrpcClientPropertiesTests.java @@ -27,7 +27,6 @@ import java.util.function.Function; import org.assertj.core.api.InstanceOfAssertFactories; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; - import org.springframework.boot.context.properties.bind.Binder; import org.springframework.boot.context.properties.source.MapConfigurationPropertySource; import org.springframework.grpc.autoconfigure.client.GrpcClientProperties.ChannelConfig; @@ -213,8 +212,18 @@ class GrpcClientPropertiesTests { defaultChannel.getSsl().setBundle("custom-bundle"); var properties = newProperties(defaultChannel, Map.of()); var newChannel = properties.getChannel("new-channel"); + assertThat(newChannel).usingRecursiveComparison().isEqualTo(defaultChannel); + assertThat(properties).extracting("channels", InstanceOfAssertFactories.MAP).isEmpty(); + } + + @Test + void withUnknownNameReturnsNewChannelWithOwnAddress() { + var defaultChannel = new ChannelConfig(); + defaultChannel.setAddress("static://my-server:9999"); + var properties = newProperties(defaultChannel, Map.of()); + var newChannel = properties.getChannel("other-server:8888"); assertThat(newChannel).usingRecursiveComparison().ignoringFields("address").isEqualTo(defaultChannel); - assertThat(newChannel).hasFieldOrPropertyWithValue("address", "static://new-channel"); + assertThat(newChannel).hasFieldOrPropertyWithValue("address", "static://other-server:8888"); assertThat(properties).extracting("channels", InstanceOfAssertFactories.MAP).isEmpty(); }