From 482ccd231190464940baf9b88975e75ce89fc62a Mon Sep 17 00:00:00 2001 From: thangdtran Date: Wed, 6 Mar 2024 14:54:44 +0700 Subject: [PATCH] Fix SessionRequestVOJsonAdapter when `params` json has only one field --- .../adapters/SessionRequestVOJsonAdapter.kt | 29 +--------- .../SessionRequestVOJsonAdapterTest.kt | 55 ++++++++++++++++++- 2 files changed, 57 insertions(+), 27 deletions(-) diff --git a/protocol/sign/src/main/kotlin/com/walletconnect/sign/common/adapters/SessionRequestVOJsonAdapter.kt b/protocol/sign/src/main/kotlin/com/walletconnect/sign/common/adapters/SessionRequestVOJsonAdapter.kt index fcf54bf21..2895a0178 100644 --- a/protocol/sign/src/main/kotlin/com/walletconnect/sign/common/adapters/SessionRequestVOJsonAdapter.kt +++ b/protocol/sign/src/main/kotlin/com/walletconnect/sign/common/adapters/SessionRequestVOJsonAdapter.kt @@ -8,9 +8,10 @@ import com.squareup.moshi.JsonWriter import com.squareup.moshi.Moshi import com.squareup.moshi.internal.Util import com.walletconnect.sign.common.model.vo.clientsync.session.payload.SessionRequestVO +import kotlin.String import org.json.JSONArray import org.json.JSONObject -import kotlin.String + internal class SessionRequestVOJsonAdapter(moshi: Moshi) : JsonAdapter() { private val options: JsonReader.Options = JsonReader.Options.of("method", "params", "expiryTimestamp") @@ -39,31 +40,7 @@ internal class SessionRequestVOJsonAdapter(moshi: Moshi) : JsonAdapter - - if (paramsMap.size == 1) { - val paramsMapEntry: Map.Entry<*, *> = paramsAny.firstNotNullOf { it } - val key = paramsMapEntry.key as String - - when (paramsMapEntry.value) { - is List<*> -> { - val jsonArray = upsertArray(JSONArray(), paramsMapEntry.value as List<*>).toString() - - "\"$key\":$jsonArray" - } - - is Map<*, *> -> { - val jsonObject = upsertObject(JSONObject(), paramsMapEntry.value as Map<*, *>) - - "\"$key\":$jsonObject" - } - - else -> { - upsertObject(JSONObject(), paramsMap).toString() - } - } - } else { - upsertObject(JSONObject(), paramsMap).toString() - } + upsertObject(JSONObject(), paramsMap).toString() } } diff --git a/protocol/sign/src/test/kotlin/com/walletconnect/sign/core/adapters/SessionRequestVOJsonAdapterTest.kt b/protocol/sign/src/test/kotlin/com/walletconnect/sign/core/adapters/SessionRequestVOJsonAdapterTest.kt index 12d9c5a1f..95a2b10f6 100644 --- a/protocol/sign/src/test/kotlin/com/walletconnect/sign/core/adapters/SessionRequestVOJsonAdapterTest.kt +++ b/protocol/sign/src/test/kotlin/com/walletconnect/sign/core/adapters/SessionRequestVOJsonAdapterTest.kt @@ -133,7 +133,7 @@ internal class SessionRequestVOJsonAdapterTest { """.trimIndent() val expectedParamsJsonObj = JSONObject(params) - val actualParamsJsonObj = JSONObject("{$serializedParams}") + val actualParamsJsonObj = JSONObject(serializedParams) assertEquals(expectedParamsJsonObj.getJSONArray("transactions").length(), actualParamsJsonObj.getJSONArray("transactions").length()) @@ -639,6 +639,59 @@ internal class SessionRequestVOJsonAdapterTest { iterateJsonObjects(expectedParamsJsonObj, actualParamsJsonObj) } + @Test + fun jsonParamsObjectWithOneArrayField() { + @Language("json") + params = """ + { + "filter": ["date", "name"] + } + """.trimIndent() + + val expectedParamsJsonObj = JSONObject(params) + val actualParamsJsonObj = JSONObject(serializedParams) + + assertEquals(expectedParamsJsonObj.length(), actualParamsJsonObj.length()) + + iterateJsonObjects(expectedParamsJsonObj, actualParamsJsonObj) + } + + @Test + fun jsonParamsObjectWithOneObjectField() { + @Language("json") + params = """ + { + "filter": { + "timestamp" : 1111199999, + "page" : 0, + "owner": "owner1" + } + } + """.trimIndent() + val expectedParamsJsonObj = JSONObject(params) + val actualParamsJsonObj = JSONObject(serializedParams) + + assertEquals(expectedParamsJsonObj.length(), actualParamsJsonObj.length()) + + iterateJsonObjects(expectedParamsJsonObj, actualParamsJsonObj) + } + + @Test + fun jsonParamsObjectWithOneStringField() { + @Language("json") + params = """ + { + "filter": "id,date" + } + """.trimIndent() + val expectedParamsJsonObj = JSONObject(params) + val actualParamsJsonObj = JSONObject(serializedParams) + + assertEquals(expectedParamsJsonObj.length(), actualParamsJsonObj.length()) + + iterateJsonObjects(expectedParamsJsonObj, actualParamsJsonObj) + } + private fun iterateJsonObjects(expJsonObject: JSONObject, actJsonObject: JSONObject) { expJsonObject.keys().forEach { key -> assert(actJsonObject.has(key))