Skip to content

Commit

Permalink
Improve error output for types with type parameters (#714)
Browse files Browse the repository at this point in the history
Helps with #713
  • Loading branch information
kevmoo authored Sep 10, 2020
1 parent d09c99f commit 295b264
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions json_serializable/lib/src/helper_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ String typeToCode(DartType type) {
final typeArgumentsCode = typeArguments.map(typeToCode).join(', ');
return '${type.element.name}<$typeArgumentsCode>';
}
} else if (type is TypeParameterType) {
return '${type.getDisplayString()} (type parameter)';

This comment has been minimized.

Copy link
@Albert221

Albert221 Sep 10, 2020

Shouldn't you throw it as UnimplementedException?

This comment has been minimized.

Copy link
@kevmoo

kevmoo Sep 10, 2020

Author Collaborator

In theory, yes.

In the throwing code path, it's used for the error message. I'm going to cleanup this entire code path shortly.

This comment has been minimized.

Copy link
@kevmoo

kevmoo Sep 10, 2020

Author Collaborator

...great catch, BTW!

This comment has been minimized.

Copy link
@kevmoo

kevmoo Sep 10, 2020

Author Collaborator

See #715

}
throw UnimplementedError('(${type.runtimeType}) $type');
}
1 change: 1 addition & 0 deletions json_serializable/test/json_serializable_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const _expectedAnnotatedTests = [
'InvalidChildClassFromJson3',
'InvalidFromFunc2Args',
'InvalidToFunc2Args',
'Issue713',
'JsonConverterCtorParams',
'JsonConverterDuplicateAnnotations',
'JsonConverterNamedCtor',
Expand Down
10 changes: 10 additions & 0 deletions json_serializable/test/src/generic_test_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@

part of '_json_serializable_test_input.dart';

@ShouldThrow(
'Could not generate `fromJson` code for `result` because of type '
'`TResult (type parameter)`.\n'
'None of the provided `TypeHelper` instances support the defined type.',
)
@JsonSerializable()
class Issue713<TResult> {
List<TResult> result;
}

@ShouldGenerate(r'''
GenericClass<T, S> _$GenericClassFromJson<T extends num, S>(
Map<String, dynamic> json) {
Expand Down

0 comments on commit 295b264

Please sign in to comment.