aws-mobile-appsync-sdk-android icon indicating copy to clipboard operation
aws-mobile-appsync-sdk-android copied to clipboard

Fix generating and parsing enums

Open admund opened this issue 2 years ago • 4 comments

Issue #, if available: https://github.com/awslabs/aws-mobile-appsync-sdk-android/issues/349

Description of changes: When somebody will add a new value for any Enum in the schema and will start sending it, apps will older schema will crash. I fixed the code generator to handle empty or unknown Enum values. Fix:

  1. I added UNKNOWN enum value to every generated enum.
  2. UKNOWN will be set when the enum value is null (to by safe for kotlin) or some new one.

Old generated code:

final MyEnum something;
if (somethingStr != null) {
  something = MyEnum.valueOf(somethingStr);
} else {
  something = null;
}

New generated code:

MyEnum something;
if (somethingStr != null) {
  try {
    something = MyEnum.valueOf(somethingStr);
  } catch (IllegalArgumentException exception) {
    something = MyEnum.UNKNOWN;
  }
} else {
  something = MyEnum.UNKNOWN;
}

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

admund avatar Nov 17 '21 10:11 admund