Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions rest/diagnostic_doc_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,22 @@ type SyncFnDryRun struct {

type ImportFilterDryRun struct {
ShouldImport bool `json:"shouldImport"`
Error string `json:"error"`
Exception string `json:"exception"`
Logging DryRunLogging `json:"logging"`
}

type SyncFnDryRunMetaMap struct {
Xattrs map[string]any `json:"xattrs"`
}
type SyncFnDryRunPayload struct {
DocID string `json:"doc_id"`
Function string `json:"sync_function"`
Doc db.Body `json:"doc,omitempty"`
Meta SyncFnDryRunMetaMap `json:"meta,omitempty"`
}

type ImportFilterDryRunPayload struct {
DocID string `json:"doc_id"`
Function string `json:"import_filter"`
Doc db.Body `json:"doc,omitempty"`
}
Expand Down Expand Up @@ -89,14 +91,14 @@ func (h *handler) handleGetDocChannels() error {
// docid only provided, the sync function will run using the current revision in the bucket as doc
// If docid is specified and the document does not exist in the bucket, it will return error
func (h *handler) handleSyncFnDryRun() error {
docid := h.getQuery("doc_id")

var syncDryRunPayload SyncFnDryRunPayload
err := h.readJSONInto(&syncDryRunPayload)
if err != nil {
return base.HTTPErrorf(http.StatusBadRequest, "Error reading sync function payload: %v", err)
}

docid := syncDryRunPayload.DocID
if syncDryRunPayload.Doc == nil && docid == "" {
Comment on lines +101 to 102
Copy link

Copilot AI Jan 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable docid uses inconsistent naming. The payload field is DocID (camelCase with capitalized ID), but the variable is docid (lowercase). Consider renaming to docID for consistency with Go naming conventions where acronyms should be consistently capitalized.

Copilot uses AI. Check for mistakes.
return base.HTTPErrorf(http.StatusBadRequest, "no doc_id or document provided")
}
Expand Down Expand Up @@ -221,14 +223,14 @@ func (h *handler) handleSyncFnDryRun() error {

// HTTP handler for running a document through the import filter and returning the results
func (h *handler) handleImportFilterDryRun() error {
docid := h.getQuery("doc_id")

var importFilterPayload ImportFilterDryRunPayload
err := h.readJSONInto(&importFilterPayload)
if err != nil {
return base.HTTPErrorf(http.StatusBadRequest, "Error reading import filter payload: %v", err)
}

docid := importFilterPayload.DocID
// Cannot pass both doc_id and body in the request body
Comment on lines +233 to 234
Copy link

Copilot AI Jan 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable docid uses inconsistent naming. The payload field is DocID (camelCase with capitalized ID), but the variable is docid (lowercase). Consider renaming to docID for consistency with Go naming conventions where acronyms should be consistently capitalized.

Copilot uses AI. Check for mistakes.
if len(importFilterPayload.Doc) > 0 && docid != "" {
return base.HTTPErrorf(http.StatusBadRequest, "doc body and doc id provided. Please provide either the body or a doc id for the import filter dry run")
Expand Down
38 changes: 20 additions & 18 deletions rest/diagnostic_doc_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1259,13 +1259,13 @@ func TestSyncFuncDryRun(t *testing.T) {
rt.PutDoc(test.name, test.existingDocBody)
}

bodyBytes, err := json.Marshal(test.request)
require.NoError(t, err)

url := "/{{.keyspace}}/_sync"
if test.requestDocID {
url += "?doc_id=" + test.name
test.request.DocID = test.name
}

bodyBytes, err := json.Marshal(test.request)
require.NoError(t, err)
resp := rt.SendDiagnosticRequest("POST", url, string(bodyBytes))
RequireStatus(t, resp, test.expectedStatus)

Expand Down Expand Up @@ -1416,13 +1416,14 @@ func TestSyncFuncDryRunUserXattrs(t *testing.T) {
}
}

bodyBytes, err := json.Marshal(test.request)
require.NoError(t, err)

url := "/{{.keyspace}}/_sync"
if test.requestDocID {
url += "?doc_id=" + test.name
test.request.DocID = test.name
}

bodyBytes, err := json.Marshal(test.request)
require.NoError(t, err)

resp := rt.SendDiagnosticRequest("POST", url, string(bodyBytes))
RequireStatus(t, resp, test.expectedStatus)

Expand All @@ -1439,7 +1440,7 @@ func TestSyncFuncDryRunErrors(t *testing.T) {
defer rt.Close()

// doc ID not found
RequireStatus(t, rt.SendDiagnosticRequest(http.MethodPost, "/{{.keyspace}}/_sync?doc_id=missing", `{}`), http.StatusNotFound)
RequireStatus(t, rt.SendDiagnosticRequest(http.MethodPost, "/{{.keyspace}}/_sync", `{"doc_id": "missing"}`), http.StatusNotFound)
// no doc ID or inline body provided
RequireStatus(t, rt.SendDiagnosticRequest(http.MethodPost, "/{{.keyspace}}/_sync", `{}`), http.StatusBadRequest)
// invalid request json
Expand Down Expand Up @@ -1586,7 +1587,7 @@ func TestImportFilterDryRun(t *testing.T) {
},
},
expectedOutput: ImportFilterDryRun{
Error: "Error returned from Import Filter: TypeError: Cannot access member 'num' of undefined",
Exception: "Error returned from Import Filter: TypeError: Cannot access member 'num' of undefined",
Logging: DryRunLogging{
Errors: []string{},
Info: []string{},
Expand All @@ -1610,7 +1611,7 @@ func TestImportFilterDryRun(t *testing.T) {
},
},
expectedOutput: ImportFilterDryRun{
Error: "Error returned from Import Filter: TypeError: Cannot access member 'num' of undefined",
Exception: "Error returned from Import Filter: TypeError: Cannot access member 'num' of undefined",
Logging: DryRunLogging{
Errors: []string{},
Info: []string{},
Expand Down Expand Up @@ -1642,7 +1643,7 @@ func TestImportFilterDryRun(t *testing.T) {
},
},
expectedOutput: ImportFilterDryRun{
Error: "Error returned from Import Filter: TypeError: Cannot access member 'num' of undefined",
Exception: "Error returned from Import Filter: TypeError: Cannot access member 'num' of undefined",
Logging: DryRunLogging{
Errors: []string{},
Info: []string{},
Expand Down Expand Up @@ -1890,13 +1891,14 @@ func TestImportFilterDryRun(t *testing.T) {
rt.PutDoc(test.name, test.existingDocBody)
}

bodyBytes, err := json.Marshal(test.request)
require.NoError(t, err)

url := "/{{.keyspace}}/_import_filter"
if test.requestDocID {
url += "?doc_id=" + test.name
test.request.DocID = test.name
}

bodyBytes, err := json.Marshal(test.request)
require.NoError(t, err)

resp := rt.SendDiagnosticRequest("POST", url, string(bodyBytes))
RequireStatus(t, resp, test.expectedStatus)

Expand All @@ -1913,9 +1915,9 @@ func TestImportFilterDryRunErrors(t *testing.T) {
defer rt.Close()

// doc ID not found
RequireStatus(t, rt.SendDiagnosticRequest(http.MethodPost, "/{{.keyspace}}/_import_filter?doc_id=missing", `{}`), http.StatusNotFound)
RequireStatus(t, rt.SendDiagnosticRequest(http.MethodPost, "/{{.keyspace}}/_import_filter", `{"doc_id": "missing"}`), http.StatusNotFound)
// invalid request
RequireStatus(t, rt.SendDiagnosticRequest(http.MethodPost, "/{{.keyspace}}/_import_filter?doc_id=doc", `{"doc": { "user" : {"num": 23 }}}`), http.StatusBadRequest)
RequireStatus(t, rt.SendDiagnosticRequest(http.MethodPost, "/{{.keyspace}}/_import_filter", `{"doc_id": "doc", "doc": { "user" : {"num": 23 }}}`), http.StatusBadRequest)
// invalid request json
RequireStatus(t, rt.SendDiagnosticRequest(http.MethodPost, "/{{.keyspace}}/_import_filter", `{"doc": {"invalid_json"}`), http.StatusBadRequest)
// invalid doc body type
Expand Down
Loading