Repository navigation
feat[Go]: implement create_connector API - #15285
Conversation
📝 WalkthroughWalkthroughAdds a POST /api/v1/connectors/ endpoint: service DTO and defaults, handler JSON validation and service call, custom Connector JSON marshaling, router registration, and updated handler test doubles. ChangesConnector creation endpoint
Sequence DiagramsequenceDiagram
participant Client
participant Router
participant ConnectorHandler
participant ConnectorService
participant ConnectorDAO
Client->>Router: POST /api/v1/connectors/
Router->>ConnectorHandler: CreateConnector(ctx)
ConnectorHandler->>ConnectorHandler: Bind JSON, trim/validate Name/Source, check Config
ConnectorHandler->>ConnectorService: CreateConnector(userID, req)
ConnectorService->>ConnectorService: Apply default refresh/prune/timeout
ConnectorService->>ConnectorDAO: Create(entity.Connector)
ConnectorDAO-->>ConnectorService: persisted ID
ConnectorService->>ConnectorDAO: GetByID(id)
ConnectorDAO-->>ConnectorService: connector entity
ConnectorService-->>ConnectorHandler: connector entity
ConnectorHandler-->>Client: HTTP 200 + connector JSON
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. 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.
Actionable comments posted: 2
🤖 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.
Inline comments:
In `@internal/handler/connector.go`:
- Around line 117-119: Update the Swagger comment above the CreateConnector
handler: change the response type from service.ListConnectorsResponse to the
correct create response type used by the service (e.g.,
service.CreateConnectorResponse or service.ConnectorResponse used for
single-connector returns) and change the documented route from `/connector/
[post]` to the actual router path `/api/v1/connectors/ [post]` so the annotation
on the CreateConnector function matches the router and response shape.
In `@internal/service/connector.go`:
- Around line 83-87: The Connector is being persisted with TenantID set to
userID which breaks tenant-scoped reads (ListConnectors); change the assignment
so Connector.TenantID is the actual tenant identifier used by ListConnectors
(e.g., tenantID from the request or context) instead of userID—update the struct
construction in the connector creation (entity.Connector with field TenantID) to
use the correct tenant variable and ensure any upstream variable (where userID
is currently used) is replaced or supplemented so ownership is stored by tenant
ID.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 2ddebe7b-62ba-485a-8464-994e12e147f0
📒 Files selected for processing (3)
internal/handler/connector.gointernal/router/router.gointernal/service/connector.go
| // @Success 200 {object} service.ListConnectorsResponse | ||
| // @Router /connector/ [post] | ||
| func (h *ConnectorHandler) CreateConnector(c *gin.Context) { |
There was a problem hiding this comment.
Fix Swagger contract: wrong response type and route path.
Line 117 uses service.ListConnectorsResponse for a create endpoint, and Line 118 documents /connector/ [post] while the router exposes POST /api/v1/connectors/ (internal/router/router.go Line 311). This will generate inaccurate API docs.
🤖 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 `@internal/handler/connector.go` around lines 117 - 119, Update the Swagger
comment above the CreateConnector handler: change the response type from
service.ListConnectorsResponse to the correct create response type used by the
service (e.g., service.CreateConnectorResponse or service.ConnectorResponse used
for single-connector returns) and change the documented route from `/connector/
[post]` to the actual router path `/api/v1/connectors/ [post]` so the annotation
on the CreateConnector function matches the router and response shape.
| connector := &entity.Connector{ | ||
| ID: common.GenerateUUID(), | ||
| TenantID: userID, | ||
| Name: req.Name, | ||
| Source: req.Source, |
There was a problem hiding this comment.
Persist connector ownership by tenant ID, not raw userID.
At Line 85, TenantID is set to userID, but ListConnectors reads by tenant ID (Lines 136-154). For users whose tenant ID differs from user ID, created connectors can become invisible in list results and won’t align with tenant-scoped access.
Suggested fix
func (s *ConnectorService) CreateConnector(userID string, req *CreateConnectorRequest) (*entity.Connector, error) {
+ tenantIDs, err := s.userTenantDAO.GetTenantIDsByUserID(userID)
+ if err != nil {
+ return nil, err
+ }
+ tenantID := userID
+ if len(tenantIDs) > 0 {
+ tenantID = tenantIDs[0]
+ }
+
refreshFreq := int64(defaultConnectorFreq)
if req.RefreshFreq != nil {
refreshFreq = *req.RefreshFreq
}
@@
connector := &entity.Connector{
ID: common.GenerateUUID(),
- TenantID: userID,
+ TenantID: tenantID,
Name: req.Name,
Source: req.Source,📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| connector := &entity.Connector{ | |
| ID: common.GenerateUUID(), | |
| TenantID: userID, | |
| Name: req.Name, | |
| Source: req.Source, | |
| func (s *ConnectorService) CreateConnector(userID string, req *CreateConnectorRequest) (*entity.Connector, error) { | |
| tenantIDs, err := s.userTenantDAO.GetTenantIDsByUserID(userID) | |
| if err != nil { | |
| return nil, err | |
| } | |
| tenantID := userID | |
| if len(tenantIDs) > 0 { | |
| tenantID = tenantIDs[0] | |
| } | |
| refreshFreq := int64(defaultConnectorFreq) | |
| if req.RefreshFreq != nil { | |
| refreshFreq = *req.RefreshFreq | |
| } | |
| connector := &entity.Connector{ | |
| ID: common.GenerateUUID(), | |
| TenantID: tenantID, | |
| Name: req.Name, | |
| Source: req.Source, |
🤖 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 `@internal/service/connector.go` around lines 83 - 87, The Connector is being
persisted with TenantID set to userID which breaks tenant-scoped reads
(ListConnectors); change the assignment so Connector.TenantID is the actual
tenant identifier used by ListConnectors (e.g., tenantID from the request or
context) instead of userID—update the struct construction in the connector
creation (entity.Connector with field TenantID) to use the correct tenant
variable and ensure any upstream variable (where userID is currently used) is
replaced or supplemented so ownership is stored by tenant ID.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #15285 +/- ##
=======================================
Coverage 93.16% 93.16%
=======================================
Files 10 10
Lines 717 717
Branches 118 118
=======================================
Hits 668 668
Misses 29 29
Partials 20 20 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
internal/entity/connector.go (1)
45-90: ⚡ Quick winAdd a JSON contract test for
Connector.MarshalJSON.This method now defines the handler-facing response shape, but the exact
2006-01-02T15:04:05layout andnilvs omitted behavior are unpinned. A small table-driven marshal test would make future contract drift obvious.🤖 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 `@internal/entity/connector.go` around lines 45 - 90, Add a table-driven unit test that verifies the JSON contract produced by Connector.MarshalJSON (and indirectly formatConnectorTime): create multiple Connector cases (with populated times, nil times, and edge values) and assert the marshaled JSON matches the exact field names and time layout "2006-01-02T15:04:05", and that nil time fields are omitted or serialized as null exactly per current behavior; include checks for CreateTime/CreateDate and UpdateTime/UpdateDate, comparing both presence/absence and string formatting so any future contract drift is caught.internal/handler/connector_test.go (1)
28-44: ⚡ Quick winAdd handler coverage for
CreateConnector.This file still only exercises
GetConnector, so the new POST/connectors/flow's request validation and response envelope are not locked down here. A success case plus a couple invalid-payload cases would give this fake a purpose beyond compile-time interface coverage.🤖 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 `@internal/handler/connector_test.go` around lines 28 - 44, Tests currently only exercise GetConnector so CreateConnector request validation and response envelope are untested; add unit tests that POST to the handler's CreateConnector path using the fakeConnectorService (the fakeConnectorService.CreateConnector method) covering at least a successful creation (valid JSON payload -> expect 201 and proper response envelope containing the created entity) and a couple invalid-payload cases (malformed JSON and missing required fields -> expect 400 with validation error envelope). In each test, construct the HTTP request to the POST /connectors/ handler, inject a fakeConnectorService configured to return the desired connector or error, assert response status codes, and decode the response body to verify the envelope structure and error codes match the handler behavior.
🤖 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.
Inline comments:
In `@internal/handler/connector_test.go`:
- Around line 28-30: The fakeConnectorService.CreateConnector method currently
returns s.connector even when s.err is non-nil; change CreateConnector (on type
fakeConnectorService) to return (nil, s.err) when s.err is set so it mirrors
GetConnector and UpdateConnector behavior and avoids exposing a partial-success
state in tests.
---
Nitpick comments:
In `@internal/entity/connector.go`:
- Around line 45-90: Add a table-driven unit test that verifies the JSON
contract produced by Connector.MarshalJSON (and indirectly formatConnectorTime):
create multiple Connector cases (with populated times, nil times, and edge
values) and assert the marshaled JSON matches the exact field names and time
layout "2006-01-02T15:04:05", and that nil time fields are omitted or serialized
as null exactly per current behavior; include checks for CreateTime/CreateDate
and UpdateTime/UpdateDate, comparing both presence/absence and string formatting
so any future contract drift is caught.
In `@internal/handler/connector_test.go`:
- Around line 28-44: Tests currently only exercise GetConnector so
CreateConnector request validation and response envelope are untested; add unit
tests that POST to the handler's CreateConnector path using the
fakeConnectorService (the fakeConnectorService.CreateConnector method) covering
at least a successful creation (valid JSON payload -> expect 201 and proper
response envelope containing the created entity) and a couple invalid-payload
cases (malformed JSON and missing required fields -> expect 400 with validation
error envelope). In each test, construct the HTTP request to the POST
/connectors/ handler, inject a fakeConnectorService configured to return the
desired connector or error, assert response status codes, and decode the
response body to verify the envelope structure and error codes match the handler
behavior.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 56c2ec6a-266a-4eed-b72b-c8cc5cd26f31
📒 Files selected for processing (2)
internal/entity/connector.gointernal/handler/connector_test.go
| func (s fakeConnectorService) CreateConnector(string, *service.CreateConnectorRequest) (*entity.Connector, error) { | ||
| return s.connector, s.err | ||
| } |
There was a problem hiding this comment.
Make CreateConnector fail like the other fake methods.
Line 29 returns s.connector even when s.err is set, which gives tests a partial-success state the real service should not expose. Returning nil on error keeps this fake aligned with GetConnector and UpdateConnector and avoids masking handler bugs.
Suggested fix
func (s fakeConnectorService) CreateConnector(string, *service.CreateConnectorRequest) (*entity.Connector, error) {
- return s.connector, s.err
+ if s.err != nil {
+ return nil, s.err
+ }
+ return s.connector, nil
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| func (s fakeConnectorService) CreateConnector(string, *service.CreateConnectorRequest) (*entity.Connector, error) { | |
| return s.connector, s.err | |
| } | |
| func (s fakeConnectorService) CreateConnector(string, *service.CreateConnectorRequest) (*entity.Connector, error) { | |
| if s.err != nil { | |
| return nil, s.err | |
| } | |
| return s.connector, nil | |
| } |
🤖 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 `@internal/handler/connector_test.go` around lines 28 - 30, The
fakeConnectorService.CreateConnector method currently returns s.connector even
when s.err is non-nil; change CreateConnector (on type fakeConnectorService) to
return (nil, s.err) when s.err is set so it mirrors GetConnector and
UpdateConnector behavior and avoids exposing a partial-success state in tests.
What problem does this PR solve?
implement create_connector API
Verified from POSTMAN
Type of change