diff --git a/src/main/java/io/pinecone/clients/Pinecone.java b/src/main/java/io/pinecone/clients/Pinecone.java index 9d7cc082..9131a5ff 100644 --- a/src/main/java/io/pinecone/clients/Pinecone.java +++ b/src/main/java/io/pinecone/clients/Pinecone.java @@ -1778,11 +1778,16 @@ public Pinecone build() { apiClient = new ApiClient(customOkHttpClient); } else { apiClient = new ApiClient(buildOkHttpClient(proxyConfig)); - if(host!=null && !host.isEmpty()) { - config.setHost(host); - apiClient.setBasePath(host); - } } + + // Host must be applied regardless of which OkHttpClient path was used above, + // otherwise an explicitly configured host is silently ignored whenever a + // customOkHttpClient is also supplied. See https://github.com/pinecone-io/pinecone-java-client/issues/192 + if (host != null && !host.isEmpty()) { + config.setHost(host); + apiClient.setBasePath(host); + } + apiClient.setApiKey(config.getApiKey()); apiClient.setUserAgent(config.getUserAgent()); apiClient.addDefaultHeader("X-Pinecone-Api-Version", Configuration.VERSION); diff --git a/src/test/java/io/pinecone/PineconeBuilderTest.java b/src/test/java/io/pinecone/PineconeBuilderTest.java index 9557f973..8148bc69 100644 --- a/src/test/java/io/pinecone/PineconeBuilderTest.java +++ b/src/test/java/io/pinecone/PineconeBuilderTest.java @@ -78,6 +78,36 @@ public void PineconeWithOkHttpClientAndUserAgent() throws IOException { assertEquals("lang=java; pineconeClientVersion=" + pineconeClientVersion, requestCaptor.getValue().header("User-Agent")); } + @Test + public void PineconeWithHostAndCustomOkHttpClientUsesHost() throws IOException { + // Regression test for https://github.com/pinecone-io/pinecone-java-client/issues/192 -- + // withHost(...) must still be honored when a custom OkHttpClient is also supplied. + String filePath = "src/test/resources/serverlessIndexJsonString.json"; + String indexJsonStringServerless = new String(Files.readAllBytes(Paths.get(filePath))); + + AbstractMap.SimpleEntry mockCallAndClient = + buildMockCallAndClient(ResponseBody.create( + indexJsonStringServerless, + MediaType.parse("application/json"))); + + OkHttpClient mockClient = mockCallAndClient.getValue(); + + Pinecone client = new Pinecone.Builder("testAPiKey") + .withHost("http://my-custom-host:5080") + .withOkHttpClient(mockClient) + .build(); + // Create expected index after client creation to ensure JSON class is initialized + IndexModel expectedIndex = IndexModel.fromJson(indexJsonStringServerless); + IndexModel index = client.describeIndex("testIndex"); + + ArgumentCaptor requestCaptor = ArgumentCaptor.forClass(Request.class); + + assertEquals(expectedIndex, index); + verify(mockClient, times(1)).newCall(requestCaptor.capture()); + assertEquals("my-custom-host", requestCaptor.getValue().url().host()); + assertEquals(5080, requestCaptor.getValue().url().port()); + } + @Test public void PineconeWithSourceTag() throws IOException { String filePath = "src/test/resources/serverlessIndexJsonString.json";