Skip to content

Commit 01d43de

Browse files
committed
Support unknownEnumValue on Map values (#1093)
1 parent 755d5d3 commit 01d43de

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

json_serializable/lib/src/json_key_utils.dart

+3-1
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,13 @@ KeyConfig _from(FieldElement element, ClassConfig classAnnotation) {
162162
targetEnumType = element.type;
163163
} else if (coreIterableTypeChecker.isAssignableFromType(element.type)) {
164164
targetEnumType = coreIterableGenericType(element.type);
165+
} else if (coreMapTypeChecker.isAssignableFromType(element.type)) {
166+
targetEnumType = coreMapGenericValueType(element.type);
165167
} else {
166168
throwUnsupported(
167169
element,
168170
'`$fieldName` can only be set on fields of type enum or on '
169-
'Iterable, List, or Set instances of an enum type.',
171+
'Iterable, List, Set or Map instances of an enum type.',
170172
);
171173
}
172174

json_serializable/lib/src/shared_checkers.dart

+12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ const coreMapTypeChecker = TypeChecker.fromUrl('dart:core#Map');
1919
DartType coreIterableGenericType(DartType type) =>
2020
type.typeArgumentsOf(coreIterableTypeChecker)!.single;
2121

22+
/// Returns the generic key type of the [Map] represented by [type].
23+
///
24+
/// If [type] does not extend [Map], an error is thrown.
25+
DartType coreMapGenericKeyType(DartType type) =>
26+
type.typeArgumentsOf(coreMapTypeChecker)![0];
27+
28+
/// Returns the generic value type of the [Map] represented by [type].
29+
///
30+
/// If [type] does not extend [Map], an error is thrown.
31+
DartType coreMapGenericValueType(DartType type) =>
32+
type.typeArgumentsOf(coreMapTypeChecker)![1];
33+
2234
/// A [TypeChecker] for [String], [bool] and [num].
2335
const simpleJsonTypeChecker = TypeChecker.any([
2436
coreStringTypeChecker,

json_serializable/test/json_serializable_test.dart

+2
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ const _expectedAnnotatedTests = {
133133
'UnknownEnumValue',
134134
'UnknownEnumValueListWrongEnumType',
135135
'UnknownEnumValueListWrongType',
136+
'UnknownEnumValueMapValueWrongEnumType',
137+
'UnknownEnumValueMapValueWrongType',
136138
'UnknownEnumValueNotEnumField',
137139
'UnknownEnumValueWrongEnumType',
138140
'UnsupportedDateTimeField',

json_serializable/test/src/unknown_enum_value_test_input.dart

+23-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,28 @@ class UnknownEnumValueListWrongEnumType {
5353
late List<UnknownEnumValueItems> value;
5454
}
5555

56+
@ShouldThrow(
57+
'Error with `@JsonKey` on the `value` field. `unknownEnumValue` has type '
58+
'`int`, but the provided unknownEnumValue is of type '
59+
'`WrongEnumType`.',
60+
)
61+
@JsonSerializable()
62+
class UnknownEnumValueMapValueWrongType {
63+
@JsonKey(unknownEnumValue: WrongEnumType.otherValue)
64+
late Map<String, int> value;
65+
}
66+
67+
@ShouldThrow(
68+
'Error with `@JsonKey` on the `value` field. `unknownEnumValue` has type '
69+
'`UnknownEnumValueItems`, but the provided unknownEnumValue is of type '
70+
'`WrongEnumType`.',
71+
)
72+
@JsonSerializable()
73+
class UnknownEnumValueMapValueWrongEnumType {
74+
@JsonKey(unknownEnumValue: WrongEnumType.otherValue)
75+
late Map<String, UnknownEnumValueItems> value;
76+
}
77+
5678
enum WrongEnumType { otherValue }
5779

5880
@ShouldThrow(
@@ -68,7 +90,7 @@ class UnknownEnumValueWrongEnumType {
6890

6991
@ShouldThrow(
7092
'Error with `@JsonKey` on the `value` field. `unknownEnumValue` can only be '
71-
'set on fields of type enum or on Iterable, List, or Set instances of an '
93+
'set on fields of type enum or on Iterable, List, Set or Map instances of an '
7294
'enum type.',
7395
)
7496
@JsonSerializable()

0 commit comments

Comments
 (0)