Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/mst-query/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/mst-query/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mst-query",
"version": "4.2.0",
"version": "4.2.1",
"description": "Query library for mobx-state-tree",
"source": "src/index.ts",
"type": "module",
Expand Down
9 changes: 8 additions & 1 deletion packages/mst-query/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ export function getSubType(t: any, data?: any): any {
return getSubType(t._subtype);
}
const subTypes = t._types.map((t: any) => getSubType(t, data));
const modelWithProperties = subTypes.find((x: any) => isModelType(x) || isReferenceType(x));
// Every subtype is a model type or reference type - return the union type and let mst
// handle reconciling the types.
if (subTypes.every((x: any) => isModelType(x) || isReferenceType(x))) {
return t;
}
// If we have a union of models and primitives (null/undefined), we need to find the first model or reference type
// to enumerate the properties of the object.
const modelWithProperties = subTypes.find((x: any) => isModelType(x) || isReferenceType(x));
if (modelWithProperties) {
return getSubType(modelWithProperties, data);
}
Expand Down
32 changes: 32 additions & 0 deletions packages/mst-query/tests/mstQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1307,3 +1307,35 @@ test('initial data should only be set on mount', async () => {

configureMobx({ enforceActions: 'observed' });
});

test('union of array models', () => {
const data = {
rules: [
{
kind: 'FIXED',
fixedValue: 'Fixed value',
},
{
kind: 'FORMAT',
formatValue: 'Formatted value',
},
],
};
const Model = types.model('UnionArrayTestModel', {
rules: types.array(
types.union(
types.late(() => types.model('FixedModel', {
kind: types.literal('FIXED'),
fixedValue: types.string,
})),
types.model('FormatModel', {
kind: types.literal('FORMAT'),
formatValue: types.string,
}),
),
),
});
const result = merge(data, Model, {});
expect(result.rules[0].fixedValue).toBe('Fixed value');
expect(result.rules[1].formatValue).toBe('Formatted value');
});