Skip to content

Commit

Permalink
Paywalls: ignore URL deserialization errors (#1399)
Browse files Browse the repository at this point in the history
Note that this is only for the optional URLs, `assetBaseURL` is still
required, but I'll handle errors there in a separate PR.
  • Loading branch information
NachoSoto authored and tonidero committed Oct 31, 2023
1 parent 4f33593 commit 9e37d73
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,16 @@ data class PaywallData(
/**
* If set, the paywall will display a terms of service link.
*/
@SerialName("tos_url") @Serializable(with = URLSerializer::class) val termsOfServiceURL: URL? = null,
@SerialName("tos_url")
@Serializable(with = OptionalURLSerializer::class)
val termsOfServiceURL: URL? = null,

/**
* If set, the paywall will display a privacy policy link.
*/
@SerialName("privacy_url") @Serializable(with = URLSerializer::class) val privacyURL: URL? = null,
@SerialName("privacy_url")
@Serializable(with = OptionalURLSerializer::class)
val privacyURL: URL? = null,

/**
* The set of colors used.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.revenuecat.purchases.paywalls

import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.nullable
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import java.net.MalformedURLException
import java.net.URL

object URLSerializer : KSerializer<URL> {
internal object URLSerializer : KSerializer<URL> {
override val descriptor = PrimitiveSerialDescriptor("URL", PrimitiveKind.STRING)

override fun deserialize(decoder: Decoder): URL {
Expand All @@ -18,3 +20,26 @@ object URLSerializer : KSerializer<URL> {
encoder.encodeString(value.toString())
}
}

/**
* Equivalent to URLSerializer but allows empty or invalid URLs.
*/
internal object OptionalURLSerializer : KSerializer<URL?> {
private val delegate = URLSerializer.nullable
override val descriptor = PrimitiveSerialDescriptor("URL?", PrimitiveKind.STRING)

override fun deserialize(decoder: Decoder): URL? {
return try {
delegate.deserialize(decoder)
} catch (e: MalformedURLException) {
null
}
}

override fun serialize(encoder: Encoder, value: URL?) {
when (value) {
null -> encoder.encodeString("")
else -> delegate.serialize(encoder, value)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ class PaywallDataTest {
assertThat(images.icon).isNull()
}

@Test
fun `does not fail to decode invalid URLs`() {
val paywall: PaywallData = decode(PAYWALLDATA_EMPTY_IMAGES)

assertThat(paywall.config.privacyURL).isNull()
assertThat(paywall.config.termsOfServiceURL).isNull()
}

@Test
fun `paywall color can be created from a ColorInt`() {
val colorInt = Color.parseColor("#FFAABB")
Expand Down
2 changes: 2 additions & 0 deletions purchases/src/test/resources/paywalldata-empty-images.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"background": " ",
"icon": null
},
"privacy_url": "invalid url",
"tos_url": "unrecognized:?/2 URL",
"colors": {
"light": {
"background": "#FFFFFF",
Expand Down

0 comments on commit 9e37d73

Please sign in to comment.