Skip to content

Commit e5f0433

Browse files
committed
fixes
1 parent cade930 commit e5f0433

File tree

2 files changed

+53
-21
lines changed

2 files changed

+53
-21
lines changed

packages/compass-data-modeling/src/utils/schema-traversal.spec.tsx

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,11 +1187,13 @@ describe('getSchemaForNewTypes', function () {
11871187
const result = getSchemaForNewTypes(oldSchema, newTypes);
11881188
expect(result).not.to.have.property('bsonType');
11891189
expect(result.anyOf).to.have.lengthOf(3);
1190-
expect(result.anyOf).to.deep.include(oldSchema.anyOf[0]);
1191-
expect(result.anyOf).to.deep.include(oldSchema.anyOf[1]);
1192-
expect(result.anyOf).to.deep.include({
1193-
bsonType: 'bool',
1194-
});
1190+
expect(result.anyOf).to.have.deep.members([
1191+
oldSchema.anyOf[0],
1192+
oldSchema.anyOf[1],
1193+
{
1194+
bsonType: 'bool',
1195+
},
1196+
]);
11951197
});
11961198

11971199
it('adds object alongside a string', function () {
@@ -1228,6 +1230,46 @@ describe('getSchemaForNewTypes', function () {
12281230
items: {},
12291231
});
12301232
});
1233+
1234+
it('adds string alongside an object', function () {
1235+
const newTypes = ['string', 'object'];
1236+
const oldSchema = {
1237+
bsonType: 'object',
1238+
properties: {
1239+
name: { bsonType: 'string' },
1240+
},
1241+
required: ['name'],
1242+
};
1243+
const result = getSchemaForNewTypes(oldSchema, newTypes);
1244+
expect(result).not.to.have.property('bsonType');
1245+
expect(result).not.to.have.property('properties');
1246+
expect(result).not.to.have.property('required');
1247+
expect(result.anyOf).to.have.lengthOf(2);
1248+
expect(result.anyOf).to.have.deep.members([
1249+
{
1250+
bsonType: 'string',
1251+
},
1252+
oldSchema,
1253+
]);
1254+
});
1255+
1256+
it('adds string alongside an array', function () {
1257+
const newTypes = ['string', 'array'];
1258+
const oldSchema = {
1259+
bsonType: 'array',
1260+
items: { bsonType: 'int' },
1261+
};
1262+
const result = getSchemaForNewTypes(oldSchema, newTypes);
1263+
expect(result).not.to.have.property('bsonType');
1264+
expect(result).not.to.have.property('items');
1265+
expect(result.anyOf).to.have.lengthOf(2);
1266+
expect(result.anyOf).to.have.deep.members([
1267+
{
1268+
bsonType: 'string',
1269+
},
1270+
oldSchema,
1271+
]);
1272+
});
12311273
});
12321274

12331275
describe('cleans up anyOf when it is no longer needed', function () {

packages/compass-data-modeling/src/utils/schema-traversal.tsx

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,10 @@ const applySchemaUpdate = ({
220220
);
221221
return {
222222
...schema,
223-
properties: Object.fromEntries(
224-
Object.entries(schema.properties).map(([key, value]) =>
225-
key === fieldName ? [key, newFieldSchema] : [key, value]
226-
)
227-
),
223+
properties: {
224+
...schema.properties,
225+
[fieldName]: newFieldSchema,
226+
},
228227
};
229228
}
230229
default:
@@ -370,16 +369,10 @@ export function getSchemaForNewTypes(
370369
): MongoDBJSONSchema {
371370
const oldTypes = getFieldTypes(oldSchema);
372371
if (oldTypes.join(',') === newTypes.join(',')) return oldSchema;
373-
const newSchema: MongoDBJSONSchema = { ...oldSchema };
374-
delete newSchema.anyOf;
375372

376373
// Simple schema - new type does includes neither object nor array
377374
if (!newTypes.some((t) => t === 'object' || t === 'array')) {
378-
newSchema.bsonType = newTypes;
379-
delete newSchema.properties;
380-
delete newSchema.items;
381-
delete newSchema.required;
382-
return newSchema;
375+
return { bsonType: newTypes };
383376
}
384377

385378
// Complex schema
@@ -398,8 +391,5 @@ export function getSchemaForNewTypes(
398391
if (newVariants.length === 1) {
399392
return newVariants[0];
400393
}
401-
newSchema.anyOf = newVariants;
402-
delete newSchema.bsonType;
403-
404-
return newSchema;
394+
return { anyOf: newVariants };
405395
}

0 commit comments

Comments
 (0)