-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CustomerCenter] Fix help path deserializing when unknown type (#1869)
### Description While testing one thing I noticed we were not handling unknown help path types. This basically caused the whole screen to not be deserialized, since we catch deserialization issues at that level, but we can do better than that and just not deserialize the unknown path type.
- Loading branch information
Showing
3 changed files
with
67 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
purchases/src/main/kotlin/com/revenuecat/purchases/customercenter/HelpPathsSerializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.revenuecat.purchases.customercenter | ||
|
||
import com.revenuecat.purchases.ExperimentalPreviewRevenueCatPurchasesAPI | ||
import com.revenuecat.purchases.common.debugLog | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.builtins.ListSerializer | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
import kotlinx.serialization.json.JsonDecoder | ||
import kotlinx.serialization.json.jsonArray | ||
|
||
@OptIn(ExperimentalPreviewRevenueCatPurchasesAPI::class) | ||
internal object HelpPathsSerializer : KSerializer<List<CustomerCenterConfigData.HelpPath>> { | ||
override val descriptor: SerialDescriptor = ListSerializer( | ||
CustomerCenterConfigData.HelpPath.serializer(), | ||
).descriptor | ||
|
||
override fun deserialize(decoder: Decoder): List<CustomerCenterConfigData.HelpPath> { | ||
val list = mutableListOf<CustomerCenterConfigData.HelpPath>() | ||
val jsonInput = decoder as? JsonDecoder ?: error("Can be deserialized only by JSON") | ||
val jsonArray = jsonInput.decodeJsonElement().jsonArray | ||
|
||
jsonArray.forEach { jsonElement -> | ||
try { | ||
list.add( | ||
jsonInput.json.decodeFromJsonElement(CustomerCenterConfigData.HelpPath.serializer(), jsonElement), | ||
) | ||
} catch (e: IllegalArgumentException) { | ||
debugLog("Issue deserializing CustomerCenter HelpPath. Ignoring. Error: $e") | ||
} | ||
} | ||
|
||
return list | ||
} | ||
|
||
override fun serialize(encoder: Encoder, value: List<CustomerCenterConfigData.HelpPath>) { | ||
ListSerializer(CustomerCenterConfigData.HelpPath.serializer()).serialize(encoder, value) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters