-
Notifications
You must be signed in to change notification settings - Fork 413
JsonKey's unknownEnumValue
allows null
fallback, which will crash at runtime
#559
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
Comments
I'll take a look here... |
@tp – would you copy-paste the generated code that calls |
Sure @kevmoo, here's the setup: So we use this on a field like
Which generates this code: T _$enumDecode<T>(
Map<T, dynamic> enumValues,
dynamic source, {
T unknownValue,
}) {
if (source == null) {
throw ArgumentError('A value must be provided. Supported values: '
'${enumValues.values.join(', ')}');
}
final value = enumValues.entries
.singleWhere((e) => e.value == source, orElse: () => null)
?.key;
if (value == null && unknownValue == null) {
throw ArgumentError('`$source` is not one of the supported values: '
'${enumValues.values.join(', ')}');
}
return value ?? unknownValue;
}
const _$OrderStatusEnumMap = {
OrderStatus.confirmed: 'confirmed',
…
};
Order _$OrderFromJson(Map<String, dynamic> json) {
return $checkedNew('Order', json, () {
$checkKeys(json,
requiredKeys: const ['status'], disallowNullValues: const ['status']);
final val = Order(
status: $checkedConvert(
json, 'status', (v) => _$enumDecode(_$OrderStatusEnumMap, v)),
);
return val;
});
} If instead I set …
Order _$OrderFromJson(Map<String, dynamic> json) {
return $checkedNew('Order', json, () {
$checkKeys(json,
requiredKeys: const ['status'], disallowNullValues: const ['status']);
final val = Order(
status: $checkedConvert(
json,
'status',
(v) => _$enumDecode(_$OrderStatusEnumMap, v,
unknownValue: OrderStatus.confirmed)),
);
return val;
});
} So as you can see using |
Ah! The problem here is that we can't tell the difference between you providing We'd need to provide some sentinel value – |
Is there an update for this issue? |
It would be very handy to be able to use it like this : |
|
I wish this issue has more attention. Why do I need
Personally I prefer the |
@kevmoo It is labeled as help wanted. How can I help this? |
I created my own solution for this in #839. If I had to create a crazy hacky solution for Swift because it was also throwing an error for an unknown value and it drove me crazy. Please let's not make the same mistake here... |
If the enum is not nullable in the class, it should not give such error. Any lead on this issue? |
I'm starting on this now! |
We just hit an issue at runtime, where a JSON field defined with
unknownEnumValue: null,
would crash during parsing at runtime, due to this check in the generated code:I think if we know for sure that an unknown value of
null
is not allowed, we shouldn't allow that to be declared on theJsonKey
.On the other hand, to me it seems perfectly fine to have
null
values for unknowns (if the field is properly document and in the future marked as nullable).So to fix that, I would propose to use a sentinel value to denote "no unknownEnumValue is specified" and only err when there really is no value – else return
null
or whatever else was defined as fallback.Any thoughts on this? If desired I could implement this as a PR.
The text was updated successfully, but these errors were encountered: