@@ -418,7 +418,8 @@ public String toString() {
418418 public static class Builder <T extends Builder <T >> {
419419 private ManagedChannel channel ;
420420 private SslContext sslContext ;
421- private Boolean enableHttps ;
421+ private boolean enableHttps ;
422+ private boolean enableHttpsExplicitlySet ;
422423 private String target ;
423424 private Consumer <ManagedChannelBuilder <?>> channelInitializer ;
424425 private Duration healthCheckAttemptTimeout ;
@@ -435,6 +436,7 @@ public static class Builder<T extends Builder<T>> {
435436 private Collection <GrpcMetadataProvider > grpcMetadataProviders ;
436437 private Collection <ClientInterceptor > grpcClientInterceptors ;
437438 private Scope metricsScope ;
439+ private boolean apiKeyProvided ;
438440
439441 protected Builder () {}
440442
@@ -443,6 +445,7 @@ protected Builder(ServiceStubsOptions options) {
443445 this .target = options .target ;
444446 this .channelInitializer = options .channelInitializer ;
445447 this .enableHttps = options .enableHttps ;
448+ this .enableHttpsExplicitlySet = true ;
446449 this .sslContext = options .sslContext ;
447450 this .healthCheckAttemptTimeout = options .healthCheckAttemptTimeout ;
448451 this .healthCheckTimeout = options .healthCheckTimeout ;
@@ -542,6 +545,7 @@ public T setSslContext(SslContext sslContext) {
542545 */
543546 public T setEnableHttps (boolean enableHttps ) {
544547 this .enableHttps = enableHttps ;
548+ this .enableHttpsExplicitlySet = true ;
545549 return self ();
546550 }
547551
@@ -613,6 +617,7 @@ public T addGrpcMetadataProvider(GrpcMetadataProvider grpcMetadataProvider) {
613617 * @return {@code this}
614618 */
615619 public T addApiKey (AuthorizationTokenSupplier apiKey ) {
620+ this .apiKeyProvided = true ;
616621 addGrpcMetadataProvider (
617622 new AuthorizationGrpcMetadataProvider (() -> "Bearer " + apiKey .supply ()));
618623 return self ();
@@ -852,15 +857,11 @@ public ServiceStubsOptions validateAndBuildWithDefaults() {
852857 MoreObjects .firstNonNull (this .grpcClientInterceptors , Collections .emptyList ());
853858
854859 // Auto-enable TLS when API key is provided and TLS is not explicitly set
855- boolean enableHttps = this .enableHttps != null ? this .enableHttps : false ;
856- if (this .enableHttps == null && this .sslContext == null ) {
857- // Check if an API key provider was added
858- boolean hasApiKey =
859- grpcMetadataProviders .stream ()
860- .anyMatch (provider -> provider instanceof AuthorizationGrpcMetadataProvider );
861- if (hasApiKey ) {
862- enableHttps = true ;
863- }
860+ boolean enableHttps ;
861+ if (this .enableHttpsExplicitlySet ) {
862+ enableHttps = this .enableHttps ;
863+ } else {
864+ enableHttps = this .apiKeyProvided ;
864865 }
865866
866867 Scope metricsScope = this .metricsScope != null ? this .metricsScope : new NoopScope ();
0 commit comments