Skip to content

Commit d6f30e6

Browse files
committed
fix: limit definitions based on use COMPASS-8941
1 parent d179c06 commit d6f30e6

File tree

7 files changed

+295
-201
lines changed

7 files changed

+295
-201
lines changed

src/schema-accessor.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Schema as InternalSchema } from './schema-analyzer';
2-
import { convertors } from './schema-convertors';
2+
import InternalToExpandedConvertor from './schema-convertors/internalToExpanded';
3+
import internalSchemaToMongodb from './schema-convertors/internalToMongoDB';
4+
import InternalToStandardConvertor from './schema-convertors/internalToStandard';
35
import { ExpandedJSONSchema, MongoDBJSONSchema, StandardJSONSchema } from './types';
46

57
export interface SchemaAccessor {
@@ -24,9 +26,13 @@ export class InternalSchemaBasedAccessor implements SchemaAccessor {
2426
private standardJSONSchema?: StandardJSONSchema;
2527
private mongodbJSONSchema?: MongoDBJSONSchema;
2628
private ExpandedJSONSchema?: ExpandedJSONSchema;
29+
public internalToStandardConvertor: InternalToStandardConvertor;
30+
public internalToExpandedConvertor: InternalToExpandedConvertor;
2731

2832
constructor(internalSchema: InternalSchema) {
2933
this.internalSchema = internalSchema;
34+
this.internalToStandardConvertor = new InternalToStandardConvertor();
35+
this.internalToExpandedConvertor = new InternalToExpandedConvertor();
3036
}
3137

3238
async getInternalSchema(): Promise<InternalSchema> {
@@ -38,20 +44,20 @@ export class InternalSchemaBasedAccessor implements SchemaAccessor {
3844
* https://json-schema.org/draft/2020-12/schema
3945
*/
4046
async getStandardJsonSchema(options: Options = {}): Promise<StandardJSONSchema> {
41-
return this.standardJSONSchema ??= await convertors.internalSchemaToStandard(this.internalSchema, options);
47+
return this.standardJSONSchema ??= await this.internalToStandardConvertor.convert(this.internalSchema, options);
4248
}
4349

4450
/**
4551
* Get MongoDB's $jsonSchema
4652
*/
4753
async getMongoDBJsonSchema(options: Options = {}): Promise<MongoDBJSONSchema> {
48-
return this.mongodbJSONSchema ??= await convertors.internalSchemaToMongoDB(this.internalSchema, options);
54+
return this.mongodbJSONSchema ??= await internalSchemaToMongodb(this.internalSchema, options);
4955
}
5056

5157
/**
5258
* Get expanded JSON Schema - with additional properties
5359
*/
5460
async getExpandedJSONSchema(options: Options = {}): Promise<ExpandedJSONSchema> {
55-
return this.ExpandedJSONSchema ??= await convertors.internalSchemaToExpanded(this.internalSchema, options);
61+
return this.ExpandedJSONSchema ??= await this.internalToExpandedConvertor.convert(this.internalSchema, options);
5662
}
5763
}

src/schema-convertors/index.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
import internalSchemaToExpanded from './internalToExpanded';
2-
import internalSchemaToMongoDB from './internalToMongoDB';
3-
import internalSchemaToStandard from './internalToStandard';
4-
5-
export const convertors = {
6-
internalSchemaToStandard,
7-
internalSchemaToMongoDB,
8-
internalSchemaToExpanded
9-
};
1+
export * from './internalToExpanded';
2+
export * from './internalToMongoDB';
3+
export * from './internalToStandard';

src/schema-convertors/internalToExpanded.test.ts

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import assert from 'assert';
2-
import internalSchemaToExpanded from './internalToExpanded';
32
import { RELAXED_EJSON_DEFINITIONS } from './internalToStandard';
3+
import InternalToExpandedConvertor from './internalToExpanded';
4+
import { ObjectId } from 'bson';
45

56
describe('internalSchemaToExpanded', async function() {
67
describe('Converts: ', async function() {
@@ -336,12 +337,21 @@ describe('internalSchemaToExpanded', async function() {
336337
}
337338
]
338339
};
339-
const standard = await internalSchemaToExpanded(internal);
340-
assert.deepStrictEqual(standard, {
340+
const convertor = new InternalToExpandedConvertor();
341+
const expanded = await convertor.convert(internal);
342+
const expectedDefinitions: any = RELAXED_EJSON_DEFINITIONS;
343+
delete expectedDefinitions.BSONSymbol;
344+
delete expectedDefinitions.CodeWScope;
345+
delete expectedDefinitions.DBPointer;
346+
delete expectedDefinitions.DBRef;
347+
delete expectedDefinitions.Date;
348+
delete expectedDefinitions.MinKey;
349+
delete expectedDefinitions.Undefined;
350+
assert.deepStrictEqual(expanded, {
341351
type: 'object',
342352
'x-bsonType': 'object',
343353
required: [],
344-
$defs: RELAXED_EJSON_DEFINITIONS,
354+
$defs: expectedDefinitions,
345355
properties: {
346356
_id: {
347357
$ref: '#/$defs/ObjectId',
@@ -593,12 +603,16 @@ describe('internalSchemaToExpanded', async function() {
593603
}
594604
]
595605
};
596-
const standard = await internalSchemaToExpanded(internal);
597-
assert.deepStrictEqual(standard, {
606+
const convertor = new InternalToExpandedConvertor();
607+
const expanded = await convertor.convert(internal);
608+
const expectedDefinitions = {
609+
Double: RELAXED_EJSON_DEFINITIONS.Double
610+
};
611+
assert.deepStrictEqual(expanded, {
598612
type: 'object',
599613
'x-bsonType': 'object',
600614
required: ['author'],
601-
$defs: RELAXED_EJSON_DEFINITIONS,
615+
$defs: expectedDefinitions,
602616
properties: {
603617
author: {
604618
type: 'object',
@@ -704,12 +718,13 @@ describe('internalSchemaToExpanded', async function() {
704718
}
705719
]
706720
};
707-
const standard = await internalSchemaToExpanded(internal);
708-
assert.deepStrictEqual(standard, {
721+
const convertor = new InternalToExpandedConvertor();
722+
const expanded = await convertor.convert(internal);
723+
assert.deepStrictEqual(expanded, {
709724
type: 'object',
710725
'x-bsonType': 'object',
711726
required: [],
712-
$defs: RELAXED_EJSON_DEFINITIONS,
727+
$defs: {},
713728
properties: {
714729
genres: {
715730
type: 'array',
@@ -867,12 +882,13 @@ describe('internalSchemaToExpanded', async function() {
867882
}
868883
]
869884
};
870-
const standard = await internalSchemaToExpanded(internal);
871-
assert.deepStrictEqual(standard, {
885+
const convertor = new InternalToExpandedConvertor();
886+
const expanded = await convertor.convert(internal);
887+
assert.deepStrictEqual(expanded, {
872888
type: 'object',
873889
'x-bsonType': 'object',
874890
required: [],
875-
$defs: RELAXED_EJSON_DEFINITIONS,
891+
$defs: {},
876892
properties: {
877893
genres: {
878894
type: 'array',
@@ -1002,12 +1018,13 @@ describe('internalSchemaToExpanded', async function() {
10021018
}
10031019
]
10041020
};
1005-
const standard = await internalSchemaToExpanded(internal);
1006-
assert.deepStrictEqual(standard, {
1021+
const convertor = new InternalToExpandedConvertor();
1022+
const expanded = await convertor.convert(internal);
1023+
assert.deepStrictEqual(expanded, {
10071024
type: 'object',
10081025
'x-bsonType': 'object',
10091026
required: ['arrayMixedType'],
1010-
$defs: RELAXED_EJSON_DEFINITIONS,
1027+
$defs: {},
10111028
properties: {
10121029
arrayMixedType: {
10131030
type: 'array',
@@ -1111,12 +1128,13 @@ describe('internalSchemaToExpanded', async function() {
11111128
}
11121129
]
11131130
};
1114-
const standard = await internalSchemaToExpanded(internal);
1115-
assert.deepStrictEqual(standard, {
1131+
const convertor = new InternalToExpandedConvertor();
1132+
const expanded = await convertor.convert(internal);
1133+
assert.deepStrictEqual(expanded, {
11161134
type: 'object',
11171135
'x-bsonType': 'object',
11181136
required: [],
1119-
$defs: RELAXED_EJSON_DEFINITIONS,
1137+
$defs: {},
11201138
properties: {
11211139
mixedType: {
11221140
'x-metadata': {
@@ -1252,12 +1270,13 @@ describe('internalSchemaToExpanded', async function() {
12521270
}
12531271
]
12541272
};
1255-
const standard = await internalSchemaToExpanded(internal);
1256-
assert.deepStrictEqual(standard, {
1273+
const convertor = new InternalToExpandedConvertor();
1274+
const expanded = await convertor.convert(internal);
1275+
assert.deepStrictEqual(expanded, {
12571276
type: 'object',
12581277
'x-bsonType': 'object',
12591278
required: [],
1260-
$defs: RELAXED_EJSON_DEFINITIONS,
1279+
$defs: {},
12611280
properties: {
12621281
mixedComplexType: {
12631282
'x-metadata': {
@@ -1360,12 +1379,16 @@ describe('internalSchemaToExpanded', async function() {
13601379
}
13611380
]
13621381
};
1363-
const standard = await internalSchemaToExpanded(internal);
1364-
assert.deepStrictEqual(standard, {
1382+
const convertor = new InternalToExpandedConvertor();
1383+
const expanded = await convertor.convert(internal);
1384+
const expectedDefinitions = {
1385+
ObjectId: RELAXED_EJSON_DEFINITIONS.ObjectId
1386+
};
1387+
assert.deepStrictEqual(expanded, {
13651388
type: 'object',
13661389
'x-bsonType': 'object',
13671390
required: ['mixedType'],
1368-
$defs: RELAXED_EJSON_DEFINITIONS,
1391+
$defs: expectedDefinitions,
13691392
properties: {
13701393
mixedType: {
13711394
'x-metadata': {
@@ -1507,7 +1530,8 @@ describe('internalSchemaToExpanded', async function() {
15071530
]
15081531
};
15091532
const abortController = new AbortController();
1510-
const promise = internalSchemaToExpanded(internal, { signal: abortController.signal });
1533+
const convertor = new InternalToExpandedConvertor();
1534+
const promise = convertor.convert(internal, { signal: abortController.signal });
15111535
abortController.abort(new Error('Too long, didn\'t wait.'));
15121536
await assert.rejects(promise, {
15131537
name: 'Error',

0 commit comments

Comments
 (0)