Skip to content

Commit 07c9af5

Browse files
authored
fix: distinguish JavascriptWithScope COMPASS-8291 (#223)
1 parent 6a55480 commit 07c9af5

9 files changed

+17
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
".esm-wrapper.mjs"
3434
],
3535
"scripts": {
36-
"test": "nyc mocha --timeout 5000 --colors -r ts-node/register test/**/*.ts src/**/*.test.ts",
36+
"test": "nyc mocha --timeout 5000 --colors -r ts-node/register test/*.ts test/**/*.ts src/**/*.test.ts",
3737
"test-example-parse-from-file": "ts-node examples/parse-from-file.ts",
3838
"test-example-parse-schema": "ts-node examples/parse-schema.ts",
3939
"test-time": "ts-node ./test/time-testing.ts",

src/schema-analyzer.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type TypeCastMap = {
2424
Binary: Binary;
2525
Boolean: boolean;
2626
Code: Code;
27+
CodeWScope: Code;
2728
Date: Date;
2829
Decimal128: Decimal128;
2930
Double: Double;
@@ -215,6 +216,9 @@ function getBSONType(value: any): SchemaBSONType {
215216
// In the resulting schema we rename `Object` to `Document`.
216217
return 'Document';
217218
}
219+
if (bsonType === 'Code' && (value as Code).scope) {
220+
return 'CodeWScope';
221+
}
218222
return bsonType;
219223
}
220224

src/schema-convertors/internalToMongoDB.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -573,12 +573,12 @@ describe('internalSchemaToMongoDB', async function() {
573573
'javascriptWithScope'
574574
],
575575
count: 1,
576-
type: 'Code',
576+
type: 'CodeWScope',
577577
probability: 0.8,
578578
hasDuplicates: false,
579579
types: [
580580
{
581-
name: 'Code',
581+
name: 'CodeWScope',
582582
path: [
583583
'javascriptWithScope'
584584
],
@@ -595,7 +595,7 @@ describe('internalSchemaToMongoDB', async function() {
595595
}
596596
}
597597
],
598-
bsonType: 'Code'
598+
bsonType: 'CodeWScope'
599599
}
600600
]
601601
},
@@ -964,7 +964,7 @@ describe('internalSchemaToMongoDB', async function() {
964964
bsonType: 'javascript'
965965
},
966966
javascriptWithScope: {
967-
bsonType: 'javascript'
967+
bsonType: 'javascriptWithScope'
968968
},
969969
long: {
970970
bsonType: 'long'

src/schema-convertors/internalToMongoDB.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export const InternalTypeToBsonTypeMap: Record<
2626
BSONSymbol: 'symbol',
2727
Symbol: 'symbol',
2828
Code: 'javascript',
29+
CodeWScope: 'javascriptWithScope',
2930
Int32: 'int',
3031
Timestamp: 'timestamp',
3132
Long: 'long',

src/schema-convertors/internalToStandard.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -576,12 +576,12 @@ describe('internalSchemaToStandard', async function() {
576576
'javascriptWithScope'
577577
],
578578
count: 1,
579-
type: 'Code',
579+
type: 'CodeWScope',
580580
probability: 0.8,
581581
hasDuplicates: false,
582582
types: [
583583
{
584-
name: 'Code',
584+
name: 'CodeWScope',
585585
path: [
586586
'javascriptWithScope'
587587
],
@@ -598,7 +598,7 @@ describe('internalSchemaToStandard', async function() {
598598
}
599599
}
600600
],
601-
bsonType: 'Code'
601+
bsonType: 'CodeWScope'
602602
}
603603
]
604604
},
@@ -970,7 +970,7 @@ describe('internalSchemaToStandard', async function() {
970970
$ref: '#/$defs/Code'
971971
},
972972
javascriptWithScope: {
973-
$ref: '#/$defs/Code'
973+
$ref: '#/$defs/CodeWScope'
974974
},
975975
long: {
976976
type: 'integer'

src/schema-convertors/internalToStandard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const InternalTypeToStandardTypeMap: Record<
2525
DBPointer: { $ref: '#/$defs/DBPointer' },
2626
BSONSymbol: { $ref: '#/$defs/BSONSymbol' },
2727
Code: { $ref: '#/$defs/Code' },
28-
CodeWScope: { $ref: '#/$defs/Code' },
28+
CodeWScope: { $ref: '#/$defs/CodeWScope' },
2929
Int32: { type: 'integer' },
3030
Timestamp: { $ref: '#/$defs/Timestamp' },
3131
Long: { type: 'integer' },

test/all-bson-types.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ describe('using a document with all bson types', function() {
1818
'Binary',
1919
'Boolean',
2020
'Code',
21+
'CodeWScope',
2122
'Date',
2223
'Decimal128',
2324
'Double',

test/simplified-schema.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ const expected = {
9999
},
100100
javascriptWithScope: {
101101
types: [{
102-
bsonType: 'Code'
102+
bsonType: 'CodeWScope'
103103
}]
104104
},
105105
int: {

0 commit comments

Comments
 (0)