You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello, recently we encountered incorrect behavior of parsing nullable enumeration by json_annotation generated code.
I guess you should return null in $enumDecodeNullable function in enum_helper instead of ArgumentError() when the unknownValue is null. Since this is a nullable enum case and it should be null as a fallback value.
if (unknownValue ==null) {
throwArgumentError( // here'`$source` is not one of the supported values: ''${enumValues.values.join(', ')}',
);
}
In our case we have different API versions and there are cases when API uses unsupported enum values, and this is expected from the mobile app perspective, so the value is defined as nullable enum in the model in our mobile app. But with your implementation the app crashes if the mapping fails.
There is a workaround to handle that case by setting unknownEnumValue: JsonKey.nullForUndefinedEnumValue in the @JsonKey annotation but I guess it is redundant for nullable enums any way.
As a general concern I would suggest to introduce a special type exception for invalid enum mapping, for example for non nullable enums (function $enumDecode) and avoid using ArgumentError. Since it's not expected to catch errors in dart code, but the API contract mismatch is a common situation so it should be represented by exception to be able to be catched and handled.
The text was updated successfully, but these errors were encountered:
Hello, recently we encountered incorrect behavior of parsing nullable enumeration by json_annotation generated code.
I guess you should return
null
in$enumDecodeNullable
function in enum_helper instead ofArgumentError()
when theunknownValue
is null. Since this is a nullable enum case and it should be null as a fallback value.In our case we have different API versions and there are cases when API uses unsupported enum values, and this is expected from the mobile app perspective, so the value is defined as nullable enum in the model in our mobile app. But with your implementation the app crashes if the mapping fails.
Ex.:
There is a workaround to handle that case by setting
unknownEnumValue: JsonKey.nullForUndefinedEnumValue
in the@JsonKey
annotation but I guess it is redundant for nullable enums any way.The text was updated successfully, but these errors were encountered: