Repository navigation
fix(auth/apikeys): fix 500 on API key create -- type assertion rejected cross-package request (closes #1440) - #1444
Conversation
Root cause: CreateAPIKeyAPI did req.(auth.APICreateAPIKeyRequest) to decode the incoming request. The HTTP handler (internal/api package) unmarshals the request body into api.CreateAPIKeyRequest - a distinct Go type that carries the same json field names but is not auth.APICreateAPIKeyRequest. The type assertion always failed, returning "invalid request type", which bubbled up as a 500 "Internal server error" on every POST /api/api-keys call. Fix: replace the direct type assertion with a type switch. The fast path handles auth.APICreateAPIKeyRequest directly (existing service tests). The default path JSON-encodes the incoming value and decodes it into APICreateAPIKeyRequest; this accepts any struct whose json tags are compatible (including api.CreateAPIKeyRequest), without requiring a cross-package import. Also fix TestAuthServiceAdapter_CreateAPIKeyAPI in internal/server which was missing GetUserByID and GetGroup mocks: the test silently passed before because the type assertion failure returned early; now the full call chain executes and the mocks must be complete. Regression test: TestService_CreateAPIKeyAPI_CrossPackageType passes an anonymous struct (same json tags, different Go type) to CreateAPIKeyAPI and asserts success. This test would have failed against the pre-fix code.
|
@coderabbitai review |
|
Warning Review limit reachedYou’ve reached a temporary PR review limit under our Fair Usage Limits Policy. Next review available in: 11 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
✅ Action performedReview finished.
|
|
@coderabbitai review |
✅ Action performedReview finished.
|
|
@coderabbitai full review |
✅ Action performedFull review finished. You're currently rate limited under our Fair Usage Limits Policy. Your recent PR review activity is in the 95th percentile or higher among CodeRabbit users, so adaptive limits apply. Your next review will be available in 11 minutes. |
Root cause
Service.CreateAPIKeyAPI in internal/auth/service_apikeys_api.go decoded the incoming request using a direct Go type assertion:
The HTTP handler (internal/api) unmarshals the JSON request body into api.CreateAPIKeyRequest, which is a distinct Go type in a different package. Even though both types carry identical json field tags (name, expires_at, permissions), Go's type system is nominal: the assertion req.(auth.APICreateAPIKeyRequest) always evaluates to false. Every call to POST /api/api-keys therefore returned "invalid request type", which bubbled up as a 500 "Failed to create API key: Internal server error".
The mock test (TestHandler_createAPIKey_Success) used mock.Anything so it never caught the type mismatch.
Fix
Replace the direct type assertion with a type switch:
Tests
Closes
Closes #1440