Skip to content

Commit e2e4d78

Browse files
qingyang-hualcaeus
authored andcommitted
GODRIVER-3476 Escape for Regex Options.
1 parent 785d943 commit e2e4d78

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

bson/bson_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,20 @@ func TestMapCodec(t *testing.T) {
226226
}
227227

228228
func TestExtJSONEscapeKey(t *testing.T) {
229-
doc := D{{Key: "\\usb#", Value: int32(1)}}
229+
doc := D{
230+
{
231+
Key: "\\usb#",
232+
Value: int32(1),
233+
},
234+
{
235+
Key: "regex",
236+
Value: Regex{Pattern: "ab\\\\\\\"ab", Options: "\""},
237+
},
238+
}
230239
b, err := MarshalExtJSON(&doc, false, false)
231240
noerr(t, err)
232241

233-
want := "{\"\\\\usb#\":1}"
242+
want := `{"\\usb#":1,"regex":{"$regularExpression":{"pattern":"ab\\\\\\\"ab","options":"\""}}}`
234243
if diff := cmp.Diff(want, string(b)); diff != "" {
235244
t.Errorf("Marshaled documents do not match. got %v, want %v", string(b), want)
236245
}

bson/bsonrw/extjson_writer.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -468,12 +468,13 @@ func (ejvw *extJSONValueWriter) WriteRegex(pattern string, options string) error
468468
return err
469469
}
470470

471+
options = sortStringAlphebeticAscending(options)
471472
var buf bytes.Buffer
472473
buf.WriteString(`{"$regularExpression":{"pattern":`)
473474
writeStringWithEscapes(pattern, &buf, ejvw.escapeHTML)
474-
buf.WriteString(`,"options":"`)
475-
buf.WriteString(sortStringAlphebeticAscending(options))
476-
buf.WriteString(`"}},`)
475+
buf.WriteString(`,"options":`)
476+
writeStringWithEscapes(options, &buf, ejvw.escapeHTML)
477+
buf.WriteString(`}},`)
477478

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

0 commit comments

Comments
 (0)