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

Allow annotated field with JsonKey.unknownEnumValue to be of type Map #1093

Open
Wesleychung93 opened this issue Feb 10, 2022 · 2 comments
Open

Comments

@Wesleychung93
Copy link

Wesleychung93 commented Feb 10, 2022

At present, the #585 and PR #691 allows us to annotate unknownEnumValue fields of type enum or on Iterable, List, or Set instances of an enum type. Is is possible to support Map ?

Example:

@JsonKey(unknownEnumValue: YourEnum.yourUnkownValue)
final Map<YourEnum, num> minWithdrawalAmount;

This is currently not possible. Enclosed error shown in terminal when generate the code

Error with `@JsonKey` on the `minWithdrawalAmount` field. `unknownEnumValue` can only be set on fields of type enum or on Iterable, List, or Set instances of an enum type.
package:yourfile.dart:19:22
   ╷
19 │   Map<YourEnum, num> minWithdrawalAmount;
   │                      ^^^^^^^^^^^^^^^^^^^
   ╵
@Yetispapa
Copy link

Same issue here

@t-beckmann
Copy link

The unknownEnumValue could apply to the key or value of a Map.

For the value the check can be passed when adding a case near https://github.com/google/json_serializable.dart/blob/master/json_serializable/lib/src/json_key_utils.dart#L164 like so:

        late DartType targetEnumType;
        if (element.type.isEnum) {
          targetEnumType = element.type;
        } else if (coreIterableTypeChecker.isAssignableFromType(element.type)) {
          targetEnumType = coreIterableGenericType(element.type);
        } else if (coreMapTypeChecker.isAssignableFromType(element.type)) {
          // Map handled here, index 1 denotes value type
          targetEnumType = element.type.typeArgumentsOf(coreMapTypeChecker)![1];
        } else {
          throwUnsupported(
            element,
            '`$fieldName` can only be set on fields of type enum or on '
            'Iterable, List, Set or Map instances of an enum type.',
          );
        }

The code generated then already properly passes the given unknownEnumValue to $enumDecode as the unknownValue without further changes, I tried.

For the key one way would be implementing a JsonKey.unknownEnumKey in a similar fashion, and some change to https://github.com/google/json_serializable.dart/blob/master/json_serializable/lib/src/type_helpers/map_helper.dart#L115. This however is less useful as such default key(s) could result in duplicate entries so that it's presence merely indicates such unknown key was encountered.

t-beckmann pushed a commit to relution-io/json_serializable.dart that referenced this issue May 26, 2023
t-beckmann added a commit to relution-io/json_serializable.dart that referenced this issue May 26, 2023
t-beckmann added a commit to relution-io/json_serializable.dart that referenced this issue May 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants