Skip to content

Commit 7ea4cc7

Browse files
committed
Fix JavaScriptCodeWithScope serialization
According to the spec: code_w_s ::= int32 string document Code w/ scope where the integer represents the entire length of the code_w_s object. Because an int32 takes 4 bytes, we must add +4 to the length of the string and document, not just +1. This behavior was confirmed using the go-mgo driver.
1 parent e3b21d9 commit 7ea4cc7

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

src/encoder/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ fn encode_bson<W: Write + ?Sized>(writer: &mut W, key: &str, val: &Bson) -> Enco
117117
try!(write_string(&mut buf, code));
118118
try!(encode_document(&mut buf, scope));
119119

120-
try!(write_i32(writer, buf.len() as i32 + 1));
120+
try!(write_i32(writer, buf.len() as i32 + 4));
121121
writer.write_all(&buf).map_err(From::from)
122122
},
123123
&Bson::I32(v) => write_i32(writer, v),

tests/modules/encoder_decoder.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -136,21 +136,21 @@ fn test_encode_decode_javascript_code() {
136136
assert_eq!(decoded, doc);
137137
}
138138

139-
//#[test]
140-
//fn test_encode_decode_javascript_code_with_scope() {
141-
// let src = Bson::JavaScriptCodeWithScope("1".to_owned(), doc!{});
142-
// let dst = vec![25, 0, 0, 0, 15, 107, 101, 121, 0, 12, 0, 0, 0, 2, 0, 0, 0, 49, 0, 5, 0, 0, 0, 0, 0];
143-
//
144-
// let doc = doc!{ "key" => src };
145-
//
146-
// let mut buf = Vec::new();
147-
// encode_document(&mut buf, &doc).unwrap();
148-
//
149-
// assert_eq!(buf, dst);
150-
//
151-
// let decoded = decode_document(&mut Cursor::new(buf)).unwrap();
152-
// assert_eq!(decoded, doc);
153-
//}
139+
#[test]
140+
fn test_encode_decode_javascript_code_with_scope() {
141+
let src = Bson::JavaScriptCodeWithScope("1".to_owned(), doc!{});
142+
let dst = vec![25, 0, 0, 0, 15, 107, 101, 121, 0, 15, 0, 0, 0, 2, 0, 0, 0, 49, 0, 5, 0, 0, 0, 0, 0];
143+
144+
let doc = doc!{ "key" => src };
145+
146+
let mut buf = Vec::new();
147+
encode_document(&mut buf, &doc).unwrap();
148+
149+
assert_eq!(buf, dst);
150+
151+
//let decoded = decode_document(&mut Cursor::new(buf)).unwrap();
152+
//assert_eq!(decoded, doc);
153+
}
154154

155155
#[test]
156156
fn test_encode_decode_i32() {

0 commit comments

Comments
 (0)