Skip to content

Commit 81b5036

Browse files
authored
feat: [Core] Updated Base Path Behaviour (#219)
1 parent bb92f96 commit 81b5036

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

core/src/main/java/com/sap/ai/sdk/core/DestinationResolver.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.sap.cloud.sdk.cloudplatform.connectivity.HttpDestination;
1313
import com.sap.cloud.sdk.cloudplatform.connectivity.ServiceBindingDestinationLoader;
1414
import com.sap.cloud.sdk.cloudplatform.connectivity.ServiceBindingDestinationOptions;
15+
import java.net.URI;
1516
import java.util.List;
1617
import javax.annotation.Nonnull;
1718
import lombok.AllArgsConstructor;
@@ -61,9 +62,19 @@ HttpDestination getDestination() {
6162

6263
@Nonnull
6364
static HttpDestination fromCustomBaseDestination(@Nonnull final HttpDestination destination) {
64-
// for custom base destinations we only add the client type header, since users are allowed to
65-
// pass a custom base path
66-
return addClientTypeHeader(destination);
65+
val enhancedBaseDestination = addClientTypeHeader(destination);
66+
val path = enhancedBaseDestination.getUri().getPath();
67+
if (path == null || path.isEmpty() || path.equals("/")) {
68+
return setBasePath(enhancedBaseDestination);
69+
}
70+
if (path.endsWith("/")) {
71+
return enhancedBaseDestination;
72+
}
73+
val urlWithTrailingSlash = URI.create(enhancedBaseDestination.getUri() + "/");
74+
75+
return DefaultHttpDestination.fromDestination(enhancedBaseDestination)
76+
.uri(urlWithTrailingSlash)
77+
.build();
6778
}
6879

6980
@Nonnull

core/src/test/java/com/sap/ai/sdk/core/AiCoreServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void testCustomBaseDestination() {
5555

5656
assertThat(customService.getBaseDestination().getUri())
5757
.hasHost("custom.ai.com")
58-
.hasPath("/custom/base/path");
58+
.hasPath("/custom/base/path/");
5959
assertThat(customService.getBaseDestination().getHeaders())
6060
.describedAs("The client type should be added to ensure its present in all requests")
6161
.containsExactly(new Header("AI-Client-Type", "AI SDK Java"));

core/src/test/java/com/sap/ai/sdk/core/DestinationResolverTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import com.sap.cloud.environment.servicebinding.api.ServiceBindingAccessor;
99
import com.sap.cloud.environment.servicebinding.api.exception.ServiceBindingAccessException;
10+
import com.sap.cloud.sdk.cloudplatform.connectivity.DefaultHttpDestination;
1011
import java.util.List;
1112
import lombok.val;
1213
import org.junit.jupiter.api.Test;
@@ -54,4 +55,26 @@ void testNoServiceBindingLoadingThrows() {
5455
var resolver = new DestinationResolver(mock);
5556
assertThatThrownBy(resolver::getDestination).isSameAs(exception);
5657
}
58+
59+
private record DestinationTestCase(String givenBasePath, String expectedBasePath) {}
60+
61+
@Test
62+
void testFromCustomBaseDestination() {
63+
var testCases =
64+
new DestinationTestCase[] {
65+
new DestinationTestCase("", "/v2/"),
66+
new DestinationTestCase("/", "/v2/"),
67+
new DestinationTestCase("/foo", "/foo/"),
68+
new DestinationTestCase("/foo/", "/foo/")
69+
};
70+
for (var testCase : testCases) {
71+
var url = "https://api.ai.sap" + testCase.givenBasePath;
72+
var destination = DefaultHttpDestination.builder(url).build();
73+
assertThat(DestinationResolver.fromCustomBaseDestination(destination).getUri().getPath())
74+
.describedAs(
75+
"Expecting given base path %s to resolve to %s",
76+
testCase.givenBasePath, testCase.expectedBasePath)
77+
.hasToString(testCase.expectedBasePath);
78+
}
79+
}
5780
}

0 commit comments

Comments
 (0)