Skip to content

Commit 52ddc9d

Browse files
authored
Handle null values during yaml metadata parsing validation (#104022)
1 parent b8b3b09 commit 52ddc9d

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

packages/flutter_tools/lib/src/flutter_project_metadata.dart

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,18 @@ FlutterProjectType? stringToProjectType(String value) {
5151
}
5252

5353
/// Verifies the expected yaml keys are present in the file.
54-
bool _validateMetadataMap(Object? yamlRoot, Map<String, Type> validations, Logger logger) {
55-
if (yamlRoot != null && yamlRoot is! YamlMap) {
56-
return false;
57-
}
58-
final YamlMap map = yamlRoot! as YamlMap;
54+
bool _validateMetadataMap(YamlMap map, Map<String, Type> validations, Logger logger) {
5955
bool isValid = true;
6056
for (final MapEntry<String, Object> entry in validations.entries) {
6157
if (!map.keys.contains(entry.key)) {
6258
isValid = false;
6359
logger.printTrace('The key `${entry.key}` was not found');
6460
break;
6561
}
66-
if (map[entry.key] != null && (map[entry.key] as Object).runtimeType != entry.value) {
62+
final Object? metadataValue = map[entry.key];
63+
if (metadataValue.runtimeType != entry.value) {
6764
isValid = false;
68-
logger.printTrace('The value of key `${entry.key}` in .metadata was expected to be ${entry.value} but was ${(map[entry.key] as Object).runtimeType}');
65+
logger.printTrace('The value of key `${entry.key}` in .metadata was expected to be ${entry.value} but was ${metadataValue.runtimeType}');
6966
break;
7067
}
7168
}
@@ -89,28 +86,26 @@ class FlutterProjectMetadata {
8986
} on YamlException {
9087
// Handled in _validate below.
9188
}
92-
if (yamlRoot == null || yamlRoot is! YamlMap) {
89+
if (yamlRoot is! YamlMap) {
9390
_logger.printTrace('.metadata file at ${_metadataFile.path} was empty or malformed.');
9491
return;
9592
}
96-
final YamlMap map = yamlRoot;
9793
if (_validateMetadataMap(yamlRoot, <String, Type>{'version': YamlMap}, _logger)) {
98-
final Object? versionYaml = map['version'];
99-
if (_validateMetadataMap(versionYaml, <String, Type>{
94+
final Object? versionYamlMap = yamlRoot['version'];
95+
if (versionYamlMap is YamlMap && _validateMetadataMap(versionYamlMap, <String, Type>{
10096
'revision': String,
10197
'channel': String,
10298
}, _logger)) {
103-
final YamlMap versionYamlMap = versionYaml! as YamlMap;
10499
_versionRevision = versionYamlMap['revision'] as String?;
105100
_versionChannel = versionYamlMap['channel'] as String?;
106101
}
107102
}
108103
if (_validateMetadataMap(yamlRoot, <String, Type>{'project_type': String}, _logger)) {
109-
_projectType = stringToProjectType(map['project_type'] as String);
104+
_projectType = stringToProjectType(yamlRoot['project_type'] as String);
110105
}
111-
final Object? migrationYaml = map['migration'];
112-
if (migrationYaml != null && migrationYaml is YamlMap) {
113-
migrateConfig.parseYaml(map['migration'] as YamlMap, _logger);
106+
final Object? migrationYaml = yamlRoot['migration'];
107+
if (migrationYaml is YamlMap) {
108+
migrateConfig.parseYaml(migrationYaml, _logger);
114109
}
115110
}
116111

@@ -289,13 +284,12 @@ migration:
289284
final Object? platformsYaml = map['platforms'];
290285
if (_validateMetadataMap(map, <String, Type>{'platforms': YamlList}, logger)) {
291286
if (platformsYaml is YamlList && platformsYaml.isNotEmpty) {
292-
for (final Object? platform in platformsYaml) {
293-
if (_validateMetadataMap(platform, <String, Type>{
287+
for (final YamlMap platformYamlMap in platformsYaml.whereType<YamlMap>()) {
288+
if (_validateMetadataMap(platformYamlMap, <String, Type>{
294289
'platform': String,
295290
'create_revision': String,
296291
'base_revision': String,
297292
}, logger)) {
298-
final YamlMap platformYamlMap = platform! as YamlMap;
299293
final SupportedPlatform platformString = SupportedPlatform.values.firstWhere(
300294
(SupportedPlatform val) => val.toString() == 'SupportedPlatform.${platformYamlMap['platform'] as String}'
301295
);

packages/flutter_tools/test/general.shard/flutter_project_metadata_test.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,21 @@ void main() {
5050
expect(logger.traceText, contains('.metadata file at .metadata was empty or malformed.'));
5151
});
5252

53+
testWithoutContext('projectType is populated when version is null', () {
54+
metadataFile
55+
..createSync()
56+
..writeAsStringSync('''
57+
version:
58+
project_type: plugin
59+
''');
60+
final FlutterProjectMetadata projectMetadata = FlutterProjectMetadata(metadataFile, logger);
61+
expect(projectMetadata.projectType, FlutterProjectType.plugin);
62+
expect(projectMetadata.versionChannel, isNull);
63+
expect(projectMetadata.versionRevision, isNull);
64+
65+
expect(logger.traceText, contains('The value of key `version` in .metadata was expected to be YamlMap but was Null'));
66+
});
67+
5368
testWithoutContext('projectType is populated when version is malformed', () {
5469
metadataFile
5570
..createSync()

0 commit comments

Comments
 (0)