Skip to content

Commit d0452ed

Browse files
authored
Fix check for implemented interfaces (#111)
The `interfaces` getter does not behave as hoped. See dart-lang/sdk#27947 This code was safe before since List happened to explicitly have `implements Iterable`, but now that there is an `EfficientLengthIterable` interface in between it isn't working. Recursively check interfaces the same way we recursively check the superclass. This now ignores the edge case where multiple interfaces matched the tester and will return the first match.
1 parent 42b6aa7 commit d0452ed

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* Use library URIs (not names) to look up annotations in the mirror system.
44
* Loosen version constraint to allow package:build version 0.6
5+
* Fix a bug against the latest SDK checking whether List implements Iterable
56

67
## 0.5.1+7
78

lib/generators/json_serializable_generator.dart

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -278,15 +278,10 @@ ParameterizedType _typeTest(
278278
if (tester(type)) return type;
279279

280280
if (type is InterfaceType) {
281-
var items = type.interfaces.where(tester).toList();
281+
var tests = type.interfaces.map((type) => _typeTest(type, tester));
282+
var match = _firstNonNull(tests);
282283

283-
if (items.length > 1) {
284-
throw 'weird - more than 1 interface matches the type test - $items';
285-
}
286-
287-
if (items.length == 1) {
288-
return items.single;
289-
}
284+
if (match != null) return match;
290285

291286
if (type.superclass != null) {
292287
return _typeTest(type.superclass, tester);
@@ -295,6 +290,9 @@ ParameterizedType _typeTest(
295290
return null;
296291
}
297292

293+
/*=T*/ _firstNonNull/*<T>*/(Iterable/*<T>*/ values) =>
294+
values.firstWhere((value) => value != null, orElse: () => null);
295+
298296
bool _isDartIterable(DartType type) {
299297
return type.element.library != null &&
300298
type.element.library.isDartCore &&

0 commit comments

Comments
 (0)