Skip to content

Commit

Permalink
Support unknownEnumValue on Map values (#1093)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Beckmann committed May 26, 2023
1 parent 755d5d3 commit 6e85b1f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
4 changes: 3 additions & 1 deletion json_serializable/lib/src/json_key_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,13 @@ KeyConfig _from(FieldElement element, ClassConfig classAnnotation) {
targetEnumType = element.type;
} else if (coreIterableTypeChecker.isAssignableFromType(element.type)) {
targetEnumType = coreIterableGenericType(element.type);
} else if (coreMapTypeChecker.isAssignableFromType(element.type)) {
targetEnumType = coreMapGenericValueType(element.type);
} else {
throwUnsupported(
element,
'`$fieldName` can only be set on fields of type enum or on '
'Iterable, List, or Set instances of an enum type.',
'Iterable, List, Set or Map instances of an enum type.',
);
}

Expand Down
12 changes: 12 additions & 0 deletions json_serializable/lib/src/shared_checkers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ const coreMapTypeChecker = TypeChecker.fromUrl('dart:core#Map');
DartType coreIterableGenericType(DartType type) =>
type.typeArgumentsOf(coreIterableTypeChecker)!.single;

/// Returns the generic key type of the [Map] represented by [type].
///
/// If [type] does not extend [Map], an error is thrown.
DartType coreMapGenericKeyType(DartType type) =>
type.typeArgumentsOf(coreMapTypeChecker)![0];

/// Returns the generic value type of the [Map] represented by [type].
///
/// If [type] does not extend [Map], an error is thrown.
DartType coreMapGenericValueType(DartType type) =>
type.typeArgumentsOf(coreMapTypeChecker)![1];

/// A [TypeChecker] for [String], [bool] and [num].
const simpleJsonTypeChecker = TypeChecker.any([
coreStringTypeChecker,
Expand Down
2 changes: 2 additions & 0 deletions json_serializable/test/json_serializable_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ const _expectedAnnotatedTests = {
'UnknownEnumValue',
'UnknownEnumValueListWrongEnumType',
'UnknownEnumValueListWrongType',
'UnknownEnumValueMapValueWrongEnumType',
'UnknownEnumValueMapValueWrongType',
'UnknownEnumValueNotEnumField',
'UnknownEnumValueWrongEnumType',
'UnsupportedDateTimeField',
Expand Down
24 changes: 23 additions & 1 deletion json_serializable/test/src/unknown_enum_value_test_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,28 @@ class UnknownEnumValueListWrongEnumType {
late List<UnknownEnumValueItems> value;
}

@ShouldThrow(
'Error with `@JsonKey` on the `value` field. `unknownEnumValue` has type '
'`int`, but the provided unknownEnumValue is of type '
'`WrongEnumType`.',
)
@JsonSerializable()
class UnknownEnumValueMapValueWrongType {
@JsonKey(unknownEnumValue: WrongEnumType.otherValue)
late Map<String, int> value;
}

@ShouldThrow(
'Error with `@JsonKey` on the `value` field. `unknownEnumValue` has type '
'`UnknownEnumValueItems`, but the provided unknownEnumValue is of type '
'`WrongEnumType`.',
)
@JsonSerializable()
class UnknownEnumValueMapValueWrongEnumType {
@JsonKey(unknownEnumValue: WrongEnumType.otherValue)
late Map<String, UnknownEnumValueItems> value;
}

enum WrongEnumType { otherValue }

@ShouldThrow(
Expand All @@ -68,7 +90,7 @@ class UnknownEnumValueWrongEnumType {

@ShouldThrow(
'Error with `@JsonKey` on the `value` field. `unknownEnumValue` can only be '
'set on fields of type enum or on Iterable, List, or Set instances of an '
'set on fields of type enum or on Iterable, List, Set or Map instances of an '
'enum type.',
)
@JsonSerializable()
Expand Down

0 comments on commit 6e85b1f

Please sign in to comment.