fix: list tables tools null fix (#2107)

## Description

Return empty list instead of null in list tables tools when no tables
found

## PR Checklist

- [x] Make sure you reviewed

[CONTRIBUTING.md](https://github.com/googleapis/genai-toolbox/blob/main/CONTRIBUTING.md)
- [x] Make sure to open an issue as a

[bug/issue](https://github.com/googleapis/genai-toolbox/issues/new/choose)
  before writing your code! That way we can discuss the change, evaluate
  designs, and agree on the general idea
- [x] Ensure the tests and linter pass
- [x] Code coverage does not decrease (if any source code was changed)
- [x] Appropriate docs were updated (if necessary)
- [x] Make sure to add `!` if this involve a breaking change

🛠️ Fixes #2027

---------

Co-authored-by: Averi Kitsch <akitsch@google.com>
This commit is contained in:
gRedHeadphone
2025-12-12 02:28:52 +05:30
committed by GitHub
parent 26ead2ed78
commit 2b45266598
7 changed files with 39 additions and 55 deletions

View File

@@ -121,7 +121,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
}
defer results.Close()
var tables []map[string]any
tables := []map[string]any{}
for results.Next() {
var tableName string
err := results.Scan(&tableName)

View File

@@ -391,7 +391,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
values[i] = &rawValues[i]
}
var out []any
out := []any{}
for rows.Next() {
err = rows.Scan(values...)
if err != nil {

View File

@@ -300,7 +300,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
return nil, fmt.Errorf("unable to get column types: %w", err)
}
var out []any
out := []any{}
for results.Next() {
err := results.Scan(values...)
if err != nil {

View File

@@ -210,7 +210,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
defer results.Close()
fields := results.FieldDescriptions()
var out []map[string]any
out := []map[string]any{}
for results.Next() {
values, err := results.Values()

View File

@@ -129,7 +129,7 @@ type Tool struct {
// processRows iterates over the spanner.RowIterator and converts each row to a map[string]any.
func processRows(iter *spanner.RowIterator) ([]any, error) {
var out []any
out := []any{}
defer iter.Stop()
for {

View File

@@ -250,7 +250,7 @@ func RunMariDBListTablesTest(t *testing.T, databaseName, tableNameParam, tableNa
name: "invoke list_tables with non-existent table",
requestBody: bytes.NewBufferString(`{"table_names": "non_existent_table"}`),
wantStatusCode: http.StatusOK,
want: nil,
want: []objectDetails{},
},
}
for _, tc := range invokeTcs {
@@ -282,7 +282,7 @@ func RunMariDBListTablesTest(t *testing.T, databaseName, tableNameParam, tableNa
if err := json.Unmarshal([]byte(resultString), &tables); err != nil {
t.Fatalf("failed to unmarshal outer JSON array into []tableInfo: %v", err)
}
var details []map[string]any
details := []map[string]any{}
for _, table := range tables {
var d map[string]any
if err := json.Unmarshal([]byte(table.ObjectDetails), &d); err != nil {
@@ -292,23 +292,19 @@ func RunMariDBListTablesTest(t *testing.T, databaseName, tableNameParam, tableNa
}
got = details
} else {
if resultString == "null" {
got = nil
} else {
var tables []tableInfo
if err := json.Unmarshal([]byte(resultString), &tables); err != nil {
t.Fatalf("failed to unmarshal outer JSON array into []tableInfo: %v", err)
}
var details []objectDetails
for _, table := range tables {
var d objectDetails
if err := json.Unmarshal([]byte(table.ObjectDetails), &d); err != nil {
t.Fatalf("failed to unmarshal nested ObjectDetails string: %v", err)
}
details = append(details, d)
}
got = details
var tables []tableInfo
if err := json.Unmarshal([]byte(resultString), &tables); err != nil {
t.Fatalf("failed to unmarshal outer JSON array into []tableInfo: %v", err)
}
details := []objectDetails{}
for _, table := range tables {
var d objectDetails
if err := json.Unmarshal([]byte(table.ObjectDetails), &d); err != nil {
t.Fatalf("failed to unmarshal nested ObjectDetails string: %v", err)
}
details = append(details, d)
}
got = details
}
opts := []cmp.Option{
@@ -319,7 +315,7 @@ func RunMariDBListTablesTest(t *testing.T, databaseName, tableNameParam, tableNa
// Checking only the current database where the test tables are created to avoid brittle tests.
if tc.isAllTables {
var filteredGot []objectDetails
filteredGot := []objectDetails{}
if got != nil {
for _, item := range got.([]objectDetails) {
if item.SchemaName == databaseName {
@@ -327,11 +323,7 @@ func RunMariDBListTablesTest(t *testing.T, databaseName, tableNameParam, tableNa
}
}
}
if len(filteredGot) == 0 {
got = nil
} else {
got = filteredGot
}
got = filteredGot
}
if diff := cmp.Diff(tc.want, got, opts...); diff != "" {

View File

@@ -1189,7 +1189,7 @@ func RunPostgresListTablesTest(t *testing.T, tableNameParam, tableNameAuth, user
api: "http://127.0.0.1:5000/api/tool/list_tables/invoke",
requestBody: bytes.NewBuffer([]byte(`{"table_names": "non_existent_table"}`)),
wantStatusCode: http.StatusOK,
want: `null`,
want: `[]`,
},
{
name: "invoke list_tables with one existing and one non-existent table",
@@ -2822,7 +2822,7 @@ func RunMySQLListTablesTest(t *testing.T, databaseName, tableNameParam, tableNam
name: "invoke list_tables with non-existent table",
requestBody: bytes.NewBufferString(`{"table_names": "non_existent_table"}`),
wantStatusCode: http.StatusOK,
want: nil,
want: []objectDetails{},
},
}
for _, tc := range invokeTcs {
@@ -2854,7 +2854,7 @@ func RunMySQLListTablesTest(t *testing.T, databaseName, tableNameParam, tableNam
if err := json.Unmarshal([]byte(resultString), &tables); err != nil {
t.Fatalf("failed to unmarshal outer JSON array into []tableInfo: %v", err)
}
var details []map[string]any
details := []map[string]any{}
for _, table := range tables {
var d map[string]any
if err := json.Unmarshal([]byte(table.ObjectDetails), &d); err != nil {
@@ -2864,23 +2864,19 @@ func RunMySQLListTablesTest(t *testing.T, databaseName, tableNameParam, tableNam
}
got = details
} else {
if resultString == "null" {
got = nil
} else {
var tables []tableInfo
if err := json.Unmarshal([]byte(resultString), &tables); err != nil {
t.Fatalf("failed to unmarshal outer JSON array into []tableInfo: %v", err)
}
var details []objectDetails
for _, table := range tables {
var d objectDetails
if err := json.Unmarshal([]byte(table.ObjectDetails), &d); err != nil {
t.Fatalf("failed to unmarshal nested ObjectDetails string: %v", err)
}
details = append(details, d)
}
got = details
var tables []tableInfo
if err := json.Unmarshal([]byte(resultString), &tables); err != nil {
t.Fatalf("failed to unmarshal outer JSON array into []tableInfo: %v", err)
}
details := []objectDetails{}
for _, table := range tables {
var d objectDetails
if err := json.Unmarshal([]byte(table.ObjectDetails), &d); err != nil {
t.Fatalf("failed to unmarshal nested ObjectDetails string: %v", err)
}
details = append(details, d)
}
got = details
}
opts := []cmp.Option{
@@ -2891,7 +2887,7 @@ func RunMySQLListTablesTest(t *testing.T, databaseName, tableNameParam, tableNam
// Checking only the current database where the test tables are created to avoid brittle tests.
if tc.isAllTables {
var filteredGot []objectDetails
filteredGot := []objectDetails{}
if got != nil {
for _, item := range got.([]objectDetails) {
if item.SchemaName == databaseName {
@@ -2899,11 +2895,7 @@ func RunMySQLListTablesTest(t *testing.T, databaseName, tableNameParam, tableNam
}
}
}
if len(filteredGot) == 0 {
got = nil
} else {
got = filteredGot
}
got = filteredGot
}
if diff := cmp.Diff(tc.want, got, opts...); diff != "" {
@@ -3491,7 +3483,7 @@ func RunMSSQLListTablesTest(t *testing.T, tableNameParam, tableNameAuth string)
api: "http://127.0.0.1:5000/api/tool/list_tables/invoke",
requestBody: `{"table_names": "non_existent_table"}`,
wantStatusCode: http.StatusOK,
want: `null`,
want: `[]`,
},
{
name: "invoke list_tables with one existing and one non-existent table",