Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paywalls: ignore URL deserialization errors #1399

Merged
merged 1 commit into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops

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`() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙌

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