diff --git a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java index 974b99d0c1..eb3485258f 100644 --- a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java +++ b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java @@ -185,23 +185,23 @@ public static ClientContext create(StubSettings settings) throws IOException { if (apiKey != null) { // if API key exists it becomes the default credential credentials = ApiKeyCredentials.create(settings.getApiKey()); - } - - // check if need to adjust credentials/endpoint/endpointContext for GDC-H - String settingsGdchApiAudience = settings.getGdchApiAudience(); - boolean usingGDCH = credentials instanceof GdchCredentials; - if (usingGDCH) { - // Can only determine if the GDC-H is being used via the Credentials. The Credentials object - // is resolved in the ClientContext and must be passed to the EndpointContext. Rebuild the - // endpointContext only on GDC-H flows. - endpointContext = endpointContext.withGDCH(); - // Resolve the new endpoint with the GDC-H flow - endpoint = endpointContext.resolvedEndpoint(); - // We recompute the GdchCredentials with the audience - credentials = getGdchCredentials(settingsGdchApiAudience, endpoint, credentials); - } else if (!Strings.isNullOrEmpty(settingsGdchApiAudience)) { - throw new IllegalArgumentException( - "GDC-H API audience can only be set when using GdchCredentials"); + } else { + // check if need to adjust credentials/endpoint/endpointContext for GDC-H + String settingsGdchApiAudience = settings.getGdchApiAudience(); + boolean usingGDCH = credentials instanceof GdchCredentials; + if (usingGDCH) { + // Can only determine if the GDC-H is being used via the Credentials. The Credentials object + // is resolved in the ClientContext and must be passed to the EndpointContext. Rebuild the + // endpointContext only on GDC-H flows. + endpointContext = endpointContext.withGDCH(); + // Resolve the new endpoint with the GDC-H flow + endpoint = endpointContext.resolvedEndpoint(); + // We recompute the GdchCredentials with the audience + credentials = getGdchCredentials(settingsGdchApiAudience, endpoint, credentials); + } else if (!Strings.isNullOrEmpty(settingsGdchApiAudience)) { + throw new IllegalArgumentException( + "GDC-H API audience can only be set when using GdchCredentials"); + } } if (settings.getQuotaProjectId() != null && credentials != null) { diff --git a/gax-java/gax/src/main/java/com/google/api/gax/rpc/StubSettings.java b/gax-java/gax/src/main/java/com/google/api/gax/rpc/StubSettings.java index 0876f35346..800dbb9c1a 100644 --- a/gax-java/gax/src/main/java/com/google/api/gax/rpc/StubSettings.java +++ b/gax-java/gax/src/main/java/com/google/api/gax/rpc/StubSettings.java @@ -589,8 +589,8 @@ public B setTracerFactory(@Nonnull ApiTracerFactory tracerFactory) { } /** - * Sets the API key. The API key will get translated to an [ApiKeyCredentials] and stored in - * [CallContext]. + * Sets the API key. The API key will get translated to an {@link + * com.google.auth.ApiKeyCredentials} and stored in {@link ClientContext}. */ public B setApiKey(String apiKey) { this.apiKey = apiKey; diff --git a/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java b/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java index 771a73b061..7f6a3e86f5 100644 --- a/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java +++ b/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java @@ -1109,7 +1109,9 @@ void testCreateClient_throwsErrorIfApiKeyAndCredentialsAreProvided() throws Exce Mockito.when(headerProvider.getHeaders()).thenReturn(ImmutableMap.of()); builder.setHeaderProvider(headerProvider); builder.setApiKey(apiKey); - builder.setCredentialsProvider(Mockito.mock(CredentialsProvider.class)); + CredentialsProvider credentialsProvider = Mockito.mock(CredentialsProvider.class); + Mockito.when(credentialsProvider.getCredentials()).thenReturn(Mockito.mock(Credentials.class)); + builder.setCredentialsProvider(credentialsProvider); try { ClientContext.create(builder.build());