Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support unknownEnumValue on Map values (#1093) #1326

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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