Skip to content

Commit ce0ccda

Browse files
fix(router): handle /v1 as final path segment in build_backend_url dedup
Extends the /v1 deduplication logic to also strip /v1 from request paths when the base URL's path ends with /v1 (e.g. https://api.groq.com/openai/v1). The previous fix only matched paths starting with /v1/, which regressed providers like Groq whose base path has /v1 as the last segment rather than the first. The nested-proxy exclusion (e.g. /api/v1/openai) is preserved since /v1 appears in the middle — neither first nor last segment. Adds a regression test for the Groq-style base URL. Signed-off-by: Milos Milutinovic <[email protected]>
1 parent 1c3fc5b commit ce0ccda

1 file changed

Lines changed: 27 additions & 12 deletions

File tree

‎crates/openshell-router/src/backend.rs‎

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -768,20 +768,21 @@ fn build_provider_url(
768768

769769
fn build_backend_url(endpoint: &str, path: &str) -> String {
770770
let base = endpoint.trim_end_matches('/');
771-
// Strip the /v1 prefix from the request path only when the base URL's
772-
// own path component is exactly /v1 (e.g. openai, nvidia: ".../v1") or
773-
// starts with /v1/ (e.g. deepinfra: ".../v1/openai"). This avoids
774-
// doubling /v1 for those providers while preserving the full path for
775-
// proxy endpoints where /v1 appears deeper in the path
776-
// (e.g. "https://proxy.example/api/v1/openai").
777-
let base_path_is_v1_rooted = base
771+
// Strip the /v1 prefix from the request path when the base URL's path
772+
// component has /v1 as its first segment (e.g. openai/nvidia: "/v1",
773+
// deepinfra: "/v1/openai") or its final segment (e.g. groq:
774+
// "/openai/v1"). This covers all known provider shapes while preserving
775+
// the full path for proxy endpoints where /v1 is buried in the middle
776+
// (e.g. "https://proxy.example/api/v1/openai" → path "/api/v1/openai",
777+
// neither first nor last segment).
778+
let base_path_has_v1_edge_segment = base
778779
.find("://")
779780
.and_then(|i| base[i + 3..].find('/').map(|j| i + 3 + j))
780781
.is_some_and(|path_start| {
781782
let base_path = &base[path_start..];
782-
base_path == "/v1" || base_path.starts_with("/v1/")
783+
base_path.starts_with("/v1/") || base_path.ends_with("/v1")
783784
});
784-
if base_path_is_v1_rooted && (path == "/v1" || path.starts_with("/v1/")) {
785+
if base_path_has_v1_edge_segment && (path == "/v1" || path.starts_with("/v1/")) {
785786
return format!("{base}{}", &path[3..]);
786787
}
787788

@@ -857,11 +858,25 @@ mod tests {
857858
);
858859
}
859860

861+
#[test]
862+
fn build_backend_url_dedupes_v1_for_base_ending_with_v1() {
863+
// Providers like Groq use a base URL where /v1 is the final segment
864+
// below a non-root prefix (e.g. /openai/v1). The /v1 in the request
865+
// path must still be stripped so it is not doubled.
866+
assert_eq!(
867+
build_backend_url(
868+
"https://api.groq.com/openai/v1",
869+
"/v1/chat/completions"
870+
),
871+
"https://api.groq.com/openai/v1/chat/completions"
872+
);
873+
}
874+
860875
#[test]
861876
fn build_backend_url_preserves_v1_for_nested_proxy_path() {
862-
// A proxy whose base path has /v1 buried deeper (not at the root of
863-
// the path) must NOT have /v1 stripped — the full request path must
864-
// be appended so the upstream receives the correct API version prefix.
877+
// A proxy whose base path has /v1 buried in the middle (neither first
878+
// nor last segment) must NOT have /v1 stripped — the full request
879+
// path must be appended so the upstream receives the correct prefix.
865880
assert_eq!(
866881
build_backend_url(
867882
"https://proxy.example/api/v1/openai",

0 commit comments

Comments
 (0)