fix: 修复 Gemini 风格 /v1/models 列表请求 - #6199
Conversation
WalkthroughThe ChangesGemini models endpoint
Estimated code review effort: 2 (Simple) | ~15 minutes Sequence Diagram(s)sequenceDiagram
participant Client
participant TokenAuth
participant RelayRouter
participant ListModels
Client->>TokenAuth: GET /v1/models with API key
TokenAuth->>RelayRouter: authenticated request
RelayRouter->>ListModels: list Gemini models
ListModels-->>Client: model list response
Possibly related PRs
Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
router/relay_router_test.go (1)
39-67: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueConsider adding a test case for the
keyquery parameter.The PR objective explicitly mentions supporting the
keyquery parameter for Gemini-style authentication. While header authentication is well-tested, adding a test case for the query parameter would ensure complete regression coverage of the newly added routing behavior.💡 Proposed change to include a query parameter test
tests := []struct { name string headerName string + queryName string expectedObject string expectedField string }{ { name: "OpenAI bearer token", headerName: "Authorization", expectedObject: "list", expectedField: "data", }, { name: "Gemini API key header", headerName: "x-goog-api-key", expectedField: "models", }, + { + name: "Gemini API key in query", + queryName: "key", + expectedField: "models", + }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { recorder := httptest.NewRecorder() - request := httptest.NewRequest(http.MethodGet, "/v1/models", nil) - value := "modelstestkey" - if test.headerName == "Authorization" { - value = "Bearer " + value - } - request.Header.Set(test.headerName, value) + path := "/v1/models" + if test.queryName != "" { + path += "?" + test.queryName + "=modelstestkey" + } + request := httptest.NewRequest(http.MethodGet, path, nil) + + if test.headerName != "" { + value := "modelstestkey" + if test.headerName == "Authorization" { + value = "Bearer " + value + } + request.Header.Set(test.headerName, value) + }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@router/relay_router_test.go` around lines 39 - 67, Add a table-driven case in the relay router test’s existing tests loop for Gemini authentication via the key query parameter, using the expected Gemini models response and constructing the request URL with the key parameter instead of relying on a header. Keep the existing Authorization and x-goog-api-key cases unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@router/relay_router_test.go`:
- Around line 39-67: Add a table-driven case in the relay router test’s existing
tests loop for Gemini authentication via the key query parameter, using the
expected Gemini models response and constructing the request URL with the key
parameter instead of relying on a header. Keep the existing Authorization and
x-goog-api-key cases unchanged.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 3c68f56b-e6f8-4226-bd96-e468b85bbde0
📒 Files selected for processing (3)
middleware/auth.gorouter/relay-router.gorouter/relay_router_test.go
|
已补充 |
移植自上游 PR QuantumNous#6199(issue QuantumNous#6198)。带 x-goog-api-key 头或 ?key= 的 /v1/models 请求原先走 RetrieveModel(单模型详情),对列表请求必错; 改为 ListModels 返回模型列表。同步在 TokenAuth 里放行精确路径 /v1/models 从 query 取 key(与 /v1beta 现状一致)。补三种鉴权 (OpenAI bearer / Gemini header / Gemini query)的路由契约测试。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* fix: support Gemini model listing on v1 route * test: cover Gemini query key model listing
* fix: support Gemini model listing on v1 route * test: cover Gemini query key model listing Upstream-Commit: 3d5dc36
* fix: support Gemini model listing on v1 route * test: cover Gemini query key model listing
* fix: support Gemini model listing on v1 route * test: cover Gemini query key model listing
* fix: support Gemini model listing on v1 route * test: cover Gemini query key model listing
* fix: support Gemini model listing on v1 route * test: cover Gemini query key model listing
Important
📝 变更描述 / Description
修复 Gemini 风格的
GET /v1/models列表请求:/v1/models上读取x-goog-api-key或key查询参数。ListModels,避免因缺少:model参数进入RetrieveModel并返回空模型名错误。x-goog-api-key和?key=的响应契约。🚀 变更类型 / Type of change
🔗 关联任务 / Related Issue
✅ 提交前检查项 / Checklist
Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。📸 运行证明 / Proof of Work
本地启动完整 HTTP 服务后的实际请求结果:
Summary by CodeRabbit
Bug Fixes
/v1/modelsproperly recognizes and handles Gemini API key input from headers andkeyquery parameters.Tests
GET /v1/modelsvalidating successful responses for OpenAI bearer auth, Gemini header auth, and Gemini?key=...query auth.