From e0bc47c094ca04982ef08baa4584a47593572fc8 Mon Sep 17 00:00:00 2001 From: Devin Morgan Date: Fri, 12 Jul 2024 09:17:23 -0400 Subject: [PATCH] Fix BT response schema bug from API-VERSION 2024-01-08 I'm not sure why BT responses all of a sudden stopped returning as strings and started returning as Gson representations. These changes seem to mitigate the issue though. Signed-off-by: Devin Morgan --- .../services/vault/bt/BTResponseParser.kt | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/forage-android/src/main/java/com/joinforage/forage/android/ecom/services/vault/bt/BTResponseParser.kt b/forage-android/src/main/java/com/joinforage/forage/android/ecom/services/vault/bt/BTResponseParser.kt index 21ffc2d0..09990e65 100644 --- a/forage-android/src/main/java/com/joinforage/forage/android/ecom/services/vault/bt/BTResponseParser.kt +++ b/forage-android/src/main/java/com/joinforage/forage/android/ecom/services/vault/bt/BTResponseParser.kt @@ -6,6 +6,7 @@ import com.joinforage.forage.android.core.services.forageapi.network.ForageApiRe import com.joinforage.forage.android.core.services.forageapi.network.ForageError import com.joinforage.forage.android.core.services.forageapi.network.UnknownErrorApiResponse import com.joinforage.forage.android.core.services.vault.VaultResponseParser +import org.json.JSONObject internal class BTResponseParser(btRes: Result) : VaultResponseParser { override val isNullResponse: Boolean = false @@ -63,9 +64,21 @@ internal class BTResponseParser(btRes: Result) : VaultResponseParser { return if (!isSuccessful) { null } else { - // the BT response get's parsed as a Map instead of String - val jsonResponse = Gson().toJson(vaultResponse) - ForageApiResponse.Success(jsonResponse.toString()) + // BT uses the Gson package under the hood to work with JSON + // Unfortunately, they are returning Gson parsed JSON instead + // of standard JSON which forces us to use Gson to get back + // the original JSON (we return a JSON string in our + // ForageApiResponse.Success) + val gsonParsedJson = Gson().toJson(vaultResponse) + + // for some really really strange reason, Gson wraps the + // actual JSON we care about into top-level "value" JSON + // field like so: { "value": } + // So, annoyingly, we need to unwrap it. Since deferred + // payments have an empty response, we need to consider + // the case where there is no top-level "value" field + val unwrapped = JSONObject(gsonParsedJson).optJSONObject("value") ?: "" + ForageApiResponse.Success(unwrapped.toString()) } } }