Skip to content

Commit

Permalink
Merge pull request #1 from puzzlehq/feature/fix_params
Browse files Browse the repository at this point in the history
Fix SessionRequestVOJsonAdapter when `params` json has only one field
  • Loading branch information
spartan-thangtranduc authored Mar 6, 2024
2 parents a70894c + 482ccd2 commit ba40a28
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<SessionRequestVO>() {
private val options: JsonReader.Options = JsonReader.Options.of("method", "params", "expiryTimestamp")
Expand Down Expand Up @@ -39,31 +40,7 @@ internal class SessionRequestVOJsonAdapter(moshi: Moshi) : JsonAdapter<SessionRe
upsertArray(JSONArray(), paramsAny).toString()
} else {
val paramsMap = paramsAny as Map<*, *>

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()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit ba40a28

Please sign in to comment.