-
Notifications
You must be signed in to change notification settings - Fork 57
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
unknownEnumValue support for json_serializable #106
Comments
I do not see why that would be necessary. We are using enums to specifically escape implementing default behaviour and this addition will go against it. |
@Sancene Lets say I have this version of the enum and a request which returns an array:
When I change the backend implementation by ie. adding "anotherFoo" and send a type with anotherFoo, the getMenuItems will fail. With the fallback to an unkown type the json response can still be deserialized and the interface change can be handled gracefully:
|
I'm thinking about such a solution, in idea it would catch all unknown values and set $unknown as default. @JsonEnum()
enum Groups {
@JsonValue(1)
value1,
@JsonValue(2)
value2,
/// Default value for all unparsed values, allows backward compatibility when adding new values on the backend
@JsonValue(null)
$unknown;
} |
@StarProxima I tried:
json_serializable threats |
Yes, in its purest form, it unfortunately doesn't work. We can define The This approach seems promising to me, just need to check if using @JsonEnum()
enum Groups {
@JsonValue(1)
value1(1),
@JsonValue(2)
value2(2),
/// Default value for all unparsed values, allows backward compatibility when adding new values on the backend
$unknown(null);
const Groups(this.json);
final int? json;
factory Groups.fromJson(String? json) => values.firstWhere(
(e) => e.json == json,
orElse: () => $unknown,
);
} Here's what Before (for // ...
groups: (json['groups'] as List<dynamic>?)
?.map((e) => $enumDecode(_$GroupsEnumMap, e))
.toList() ??
const [],
// ... After (for // ...
groups: (json['groups'] as List<dynamic>?)
?.map((e) => Groups.fromJson(e as String?))
.toList() ??
const [],
// ... |
It works with retrofit Before: final value = ApiProviders.values.firstWhere(
(e) => e.name == _result.data,
orElse: () => throw ArgumentError(
'ApiProviders does not contain value ${_result.data}',
),
); After (with final value = ApiProviders.fromJson(_result.data!); @Carapacik What do you think about this approach, any other ideas? |
If we apply this approach for enum Groups {
value1(1),
value2(2),
/// Default value for all unparsed values, allows backward compatibility when adding new values on the backend
$unknown(null);
const Groups(this.json);
final int? json;
factory Groups.fromJson(String? json) => values.firstWhere(
(e) => e.json == json,
orElse: () => $unknown,
);
int? toJson() => json;
} |
Could you please add an option that enums are generated with unkown value? This enables enums to be more safe for future additions.
json_serializable does unfortunately not support null values for unkown cases, but you can define a fallback value like unkown.
The configuration in json_serializable for this looks like this:
The text was updated successfully, but these errors were encountered: