Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return command error from findAndModify #2646

Merged
merged 17 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
lower case findandmodify works
  • Loading branch information
chilagrow committed May 16, 2023
commit 7ed9193eae844e5ac2d25f342ffe244a9da5b295
28 changes: 24 additions & 4 deletions integration/findandmodify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ func TestFindAndModifyErrors(t *testing.T) {
t.Parallel()

for name, tc := range map[string]struct {
command bson.D
err *mongo.CommandError
altMessage string
command bson.D
findAndModify string // optional, defaults to "findAndModify"
err *mongo.CommandError
altMessage string
}{
"UpsertAndRemove": {
command: bson.D{
Expand Down Expand Up @@ -122,13 +123,32 @@ func TestFindAndModifyErrors(t *testing.T) {
},
altMessage: "BSON field 'findAndModify.upsert' is the wrong type 'string', expected type 'bool'",
},
"LowerCaseCommand": {
// go driver sends `findAndModify` in camel case, but
// js driver sends `findandmodify` in lower case.
rumyantseva marked this conversation as resolved.
Show resolved Hide resolved
findAndModify: "findandmodify",
command: bson.D{
{"update", bson.D{{"$set", bson.D{{"_id", "non-existent"}}}}},
},
err: &mongo.CommandError{
Code: 66,
Name: "ImmutableField",
Message: "Plan executor error during findAndModify :: caused by :: Performing an update on the path '_id' would modify the immutable field '_id'",
},
altMessage: "Performing an update on the path '_id' would modify the immutable field '_id'",
},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()
ctx, collection := setup.Setup(t, shareddata.DocumentsStrings)

command := bson.D{{"findAndModify", collection.Name()}}
findAndModify := "findAndModify"
if tc.findAndModify != "" {
findAndModify = tc.findAndModify
}

command := bson.D{{findAndModify, collection.Name()}}
command = append(command, tc.command...)
if command.Map()["sort"] == nil {
command = append(command, bson.D{{"sort", bson.D{{"_id", 1}}}}...)
Expand Down
2 changes: 1 addition & 1 deletion internal/handlers/common/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ func HasSupportedUpdateModifiers(command string, update *types.Document) (bool,

// newUpdateError returns CommandError for findAndModify command, WriteError for other commands.
func newUpdateError(code commonerrors.ErrorCode, msg, command string) error {
if command == "findAndModify" {
if strings.EqualFold(command, "findAndModify") {
return commonerrors.NewCommandErrorMsgWithArgument(code, msg, command)
}

Expand Down
6 changes: 3 additions & 3 deletions internal/handlers/commonparams/extract_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func ExtractParams(doc *types.Document, command string, value any, l *zap.Logger
lookup := key

// If the key is the same as the command name, then it is a collection name.
if key == command {
if strings.EqualFold(key, command) {
rumyantseva marked this conversation as resolved.
Show resolved Hide resolved
rumyantseva marked this conversation as resolved.
Show resolved Hide resolved
lookup = "collection"
}

Expand Down Expand Up @@ -179,7 +179,7 @@ func lookupFieldTag(key string, value *reflect.Value) (*int, *tagOptions, error)

optionsList := strings.Split(tag, ",")

if optionsList[0] != key {
if !strings.EqualFold(optionsList[0], key) {
continue
}

Expand Down Expand Up @@ -410,7 +410,7 @@ func checkAllRequiredFieldsPopulated(v *reflect.Value, command string, keys []st
continue
}

if !slices.Contains(keys, key) {
if !slices.Contains(keys, key) && !slices.Contains(keys, strings.ToLower(key)) {
return commonerrors.NewCommandErrorMsgWithArgument(
commonerrors.ErrMissingField,
fmt.Sprintf("BSON field '%s.%s' is missing but a required field", command, key),
Expand Down