diff --git a/CHANGELOG.md b/CHANGELOG.md index 861521fad..da4be9ef6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## 0.48.1 - Unreleased +- Drive/Docs: return `[]` instead of `null` from `drive labels list`, `drive labels file list`, `drive comments list` and `docs comments list` with `--json` when there is nothing to list, including with `--results-only`. Google omits empty list fields, so these printed `null`. (#1326) — thanks @saariuslystoned. + ## 0.48.0 - 2026-11-05 **Highlights:** New backups stay local until you configure a remote; existing configurations keep working. Store service-account keys in the keyring, search Gmail IDs efficiently, and use environment-backed watch tokens, with Calendar fixes and security dependency updates. diff --git a/internal/cmd/comment_ops.go b/internal/cmd/comment_ops.go index a882d86fc..c3d062976 100644 --- a/internal/cmd/comment_ops.go +++ b/internal/cmd/comment_ops.go @@ -119,6 +119,14 @@ type driveCommentListOptions struct { } func listDriveComments(ctx context.Context, svc *drive.Service, fileID string, opts driveCommentListOptions) ([]*drive.Comment, string, error) { + comments, nextPageToken, err := listDriveCommentPages(ctx, svc, fileID, opts) + if err != nil { + return nil, "", err + } + return nonNilSlice(comments), nextPageToken, nil +} + +func listDriveCommentPages(ctx context.Context, svc *drive.Service, fileID string, opts driveCommentListOptions) ([]*drive.Comment, string, error) { fetch := func(pageToken string) ([]*drive.Comment, string, error) { return fetchDriveCommentsPage(ctx, svc, fileID, opts.max, pageToken, opts.since, driveCommentFieldsForList(opts)) } diff --git a/internal/cmd/drive_labels.go b/internal/cmd/drive_labels.go index cda5666f9..101188cd4 100644 --- a/internal/cmd/drive_labels.go +++ b/internal/cmd/drive_labels.go @@ -74,7 +74,7 @@ func (c *DriveLabelsFileListCmd) Run(ctx context.Context, flags *RootFlags) erro } if outfmt.IsJSON(ctx) { return outfmt.WriteJSON(ctx, stdoutWriter(ctx), map[string]any{ - "labels": resp.Labels, + "labels": nonNilSlice(resp.Labels), "labelCount": len(resp.Labels), "nextPageToken": resp.NextPageToken, }) @@ -227,7 +227,7 @@ func (c *DriveLabelsListCmd) Run(ctx context.Context, flags *RootFlags) error { if outfmt.IsJSON(ctx) { return outfmt.WriteJSON(ctx, stdoutWriter(ctx), map[string]any{ - "labels": resp.Labels, + "labels": nonNilSlice(resp.Labels), "labelCount": len(resp.Labels), "nextPageToken": resp.NextPageToken, }) diff --git a/internal/cmd/empty_list_json_test.go b/internal/cmd/empty_list_json_test.go new file mode 100644 index 000000000..506fa4d43 --- /dev/null +++ b/internal/cmd/empty_list_json_test.go @@ -0,0 +1,113 @@ +package cmd + +import ( + "encoding/json" + "net/http" + "strings" + "testing" + + "google.golang.org/api/drivelabels/v2" + + "github.com/openclaw/gogcli/internal/app" +) + +// Google omits empty list fields, so each fake returns {}. +func TestEmptyListJSON(t *testing.T) { + t.Run("drive labels list", func(t *testing.T) { + svc, closeSvc := newGoogleTestService(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet || r.URL.Path != "/v2/labels" { + http.NotFound(w, r) + return + } + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(map[string]any{}) + }), drivelabels.NewService) + defer closeSvc() + + assertEmptyListJSON(t, "labels", func(args []string) executeTestResult { + return executeWithTestRuntime(t, args, &app.Runtime{Services: app.Services{ + DriveLabels: fixedDriveLabelsTestService(svc), + }}) + }, []string{"--account", "a@example.com", "drive", "labels", "list"}) + }) + + t.Run("drive labels file list", func(t *testing.T) { + svc, closeSvc := newDriveTestService(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet || r.URL.Path != "/files/file1/listLabels" { + http.NotFound(w, r) + return + } + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(map[string]any{}) + })) + defer closeSvc() + + assertEmptyListJSON(t, "labels", func(args []string) executeTestResult { + return executeWithDriveTestService(t, args, svc) + }, []string{"--account", "a@example.com", "drive", "labels", "file", "list", "file1"}) + }) + + t.Run("docs comments list", func(t *testing.T) { + svc, closeSvc := newDriveTestService(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + path := strings.TrimPrefix(r.URL.Path, "/drive/v3") + if r.Method != http.MethodGet || path != "/files/empty/comments" { + http.NotFound(w, r) + return + } + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(map[string]any{}) + })) + defer closeSvc() + + assertEmptyListJSON(t, "comments", func(args []string) executeTestResult { + return executeWithDriveTestService(t, args, svc) + }, []string{"--account", "a@b.com", "docs", "comments", "list", "empty"}) + }) + + t.Run("drive comments list", func(t *testing.T) { + svc, closeSvc := newDriveTestService(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + path := strings.TrimPrefix(r.URL.Path, "/drive/v3") + if r.Method != http.MethodGet || path != "/files/empty/comments" { + http.NotFound(w, r) + return + } + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(map[string]any{}) + })) + defer closeSvc() + + assertEmptyListJSON(t, "comments", func(args []string) executeTestResult { + return executeWithDriveTestService(t, args, svc) + }, []string{"--account", "a@b.com", "drive", "comments", "list", "empty"}) + }) +} + +func assertEmptyListJSON(t *testing.T, listKey string, run func([]string) executeTestResult, args []string) { + t.Helper() + + jsonArgs := append([]string{"--json"}, args...) + result := run(jsonArgs) + if result.err != nil { + t.Fatalf("Execute: %v\nstderr=%q\nstdout=%q", result.err, result.stderr, result.stdout) + } + var parsed map[string]json.RawMessage + if err := json.Unmarshal([]byte(result.stdout), &parsed); err != nil { + t.Fatalf("json parse: %v\n%s", err, result.stdout) + } + raw, ok := parsed[listKey] + if !ok { + t.Fatalf("missing %s key: %s", listKey, result.stdout) + } + if strings.TrimSpace(string(raw)) != "[]" { + t.Fatalf("%s = %s, want []\nstdout=%s", listKey, raw, result.stdout) + } + + onlyArgs := append([]string{"--json", "--results-only"}, args...) + result = run(onlyArgs) + if result.err != nil { + t.Fatalf("Execute --results-only: %v\nstderr=%q\nstdout=%q", result.err, result.stderr, result.stdout) + } + if got := strings.TrimSpace(result.stdout); got != "[]" { + t.Fatalf("--results-only = %q, want []", result.stdout) + } +} diff --git a/internal/cmd/output_helpers.go b/internal/cmd/output_helpers.go index 741b4587a..d3cff9d18 100644 --- a/internal/cmd/output_helpers.go +++ b/internal/cmd/output_helpers.go @@ -94,3 +94,12 @@ func escapeTerminalControls(value string) string { } return output.String() } + +// nonNilSlice returns s, or an empty slice when s is nil. Google omits empty +// list fields, and JSON output should still encode an empty list as []. +func nonNilSlice[T any](s []T) []T { + if s == nil { + return []T{} + } + return s +}