fix(tools/mongodb): remove required tag from the canonical field (#2099)

The `required` tag raises validation failure error when a boolean field
is defined as `false`:
```
ERROR "unable to parse tool file at "mongodb_tools.yaml": unable to parse tool "insert-one-device" as kind "mongodb-insert-one": [2:12] Key: 'Config.Canonical' Error:Field validation for 'Canonical' failed on the 'required' tag\n   1 | authRequired: []\n>  2 | canonical: false\n                  ^\n   3 | collection: Device\n   4 | database: xiar\n   5 | description: Inserts a single new document into the Device collection. The 'data' parameter must be a string containing the JSON object to insert.\n   6 | "
```
All the `required` tags are removed from the boolean `canonical` field
of the MongoDB tools. Unit tests are added.
This commit is contained in:
Wenxin Du
2025-12-04 13:25:16 -05:00
committed by GitHub
parent 155bff80c1
commit 744214e04c
12 changed files with 374 additions and 46 deletions

View File

@@ -54,7 +54,7 @@ type Config struct {
Description string `yaml:"description" validate:"required"`
Database string `yaml:"database" validate:"required"`
Collection string `yaml:"collection" validate:"required"`
Canonical bool `yaml:"canonical" validate:"required"` //i want to force the user to choose
Canonical bool `yaml:"canonical"`
}
// validate interface

View File

@@ -39,6 +39,30 @@ func TestParseFromYamlMongoQuery(t *testing.T) {
{
desc: "basic example",
in: `
tools:
example_tool:
kind: mongodb-insert-many
source: my-instance
description: some description
database: test_db
collection: test_coll
`,
want: server.ToolConfigs{
"example_tool": mongodbinsertmany.Config{
Name: "example_tool",
Kind: "mongodb-insert-many",
Source: "my-instance",
AuthRequired: []string{},
Database: "test_db",
Collection: "test_coll",
Description: "some description",
Canonical: false,
},
},
},
{
desc: "true canonical",
in: `
tools:
example_tool:
kind: mongodb-insert-many
@@ -61,6 +85,31 @@ func TestParseFromYamlMongoQuery(t *testing.T) {
},
},
},
{
desc: "false canonical",
in: `
tools:
example_tool:
kind: mongodb-insert-many
source: my-instance
description: some description
database: test_db
collection: test_coll
canonical: false
`,
want: server.ToolConfigs{
"example_tool": mongodbinsertmany.Config{
Name: "example_tool",
Kind: "mongodb-insert-many",
Source: "my-instance",
AuthRequired: []string{},
Database: "test_db",
Collection: "test_coll",
Description: "some description",
Canonical: false,
},
},
},
}
for _, tc := range tcs {
t.Run(tc.desc, func(t *testing.T) {

View File

@@ -54,7 +54,7 @@ type Config struct {
Description string `yaml:"description" validate:"required"`
Database string `yaml:"database" validate:"required"`
Collection string `yaml:"collection" validate:"required"`
Canonical bool `yaml:"canonical" validate:"required"` //i want to force the user to choose
Canonical bool `yaml:"canonical"`
}
// validate interface

View File

@@ -39,6 +39,30 @@ func TestParseFromYamlMongoQuery(t *testing.T) {
{
desc: "basic example",
in: `
tools:
example_tool:
kind: mongodb-insert-one
source: my-instance
description: some description
database: test_db
collection: test_coll
`,
want: server.ToolConfigs{
"example_tool": mongodbinsertone.Config{
Name: "example_tool",
Kind: "mongodb-insert-one",
Source: "my-instance",
AuthRequired: []string{},
Database: "test_db",
Collection: "test_coll",
Canonical: false,
Description: "some description",
},
},
},
{
desc: "true canonical",
in: `
tools:
example_tool:
kind: mongodb-insert-one
@@ -61,6 +85,31 @@ func TestParseFromYamlMongoQuery(t *testing.T) {
},
},
},
{
desc: "false canonical",
in: `
tools:
example_tool:
kind: mongodb-insert-one
source: my-instance
description: some description
database: test_db
collection: test_coll
canonical: false
`,
want: server.ToolConfigs{
"example_tool": mongodbinsertone.Config{
Name: "example_tool",
Kind: "mongodb-insert-one",
Source: "my-instance",
AuthRequired: []string{},
Database: "test_db",
Collection: "test_coll",
Canonical: false,
Description: "some description",
},
},
},
}
for _, tc := range tcs {
t.Run(tc.desc, func(t *testing.T) {

View File

@@ -56,7 +56,7 @@ type Config struct {
FilterParams parameters.Parameters `yaml:"filterParams"`
UpdatePayload string `yaml:"updatePayload" validate:"required"`
UpdateParams parameters.Parameters `yaml:"updateParams" validate:"required"`
Canonical bool `yaml:"canonical" validate:"required"`
Canonical bool `yaml:"canonical"`
Upsert bool `yaml:"upsert"`
}

View File

@@ -40,6 +40,62 @@ func TestParseFromYamlMongoQuery(t *testing.T) {
{
desc: "basic example",
in: `
tools:
example_tool:
kind: mongodb-update-many
source: my-instance
description: some description
database: test_db
collection: test_coll
filterPayload: |
{ name: {{json .name}} }
filterParams:
- name: name
type: string
description: small description
updatePayload: |
{ $set: { name: {{json .name}} } }
updateParams:
- name: name
type: string
description: small description
`,
want: server.ToolConfigs{
"example_tool": mongodbupdatemany.Config{
Name: "example_tool",
Kind: "mongodb-update-many",
Source: "my-instance",
AuthRequired: []string{},
Database: "test_db",
Collection: "test_coll",
FilterPayload: "{ name: {{json .name}} }\n",
FilterParams: parameters.Parameters{
&parameters.StringParameter{
CommonParameter: parameters.CommonParameter{
Name: "name",
Type: "string",
Desc: "small description",
},
},
},
UpdatePayload: "{ $set: { name: {{json .name}} } }\n",
UpdateParams: parameters.Parameters{
&parameters.StringParameter{
CommonParameter: parameters.CommonParameter{
Name: "name",
Type: "string",
Desc: "small description",
},
},
},
Description: "some description",
Canonical: false,
},
},
},
{
desc: "true canonical",
in: `
tools:
example_tool:
kind: mongodb-update-many
@@ -94,6 +150,63 @@ func TestParseFromYamlMongoQuery(t *testing.T) {
},
},
},
{
desc: "false canonical",
in: `
tools:
example_tool:
kind: mongodb-update-many
source: my-instance
description: some description
database: test_db
collection: test_coll
filterPayload: |
{ name: {{json .name}} }
filterParams:
- name: name
type: string
description: small description
canonical: false
updatePayload: |
{ $set: { name: {{json .name}} } }
updateParams:
- name: name
type: string
description: small description
`,
want: server.ToolConfigs{
"example_tool": mongodbupdatemany.Config{
Name: "example_tool",
Kind: "mongodb-update-many",
Source: "my-instance",
AuthRequired: []string{},
Database: "test_db",
Collection: "test_coll",
FilterPayload: "{ name: {{json .name}} }\n",
FilterParams: parameters.Parameters{
&parameters.StringParameter{
CommonParameter: parameters.CommonParameter{
Name: "name",
Type: "string",
Desc: "small description",
},
},
},
UpdatePayload: "{ $set: { name: {{json .name}} } }\n",
UpdateParams: parameters.Parameters{
&parameters.StringParameter{
CommonParameter: parameters.CommonParameter{
Name: "name",
Type: "string",
Desc: "small description",
},
},
},
Description: "some description",
Canonical: false,
},
},
},
}
for _, tc := range tcs {
t.Run(tc.desc, func(t *testing.T) {

View File

@@ -57,7 +57,7 @@ type Config struct {
UpdatePayload string `yaml:"updatePayload" validate:"required"`
UpdateParams parameters.Parameters `yaml:"updateParams" validate:"required"`
Canonical bool `yaml:"canonical" validate:"required"`
Canonical bool `yaml:"canonical"`
Upsert bool `yaml:"upsert"`
}

View File

@@ -40,6 +40,123 @@ func TestParseFromYamlMongoQuery(t *testing.T) {
{
desc: "basic example",
in: `
tools:
example_tool:
kind: mongodb-update-one
source: my-instance
description: some description
database: test_db
collection: test_coll
filterPayload: |
{ name: {{json .name}} }
filterParams:
- name: name
type: string
description: small description
updatePayload: |
{ $set : { item: {{json .item}} } }
updateParams:
- name: item
type: string
description: small description
upsert: true
`,
want: server.ToolConfigs{
"example_tool": mongodbupdateone.Config{
Name: "example_tool",
Kind: "mongodb-update-one",
Source: "my-instance",
AuthRequired: []string{},
Database: "test_db",
Collection: "test_coll",
Canonical: false,
FilterPayload: "{ name: {{json .name}} }\n",
FilterParams: parameters.Parameters{
&parameters.StringParameter{
CommonParameter: parameters.CommonParameter{
Name: "name",
Type: "string",
Desc: "small description",
},
},
},
UpdatePayload: "{ $set : { item: {{json .item}} } }\n",
UpdateParams: parameters.Parameters{
&parameters.StringParameter{
CommonParameter: parameters.CommonParameter{
Name: "item",
Type: "string",
Desc: "small description",
},
},
},
Upsert: true,
Description: "some description",
},
},
},
{
desc: "false canonical",
in: `
tools:
example_tool:
kind: mongodb-update-one
source: my-instance
description: some description
database: test_db
collection: test_coll
filterPayload: |
{ name: {{json .name}} }
filterParams:
- name: name
type: string
description: small description
updatePayload: |
{ $set : { item: {{json .item}} } }
updateParams:
- name: item
type: string
description: small description
canonical: false
upsert: true
`,
want: server.ToolConfigs{
"example_tool": mongodbupdateone.Config{
Name: "example_tool",
Kind: "mongodb-update-one",
Source: "my-instance",
AuthRequired: []string{},
Database: "test_db",
Collection: "test_coll",
Canonical: false,
FilterPayload: "{ name: {{json .name}} }\n",
FilterParams: parameters.Parameters{
&parameters.StringParameter{
CommonParameter: parameters.CommonParameter{
Name: "name",
Type: "string",
Desc: "small description",
},
},
},
UpdatePayload: "{ $set : { item: {{json .item}} } }\n",
UpdateParams: parameters.Parameters{
&parameters.StringParameter{
CommonParameter: parameters.CommonParameter{
Name: "item",
Type: "string",
Desc: "small description",
},
},
},
Upsert: true,
Description: "some description",
},
},
},
{
desc: "true canonical",
in: `
tools:
example_tool:
kind: mongodb-update-one