Skip to content

Commit 824f17c

Browse files
committed
fix: raw option
1 parent aa0e3a9 commit 824f17c

File tree

4 files changed

+29
-16
lines changed

4 files changed

+29
-16
lines changed

src/cmap/connection.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,22 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
520520
);
521521
}
522522

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+
524539
this.throwIfAborted();
525540
}
526541
} catch (error) {

src/cmap/wire_protocol/responses.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,18 @@ export class MongoDBResponse extends OnDemandDocument {
7979
return this.clusterTime ?? null;
8080
}
8181

82-
/** TODO improve the special case of enableUtf8Validation for writeErrors */
8382
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 ?? {},
8691
validation: this.parseBsonSerializationOptions(options)
8792
};
88-
return super.toObject(optionsWithValidation);
93+
return super.toObject(exactBSONOptions);
8994
}
9095

9196
private parseBsonSerializationOptions({ enableUtf8Validation }: BSONSerializeOptions): {

src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,6 @@ export type {
265265
OpQueryOptions,
266266
OpQueryRequest,
267267
OpReply,
268-
OpResponseOptions,
269268
WriteProtocolMessageType
270269
} from './cmap/commands';
271270
export type { HandshakeDocument } from './cmap/connect';

test/integration/node-specific/bson-options/raw.test.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
import { expect } from 'chai';
22

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', () => {
126
describe('raw', () => {
137
describe('option inheritance', () => {
148
// define client and option for tests to use
@@ -33,7 +27,7 @@ describe.only('raw bson support', () => {
3327
if (passOptionTo === 'client') {
3428
// TODO(NODE-3946): When the raw option is passed to the client it crashed parsing hellos
3529
// 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
3731
} else {
3832
expect(insertResult).to.have.property('insertedId').that.is.instanceOf(ObjectId);
3933
expect(findOneResult).to.be.instanceOf(Buffer);

0 commit comments

Comments
 (0)