File tree Expand file tree Collapse file tree 4 files changed +29
-16
lines changed
test/integration/node-specific/bson-options Expand file tree Collapse file tree 4 files changed +29
-16
lines changed Original file line number Diff line number Diff line change @@ -520,7 +520,22 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
520
520
) ;
521
521
}
522
522
523
- yield returnAs == null ? document . toObject ( options ) : document ;
523
+ if ( returnAs == null ) {
524
+ // If `documentsReturnedIn` not set or raw is not enabled, use input bson options
525
+ // Otherwise, support raw flag. Raw only works for cursors that hardcode firstBatch/nextBatch fields
526
+ const bsonOptions =
527
+ options . documentsReturnedIn == null || ! options . raw
528
+ ? options
529
+ : {
530
+ ...options ,
531
+ raw : false ,
532
+ fieldsAsRaw : { [ options . documentsReturnedIn ] : true }
533
+ } ;
534
+ yield document . toObject ( bsonOptions ) ;
535
+ } else {
536
+ yield document ;
537
+ }
538
+
524
539
this . throwIfAborted ( ) ;
525
540
}
526
541
} catch ( error ) {
Original file line number Diff line number Diff line change @@ -79,13 +79,18 @@ export class MongoDBResponse extends OnDemandDocument {
79
79
return this . clusterTime ?? null ;
80
80
}
81
81
82
- /** TODO improve the special case of enableUtf8Validation for writeErrors */
83
82
public override toObject ( options : BSONSerializeOptions = { } ) : Record < string , any > {
84
- const optionsWithValidation = {
85
- ...options ,
83
+ const exactBSONOptions = {
84
+ useBigInt64 : options . useBigInt64 ,
85
+ promoteLongs : options . promoteLongs ,
86
+ promoteValues : options . promoteValues ,
87
+ promoteBuffers : options . promoteBuffers ,
88
+ bsonRegExp : options . bsonRegExp ,
89
+ raw : options . raw ?? false ,
90
+ fieldsAsRaw : options . fieldsAsRaw ?? { } ,
86
91
validation : this . parseBsonSerializationOptions ( options )
87
92
} ;
88
- return super . toObject ( optionsWithValidation ) ;
93
+ return super . toObject ( exactBSONOptions ) ;
89
94
}
90
95
91
96
private parseBsonSerializationOptions ( { enableUtf8Validation } : BSONSerializeOptions ) : {
Original file line number Diff line number Diff line change @@ -265,7 +265,6 @@ export type {
265
265
OpQueryOptions ,
266
266
OpQueryRequest ,
267
267
OpReply ,
268
- OpResponseOptions ,
269
268
WriteProtocolMessageType
270
269
} from './cmap/commands' ;
271
270
export type { HandshakeDocument } from './cmap/connect' ;
Original file line number Diff line number Diff line change 1
1
import { expect } from 'chai' ;
2
2
3
- import {
4
- type Collection ,
5
- type MongoClient ,
6
- MongoCompatibilityError ,
7
- ObjectId
8
- } from '../../../mongodb' ;
9
-
10
- describe . only ( 'raw bson support' , ( ) => {
11
- // TODO: Fix raw option
3
+ import { type Collection , type MongoClient , ObjectId } from '../../../mongodb' ;
4
+
5
+ describe ( 'raw bson support' , ( ) => {
12
6
describe ( 'raw' , ( ) => {
13
7
describe ( 'option inheritance' , ( ) => {
14
8
// define client and option for tests to use
@@ -33,7 +27,7 @@ describe.only('raw bson support', () => {
33
27
if ( passOptionTo === 'client' ) {
34
28
// TODO(NODE-3946): When the raw option is passed to the client it crashed parsing hellos
35
29
// since they are returned as buffers and not js objects
36
- expect ( insertResult ) . to . be . instanceOf ( MongoCompatibilityError ) ;
30
+ expect ( insertResult ) . to . be . instanceOf ( Error ) ; // Does it matter what error? This is just a known limitation
37
31
} else {
38
32
expect ( insertResult ) . to . have . property ( 'insertedId' ) . that . is . instanceOf ( ObjectId ) ;
39
33
expect ( findOneResult ) . to . be . instanceOf ( Buffer ) ;
You can’t perform that action at this time.
0 commit comments