-
Notifications
You must be signed in to change notification settings - Fork 132
fix: parse adjacency-list values for non-root paths #712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
andrewkolos
wants to merge
2
commits into
flutter:main
Choose a base branch
from
andrewkolos:data-model-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -115,6 +115,13 @@ class DataContext { | |
| /// Manages the application's Object? data model and provides | ||
| /// a subscription-based mechanism for reactive UI updates. | ||
| class DataModel { | ||
| static const _a2uiValueKeys = [ | ||
| 'valueString', | ||
| 'valueNumber', | ||
| 'valueBoolean', | ||
| 'valueMap', | ||
| ]; | ||
|
|
||
| JsonMap _data = {}; | ||
| final Map<DataPath, ValueNotifier<Object?>> _subscriptions = {}; | ||
| final Map<DataPath, ValueNotifier<Object?>> _valueSubscriptions = {}; | ||
|
|
@@ -129,9 +136,20 @@ class DataModel { | |
| 'DataModel.update: path=$absolutePath, contents=' | ||
| '${const JsonEncoder.withIndent(' ').convert(contents)}', | ||
| ); | ||
| final bool isAdjacencyList = contents is List && _isAdjacencyList(contents); | ||
| final Object? parsedContents = isAdjacencyList | ||
| ? _parseDataModelContents(contents) | ||
| : contents; | ||
|
|
||
| if (absolutePath == null || absolutePath.segments.isEmpty) { | ||
| if (contents is List) { | ||
| _data = _parseDataModelContents(contents); | ||
| if (parsedContents is Map) { | ||
| _data = parsedContents as Map<String, Object?>; | ||
| } else if (parsedContents is List) { | ||
| genUiLogger.warning( | ||
| 'DataModel.update: literal list cannot be used as ' | ||
| 'root data model: $contents', | ||
| ); | ||
| _data = <String, Object?>{}; | ||
| } else if (contents is Map) { | ||
| // Permissive: Allow a map to be sent for the root, even though the | ||
| // schema expects a list. | ||
|
|
@@ -151,7 +169,7 @@ class DataModel { | |
| return; | ||
| } | ||
|
|
||
| _updateValue(_data, absolutePath.segments, contents); | ||
| _updateValue(_data, absolutePath.segments, parsedContents); | ||
| _notifySubscribers(absolutePath); | ||
| } | ||
|
|
||
|
|
@@ -184,6 +202,18 @@ class DataModel { | |
| return notifier; | ||
| } | ||
|
|
||
| /// Determines if the given contents are likely an A2UI adjacency list. | ||
| bool _isAdjacencyList(List<Object?> contents) { | ||
| if (contents.isEmpty) return false; | ||
| for (final item in contents) { | ||
| if (item is! Map) return false; | ||
| if (!item.containsKey('key')) return false; | ||
|
|
||
| if (!_a2uiValueKeys.any(item.containsKey)) return false; | ||
| } | ||
| return true; | ||
| } | ||
|
Comment on lines
+206
to
+215
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For improved conciseness and to follow a more functional style, you could consider using the bool _isAdjacencyList(List<Object?> contents) {
if (contents.isEmpty) return false;
return contents.every((item) {
if (item is! Map || !item.containsKey('key')) {
return false;
}
return _a2uiValueKeys.any(item.containsKey);
});
} |
||
|
|
||
| /// Retrieves a static, one-time value from the data model at the | ||
| /// specified absolute path without creating a subscription. | ||
| T? getValue<T>(DataPath absolutePath) { | ||
|
|
@@ -207,13 +237,7 @@ class DataModel { | |
| Object? value; | ||
| var valueCount = 0; | ||
|
|
||
| const valueKeys = [ | ||
| 'valueString', | ||
| 'valueNumber', | ||
| 'valueBoolean', | ||
| 'valueMap', | ||
| ]; | ||
| for (final valueKey in valueKeys) { | ||
| for (final String valueKey in _a2uiValueKeys) { | ||
| if (item.containsKey(valueKey)) { | ||
| if (valueCount == 0) { | ||
| if (valueKey == 'valueMap') { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
DataModel.updatemethod logs the entirecontentsobject at theWARNINGlevel when a literal list is used as the root data model. This is a medium-severity security issue as it can lead to sensitive information exposure (PII, tokens, etc.) in the application logs. Additionally, string interpolation of large or deeply nested objects can cause performance issues or even a stack overflow due to recursivetoString()calls in Dart's collection classes. It is recommended to remove the object from the log message to prevent data leakage and ensure stability.