Skip to content
Draft
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
8 changes: 6 additions & 2 deletions packages/genui/lib/src/model/data_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,13 @@ class DataModel {
'DataModel.update: path=$absolutePath, contents='
'${const JsonEncoder.withIndent(' ').convert(contents)}',
);
final Object? parsedContents = contents is List
? _parseDataModelContents(contents)
: contents;

if (absolutePath == null || absolutePath.segments.isEmpty) {
if (contents is List) {
_data = _parseDataModelContents(contents);
_data = parsedContents as Map<String, Object?>;
} else if (contents is Map) {
// Permissive: Allow a map to be sent for the root, even though the
// schema expects a list.
Expand All @@ -151,7 +155,7 @@ class DataModel {
return;
}

_updateValue(_data, absolutePath.segments, contents);
_updateValue(_data, absolutePath.segments, parsedContents);
_notifySubscribers(absolutePath);
}

Expand Down
16 changes: 16 additions & 0 deletions packages/genui/test/model/data_model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,22 @@ void main() {
]);
expect(dataModel.getValue<Object?>(DataPath('/f')), isNull);
});

test('parses contents for non-root paths', () {
dataModel.update(DataPath('/todos'), <Object?>[
{
'key': '0',
'valueMap': <Object?>[
{'key': 'id', 'valueString': 'abc'},
{'key': 'title', 'valueString': 'Buy groceries'},
],
},
]);
expect(
dataModel.getValue<Map<Object?, Object?>>(DataPath('/todos/0')),
{'id': 'abc', 'title': 'Buy groceries'},
);
});
});
});

Expand Down
Loading