launchdarkly / android-client-sdk

LaunchDarkly Client-side SDK for Android
Other
45 stars 23 forks source link

Invalid JSON Object from LDClient #78

Closed yuriyvl closed 5 years ago

yuriyvl commented 5 years ago

This issue started happening with version 2.7.0 (version 2.6.0 works). It is still present in the latest 2.8.3.

We are getting java.lang.IllegalStateException: Not a JSON Object. Here is the snippet with version 2.6.0 that works:

2019-06-11_17-09-14

And here is the snippet with 2.7.0+:

2019-06-11_17-01-38

Note how the jsonElement is wrapped in additional "".

The exception happens when we try to extract jsonObject from jsonElement:

            if (jsonElement != null) {
                JsonObject jsonObject = jsonElement.getAsJsonObject();
...
...

The actual exception is:

    java.lang.IllegalStateException: Not a JSON Object: "{ \"tipsForTransactionType\": [ \"SalesReceipt\" ] }"
        at com.google.gson.JsonElement.getAsJsonObject(JsonElement.java:91)
gwhelanLD commented 5 years ago

Hi @yuriyvl,

Thanks for the issue report.

From looking into the issue, it seems that you are using the jsonVariation call to retrieve a flag that is set as a string in the LaunchDarkly UI. Versions of the Android SDK prior to 2.7.0 would not maintain a distinction between string and json flags in the local flag store, meaning that either type could be accessed through either variation call. In the 2.7.0 update, compatibility code was introduced to allow json flag values to be retrieved through the stringVariation call, but not the other way around.

I believe when this change was made in 2.7.0, the web UI also did not give the option of selecting a flag's variation type manually. What this meant was that when a flag was created through the web UI, it would automatically be created as a json flag if the variations consisted of valid json. I believe this is why compatibility code was not introduced to attempt parsing string flags as json in the jsonVariation call.

Ultimately we intend to remove this ambiguous conversion between string and json flags in the next major release.

Can you confirm whether this flag is created as a string flag in the LaunchDarkly web UI?

Thanks, @gwhelanLD

yuriyvl commented 5 years ago

@gwhelanLD ,

Thanks for a prompt response! That was indeed the problem. Now we just need to cleanly fix it on the back-end and upgrade.

Again, thanks for your help. @yuriyvl