awslabs / aws-mobile-appsync-sdk-android

Android SDK for AWS AppSync.
https://docs.amplify.aws/sdk/api/graphql/q/platform/android/
Apache License 2.0
105 stars 58 forks source link

Fix generating and parsing enums #374

Open admund opened 2 years ago

admund commented 2 years ago

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.

leffsu commented 1 year ago

Any chance this PR could be reviewed? I really don't want to write custom bash scripts to fix this simple issue

paigeyahnke commented 1 year ago

I would also be curious if there's any possibility this could be reviewed? Confirmed that the iOS version of the repo uses an unknown case to handle unknown enum values.

leffsu commented 1 year ago

In the meanwhile you can use these gradle tasks on Android while we have no fix coming soon https://gist.github.com/leffsu/c596197eb231f746303fa216a0a81486

paigeyahnke commented 1 year ago

In the meanwhile you can use these gradle tasks on Android while we have no fix coming soon https://gist.github.com/leffsu/c596197eb231f746303fa216a0a81486

Thanks! I appreciate the snippet. We'll give that a shot in the meantime.