only check moshi numbers for rust-sdk

This commit is contained in:
valere 2022-11-29 11:21:54 +01:00
parent d9342707fd
commit fb1995e9c9
5 changed files with 44 additions and 14 deletions

View File

@ -32,7 +32,6 @@ import org.matrix.android.sdk.api.session.room.model.message.MessageTextContent
import org.matrix.android.sdk.api.session.room.model.message.MessageType
import org.matrix.android.sdk.api.session.room.model.message.MessageVerificationRequestContent
import org.matrix.android.sdk.api.session.room.model.message.MessageVideoContent
import org.matrix.android.sdk.internal.network.parsing.CheckNumberType
import org.matrix.android.sdk.internal.network.parsing.CipherSuiteMoshiAdapter
import org.matrix.android.sdk.internal.network.parsing.ForceToBooleanJsonAdapter
import org.matrix.android.sdk.internal.network.parsing.RuntimeJsonAdapterFactory
@ -43,9 +42,6 @@ import org.matrix.android.sdk.internal.session.sync.parsing.DefaultLazyRoomSyncE
internal object MoshiProvider {
private val moshi: Moshi = Moshi.Builder()
// By default all numbers are transformed into floats by moshi
// this adapter tries to see first if it's a natural number before using float
.add(CheckNumberType.JSON_ADAPTER_FACTORY)
.add(UriMoshiAdapter())
.add(ForceToBooleanJsonAdapter())
.add(CipherSuiteMoshiAdapter())

View File

@ -24,6 +24,8 @@ import com.squareup.moshi.Moshi
import java.io.IOException
import java.lang.reflect.Type
import java.math.BigDecimal
import kotlin.math.ceil
import kotlin.math.floor
/**
* This is used to check if NUMBER in json is integer or double, so we can preserve typing when serializing/deserializing in a row.
@ -56,7 +58,16 @@ internal interface CheckNumberType {
}
override fun toJson(writer: JsonWriter, value: Any?) {
delegate.toJson(writer, value)
if (value is Number) {
val double = value.toDouble()
if (ceil(double) == floor(double)) {
writer.value(value.toLong())
} else {
writer.value(value.toDouble())
}
} else {
delegate.toJson(writer, value)
}
}
}
}

View File

@ -261,16 +261,17 @@ internal class OlmMachine @Inject constructor(
val devices =
DeviceLists(deviceChanges?.changed.orEmpty(), deviceChanges?.left.orEmpty())
val adapter = MoshiProvider.providesMoshi().adapter(ToDeviceSyncResponse::class.java)
val events = adapter.toJson(toDevice ?: ToDeviceSyncResponse()).also {
Timber.w("## VALR events: $it")
}
val adapter = MoshiProvider.providesMoshi()
.newBuilder()
.add(CheckNumberType.JSON_ADAPTER_FACTORY)
.build()
.adapter(ToDeviceSyncResponse::class.java)
val events = adapter.toJson(toDevice ?: ToDeviceSyncResponse())
// TODO once our sync response type parses the unused fallback key
// field pass in the list of unused fallback keys here
val receiveSyncChanges = inner.receiveSyncChanges(events, devices, counts, unusedFallbackKeys = null).also {
Timber.w("## VALR $it")
}
val receiveSyncChanges = inner.receiveSyncChanges(events, devices, counts, unusedFallbackKeys = null)
val outAdapter = moshi.adapter<List<Event>>(
Types.newParameterizedType(
List::class.java,

View File

@ -21,6 +21,7 @@ import org.junit.Test
import org.matrix.android.sdk.api.session.events.model.Event
import org.matrix.android.sdk.api.session.sync.model.ToDeviceSyncResponse
import org.matrix.android.sdk.internal.di.MoshiProvider
import org.matrix.android.sdk.internal.network.parsing.CheckNumberType
class MoshiNumbersAsInt {
@ -51,4 +52,26 @@ class MoshiNumbersAsInt {
jsonString shouldNotContain "1.0"
}
@Test
fun testParseThenSerialize() {
val raw = """
{"events":[{"type":"m.room.encrypted","content":{"algorithm":"m.olm.v1.curve25519-aes-sha2","ciphertext":{"cfA3dINwtmMW0DbJmnT6NiGAbOSa299Hxs6KxHgbDBw":{"body":"Awogc5L3QuIyvkluB1O/UAJp0","type":1}},"sender_key":"fqhBEOHXSSQ7ZKt1xlBg+hSTY1NEM8hezMXZ5lyBR1M"},"sender":"@web:localhost:8481"}]}
""".trimIndent()
val moshi = MoshiProvider.providesMoshi()
val adapter = moshi.adapter(ToDeviceSyncResponse::class.java)
val content = adapter.fromJson(raw)
val serialized = MoshiProvider.providesMoshi()
.newBuilder()
.add(CheckNumberType.JSON_ADAPTER_FACTORY)
.build()
.adapter(ToDeviceSyncResponse::class.java).toJson(content)
serialized shouldNotContain "1.0"
println(serialized)
}
}

View File

@ -41,7 +41,6 @@ import im.vector.app.features.html.EventHtmlRenderer
import im.vector.lib.core.utils.epoxy.charsequence.toEpoxyCharSequence
import org.matrix.android.sdk.api.session.crypto.verification.CancelCode
import org.matrix.android.sdk.api.session.crypto.verification.EVerificationState
import timber.log.Timber
import javax.inject.Inject
class SelfVerificationController @Inject constructor(
@ -61,7 +60,7 @@ class SelfVerificationController @Inject constructor(
var state: SelfVerificationViewState? = null
fun update(state: SelfVerificationViewState) {
Timber.w("VALR controller updated $state")
// Timber.w("VALR controller updated $state")
this.state = state
requestModelBuild()
}