Skip to content

Commit fc51eba

Browse files
committed
Better error output with incorrect annotations
1 parent 4db27a8 commit fc51eba

6 files changed

+36
-30
lines changed

json_serializable/lib/src/json_key_utils.dart

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ KeyConfig _from(FieldElement element, ClassConfig classAnnotation) {
4141
/// an [InvalidGenerationSourceError] using [typeInformation] to describe
4242
/// the unsupported type.
4343
Object? literalForObject(
44+
String fieldName,
4445
DartObject dartObject,
4546
Iterable<String> typeInformation,
4647
) {
@@ -65,7 +66,9 @@ KeyConfig _from(FieldElement element, ClassConfig classAnnotation) {
6566
if (badType != null) {
6667
badType = typeInformation.followedBy([badType]).join(' > ');
6768
throwUnsupported(
68-
element, '`defaultValue` is `$badType`, it must be a literal.');
69+
element,
70+
'`$fieldName` is `$badType`, it must be a literal.',
71+
);
6972
}
7073

7174
if (reader.isDouble || reader.isInt || reader.isString || reader.isBool) {
@@ -75,7 +78,7 @@ KeyConfig _from(FieldElement element, ClassConfig classAnnotation) {
7578
if (reader.isList) {
7679
return [
7780
for (var e in reader.listValue)
78-
literalForObject(e, [
81+
literalForObject(fieldName, e, [
7982
...typeInformation,
8083
'List',
8184
])
@@ -85,7 +88,7 @@ KeyConfig _from(FieldElement element, ClassConfig classAnnotation) {
8588
if (reader.isSet) {
8689
return {
8790
for (var e in reader.setValue)
88-
literalForObject(e, [
91+
literalForObject(fieldName, e, [
8992
...typeInformation,
9093
'Set',
9194
])
@@ -99,8 +102,8 @@ KeyConfig _from(FieldElement element, ClassConfig classAnnotation) {
99102
];
100103
return reader.mapValue.map(
101104
(k, v) => MapEntry(
102-
literalForObject(k!, mapTypeInformation),
103-
literalForObject(v!, mapTypeInformation),
105+
literalForObject(fieldName, k!, mapTypeInformation),
106+
literalForObject(fieldName, v!, mapTypeInformation),
104107
),
105108
);
106109
}
@@ -163,7 +166,7 @@ KeyConfig _from(FieldElement element, ClassConfig classAnnotation) {
163166
} else {
164167
final defaultValueLiteral = annotationValue.isNull
165168
? null
166-
: literalForObject(annotationValue.objectValue, []);
169+
: literalForObject(fieldName, annotationValue.objectValue, []);
167170
if (defaultValueLiteral == null) {
168171
return null;
169172
}

json_serializable/lib/src/utils.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ bool hasJsonKeyAnnotation(FieldElement element) =>
2828

2929
Never throwUnsupported(FieldElement element, String message) =>
3030
throw InvalidGenerationSourceError(
31-
'Error with `@JsonKey` on `${element.name}`. $message',
32-
element: element);
31+
'Error with `@JsonKey` on the `${element.name}` field. $message',
32+
element: element,
33+
);
3334

3435
FieldRename? _fromDartObject(ConstantReader reader) => reader.isNull
3536
? null

json_serializable/test/src/_json_serializable_test_input.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ class PrivateFieldCtorClass {
330330
}
331331

332332
@ShouldThrow(
333-
'Error with `@JsonKey` on `field`. '
333+
'Error with `@JsonKey` on the `field` field. '
334334
'Cannot set both `disallowNullValue` and `includeIfNull` to `true`. '
335335
'This leads to incompatible `toJson` and `fromJson` behavior.',
336336
element: 'field',

json_serializable/test/src/default_value_input.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
part of '_json_serializable_test_input.dart';
66

77
@ShouldThrow(
8-
'Error with `@JsonKey` on `field`. '
8+
'Error with `@JsonKey` on the `field` field. '
99
'`defaultValue` is `Symbol`, it must be a literal.',
1010
element: 'field',
1111
)
@@ -20,7 +20,7 @@ class DefaultWithSymbol {
2020
int _function() => 42;
2121

2222
@ShouldThrow(
23-
'Error with `@JsonKey` on `field`. '
23+
'Error with `@JsonKey` on the `field` field. '
2424
'`defaultValue` is `Function`, it must be a literal.',
2525
element: 'field',
2626
)
@@ -33,7 +33,7 @@ class DefaultWithFunction {
3333
}
3434

3535
@ShouldThrow(
36-
'Error with `@JsonKey` on `field`. '
36+
'Error with `@JsonKey` on the `field` field. '
3737
'`defaultValue` is `Type`, it must be a literal.',
3838
element: 'field',
3939
)
@@ -46,7 +46,7 @@ class DefaultWithType {
4646
}
4747

4848
@ShouldThrow(
49-
'Error with `@JsonKey` on `field`. '
49+
'Error with `@JsonKey` on the `field` field. '
5050
'`defaultValue` is `Duration`, it must be a literal.',
5151
element: 'field',
5252
)
@@ -61,7 +61,7 @@ class DefaultWithConstObject {
6161
enum Enum { value }
6262

6363
@ShouldThrow(
64-
'Error with `@JsonKey` on `field`. '
64+
'Error with `@JsonKey` on the `field` field. '
6565
'`defaultValue` is `List > Enum`, it must be a literal.',
6666
element: 'field',
6767
)

json_serializable/test/src/to_from_json_test_input.dart

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ Object _toObject(Object input) => throw UnimplementedError();
1515
String _toStringFromObject(Object? input) => throw UnimplementedError();
1616

1717
@ShouldThrow(
18-
'Error with `@JsonKey` on `field`. The `fromJson` function `_toInt` '
19-
'return type `int` is not compatible with field type `String`.',
18+
'Error with `@JsonKey` on the `field` field. The `fromJson` function '
19+
'`_toInt` return type `int` is not compatible with field type `String`.',
2020
element: 'field',
2121
)
2222
@JsonSerializable()
@@ -26,7 +26,7 @@ class BadFromFuncReturnType {
2626
}
2727

2828
@ShouldThrow(
29-
'Error with `@JsonKey` on `field`. The `fromJson` function '
29+
'Error with `@JsonKey` on the `field` field. The `fromJson` function '
3030
'`_twoArgFunction` must have one positional parameter.',
3131
element: 'field',
3232
)
@@ -59,7 +59,7 @@ class ValidToFromFuncClassStatic {
5959
}
6060

6161
@ShouldThrow(
62-
'Error with `@JsonKey` on `field`. The `toJson` function `_toInt` '
62+
'Error with `@JsonKey` on the `field` field. The `toJson` function `_toInt` '
6363
'argument type `bool` is not compatible with field type `String`.',
6464
element: 'field',
6565
)
@@ -70,8 +70,9 @@ class BadToFuncReturnType {
7070
}
7171

7272
@ShouldThrow(
73-
'Error with `@JsonKey` on `values`. The `fromJson` function `_fromList` '
74-
'return type `List<int>?` is not compatible with field type `List<int>`.',
73+
'Error with `@JsonKey` on the `values` field. The `fromJson` function '
74+
'`_fromList` return type `List<int>?` is not compatible with field type '
75+
'`List<int>`.',
7576
element: 'values',
7677
)
7778
@JsonSerializable()
@@ -127,7 +128,7 @@ List<List>? _toList(List<int>? pairs) =>
127128
pairs?.map((it) => [it]).toList(growable: false);
128129

129130
@ShouldThrow(
130-
'Error with `@JsonKey` on `field`. The `toJson` function '
131+
'Error with `@JsonKey` on the `field` field. The `toJson` function '
131132
'`_twoArgFunction` must have one positional parameter.',
132133
element: 'field',
133134
)
@@ -248,7 +249,7 @@ class FromNullableDynamicCollection {
248249
String _noArgs() => throw UnimplementedError();
249250

250251
@ShouldThrow(
251-
'Error with `@JsonKey` on `field`. The `fromJson` function '
252+
'Error with `@JsonKey` on the `field` field. The `fromJson` function '
252253
'`_noArgs` must have one positional parameter.',
253254
element: 'field',
254255
)
@@ -261,7 +262,7 @@ class BadNoArgs {
261262
String? _twoArgs(a, b) => null;
262263

263264
@ShouldThrow(
264-
'Error with `@JsonKey` on `field`. The `fromJson` function '
265+
'Error with `@JsonKey` on the `field` field. The `fromJson` function '
265266
'`_twoArgs` must have one positional parameter.',
266267
element: 'field',
267268
)
@@ -274,7 +275,7 @@ class BadTwoRequiredPositional {
274275
String? _oneNamed({a}) => null;
275276

276277
@ShouldThrow(
277-
'Error with `@JsonKey` on `field`. The `fromJson` function '
278+
'Error with `@JsonKey` on the `field` field. The `fromJson` function '
278279
'`_oneNamed` must have one positional parameter.',
279280
element: 'field',
280281
)

json_serializable/test/src/unknown_enum_value_test_input.dart

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class UnknownEnumValue {
3232
enum UnknownEnumValueItems { v0, v1, v2, vUnknown, vNull }
3333

3434
@ShouldThrow(
35-
'Error with `@JsonKey` on `value`. `unknownEnumValue` has type '
35+
'Error with `@JsonKey` on the `value` field. `unknownEnumValue` has type '
3636
'`int`, but the provided unknownEnumValue is of type '
3737
'`WrongEnumType`.',
3838
)
@@ -43,7 +43,7 @@ class UnknownEnumValueListWrongType {
4343
}
4444

4545
@ShouldThrow(
46-
'Error with `@JsonKey` on `value`. `unknownEnumValue` has type '
46+
'Error with `@JsonKey` on the `value` field. `unknownEnumValue` has type '
4747
'`UnknownEnumValueItems`, but the provided unknownEnumValue is of type '
4848
'`WrongEnumType`.',
4949
)
@@ -56,7 +56,7 @@ class UnknownEnumValueListWrongEnumType {
5656
enum WrongEnumType { otherValue }
5757

5858
@ShouldThrow(
59-
'Error with `@JsonKey` on `value`. `unknownEnumValue` has type '
59+
'Error with `@JsonKey` on the `value` field. `unknownEnumValue` has type '
6060
'`UnknownEnumValueItems`, but the provided unknownEnumValue is of type '
6161
'`WrongEnumType`.',
6262
)
@@ -67,7 +67,7 @@ class UnknownEnumValueWrongEnumType {
6767
}
6868

6969
@ShouldThrow(
70-
'Error with `@JsonKey` on `value`. The value provided '
70+
'Error with `@JsonKey` on the `value` field. The value provided '
7171
'for `unknownEnumValue` must be a matching enum.',
7272
)
7373
@JsonSerializable()
@@ -77,8 +77,9 @@ class UnknownEnumValueNotEnumValue {
7777
}
7878

7979
@ShouldThrow(
80-
'Error with `@JsonKey` on `value`. `unknownEnumValue` can only be set on '
81-
'fields of type enum or on Iterable, List, or Set instances of an enum type.',
80+
'Error with `@JsonKey` on the `value` field. `unknownEnumValue` can only be '
81+
'set on fields of type enum or on Iterable, List, or Set instances of an '
82+
'enum type.',
8283
)
8384
@JsonSerializable()
8485
class UnknownEnumValueNotEnumField {

0 commit comments

Comments
 (0)