Skip to content

Commit

Permalink
Merge pull request #1980 from mongodb/merge-release/2.1-into-master-1…
Browse files Browse the repository at this point in the history
…741384077659

Merge release/2.1 into master
  • Loading branch information
mongodb-drivers-pr-bot[bot] authored Mar 7, 2025
2 parents b225485 + fb62199 commit 21a1d94
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
13 changes: 11 additions & 2 deletions bson/bson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,11 +584,20 @@ func TestMapCodec(t *testing.T) {
}

func TestExtJSONEscapeKey(t *testing.T) {
doc := D{{Key: "\\usb#", Value: int32(1)}}
doc := D{
{
Key: "\\usb#",
Value: int32(1),
},
{
Key: "regex",
Value: Regex{Pattern: "ab\\\\\\\"ab", Options: "\""},
},
}
b, err := MarshalExtJSON(&doc, false, false)
noerr(t, err)

want := "{\"\\\\usb#\":1}"
want := `{"\\usb#":1,"regex":{"$regularExpression":{"pattern":"ab\\\\\\\"ab","options":"\""}}}`
if diff := cmp.Diff(want, string(b)); diff != "" {
t.Errorf("Marshaled documents do not match. got %v, want %v", string(b), want)
}
Expand Down
7 changes: 4 additions & 3 deletions bson/extjson_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,12 +407,13 @@ func (ejvw *extJSONValueWriter) WriteRegex(pattern string, options string) error
return err
}

options = sortStringAlphebeticAscending(options)
var buf bytes.Buffer
buf.WriteString(`{"$regularExpression":{"pattern":`)
writeStringWithEscapes(pattern, &buf, ejvw.escapeHTML)
buf.WriteString(`,"options":"`)
buf.WriteString(sortStringAlphebeticAscending(options))
buf.WriteString(`"}},`)
buf.WriteString(`,"options":`)
writeStringWithEscapes(options, &buf, ejvw.escapeHTML)
buf.WriteString(`}},`)

ejvw.buf = append(ejvw.buf, buf.Bytes()...)

Expand Down

0 comments on commit 21a1d94

Please sign in to comment.