Skip to content

Commit 253d68c

Browse files
committed
fix: relationship fields items and anyOf suppport COMPASS-9649
1 parent a502609 commit 253d68c

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed

packages/compass-data-modeling/src/store/diagram.spec.ts

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
openDiagram,
88
redoEdit,
99
undoEdit,
10+
selectFieldsForCurrentModel,
1011
} from './diagram';
1112
import type {
1213
Edit,
@@ -295,4 +296,125 @@ describe('Data Modeling store', function () {
295296
expect(diagramAfterRedo.edits[0]).to.deep.include(loadedDiagram.edits[0]);
296297
expect(diagramAfterRedo.edits[1]).to.deep.include(edit);
297298
});
299+
300+
describe('selectFieldsForCurrentModel', function () {
301+
it('should select fields from a flat schema', function () {
302+
const edits: Edit[] = [
303+
{
304+
type: 'SetModel',
305+
model: {
306+
collections: [
307+
{
308+
ns: 'collection1',
309+
indexes: [],
310+
displayPosition: [0, 0],
311+
shardKey: {},
312+
jsonSchema: {
313+
bsonType: 'object',
314+
properties: {
315+
field1: { bsonType: 'string' },
316+
field2: { bsonType: 'int' },
317+
field3: { bsonType: 'int' },
318+
},
319+
},
320+
},
321+
],
322+
relationships: [],
323+
},
324+
},
325+
];
326+
const selectedFields = selectFieldsForCurrentModel(edits);
327+
328+
expect(selectedFields).to.deep.equal({
329+
collection1: [['field1'], ['field2'], ['field3']],
330+
});
331+
});
332+
333+
it('should select fields from a nested schema', function () {
334+
const edits: Edit[] = [
335+
{
336+
type: 'SetModel',
337+
model: {
338+
collections: [
339+
{
340+
ns: 'collection1',
341+
indexes: [],
342+
displayPosition: [0, 0],
343+
shardKey: {},
344+
jsonSchema: {
345+
bsonType: 'object',
346+
properties: {
347+
prop1: { bsonType: 'string' },
348+
// Deeply nested properties
349+
prop2: {
350+
bsonType: 'object',
351+
properties: {
352+
prop2A: { bsonType: 'string' },
353+
prop2B: {
354+
bsonType: 'object',
355+
properties: {
356+
prop2B1: { bsonType: 'string' },
357+
prop2B2: { bsonType: 'int' },
358+
},
359+
},
360+
},
361+
},
362+
// Array of objects
363+
prop3: {
364+
bsonType: 'array',
365+
items: {
366+
bsonType: 'object',
367+
properties: {
368+
prop3A: { bsonType: 'string' },
369+
},
370+
},
371+
},
372+
// Mixed type with objects
373+
prop4: {
374+
anyOf: [
375+
{
376+
bsonType: 'object',
377+
properties: {
378+
prop4A: { bsonType: 'string' },
379+
},
380+
},
381+
{
382+
bsonType: 'object',
383+
properties: {
384+
prop4B: { bsonType: 'string' },
385+
},
386+
},
387+
],
388+
},
389+
},
390+
},
391+
},
392+
],
393+
relationships: [],
394+
},
395+
},
396+
];
397+
const selectedFields = selectFieldsForCurrentModel(edits);
398+
399+
expect(selectedFields).to.have.property('collection1');
400+
expect(selectedFields.collection1).to.deep.include(['prop1']);
401+
expect(selectedFields.collection1).to.deep.include(['prop2']);
402+
expect(selectedFields.collection1).to.deep.include(['prop2', 'prop2A']);
403+
expect(selectedFields.collection1).to.deep.include([
404+
'prop2',
405+
'prop2B',
406+
'prop2B1',
407+
]);
408+
expect(selectedFields.collection1).to.deep.include([
409+
'prop2',
410+
'prop2B',
411+
'prop2B2',
412+
]);
413+
expect(selectedFields.collection1).to.deep.include(['prop3']);
414+
expect(selectedFields.collection1).to.deep.include(['prop3', 'prop3A']);
415+
expect(selectedFields.collection1).to.deep.include(['prop4']);
416+
expect(selectedFields.collection1).to.deep.include(['prop4', 'prop4A']);
417+
expect(selectedFields.collection1).to.deep.include(['prop4', 'prop4B']);
418+
});
419+
});
298420
});

packages/compass-data-modeling/src/store/diagram.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,19 @@ function extractFields(
610610
parentKey?: string[],
611611
fields: string[][] = []
612612
) {
613+
if ('anyOf' in parentSchema && parentSchema.anyOf) {
614+
for (const schema of parentSchema.anyOf) {
615+
extractFields(schema, parentKey, fields);
616+
}
617+
}
618+
if ('items' in parentSchema && parentSchema.items) {
619+
const items = Array.isArray(parentSchema.items)
620+
? parentSchema.items
621+
: [parentSchema.items];
622+
for (const schema of items) {
623+
extractFields(schema, parentKey, fields);
624+
}
625+
}
613626
if ('properties' in parentSchema && parentSchema.properties) {
614627
for (const [key, value] of Object.entries(parentSchema.properties)) {
615628
const fullKey = parentKey ? [...parentKey, key] : [key];

0 commit comments

Comments
 (0)