fix loading posts with invalid published_at in preview card (#4993)
closes #4992
This commit is contained in:
parent
beadc165fd
commit
b5c1a57a39
4 changed files with 63 additions and 6 deletions
|
|
@ -82,8 +82,8 @@ object NetworkModule {
|
|||
@Provides
|
||||
@Singleton
|
||||
fun providesMoshi(): Moshi = Moshi.Builder()
|
||||
.add(Date::class.java, Rfc3339DateJsonAdapter())
|
||||
.add(GuardedAdapter.ANNOTATION_FACTORY)
|
||||
.add(Date::class.java, Rfc3339DateJsonAdapter())
|
||||
// Enum types with fallback value
|
||||
.add(
|
||||
Attachment.Type::class.java,
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@
|
|||
|
||||
package com.keylesspalace.tusky.entity
|
||||
|
||||
import com.keylesspalace.tusky.json.Guarded
|
||||
import com.squareup.moshi.Json
|
||||
import com.squareup.moshi.JsonClass
|
||||
import java.util.Date
|
||||
|
|
@ -27,7 +28,8 @@ data class PreviewCard(
|
|||
val authors: List<PreviewCardAuthor> = emptyList(),
|
||||
@Json(name = "author_name") val authorName: String? = null,
|
||||
@Json(name = "provider_name") val providerName: String? = null,
|
||||
@Json(name = "published_at") val publishedAt: Date?,
|
||||
// sometimes this date is invalid https://github.com/tuskyapp/Tusky/issues/4992
|
||||
@Json(name = "published_at") @Guarded val publishedAt: Date?,
|
||||
val image: String? = null,
|
||||
val type: String,
|
||||
val width: Int = 0,
|
||||
|
|
|
|||
|
|
@ -17,8 +17,8 @@
|
|||
|
||||
package com.keylesspalace.tusky.json
|
||||
|
||||
import android.util.Log
|
||||
import com.squareup.moshi.JsonAdapter
|
||||
import com.squareup.moshi.JsonDataException
|
||||
import com.squareup.moshi.JsonReader
|
||||
import com.squareup.moshi.JsonWriter
|
||||
import com.squareup.moshi.Moshi
|
||||
|
|
@ -35,10 +35,12 @@ class GuardedAdapter<T> private constructor(
|
|||
|
||||
override fun fromJson(reader: JsonReader): T? {
|
||||
return try {
|
||||
delegate.fromJson(reader)
|
||||
} catch (e: JsonDataException) {
|
||||
reader.skipValue()
|
||||
reader.peekJson().use { delegate.fromJson(it) }
|
||||
} catch (e: Exception) {
|
||||
Log.w("GuardedAdapter", "failed to read json", e)
|
||||
null
|
||||
} finally {
|
||||
reader.skipValue()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,8 +1,11 @@
|
|||
package com.keylesspalace.tusky.json
|
||||
|
||||
import com.keylesspalace.tusky.entity.PreviewCard
|
||||
import com.keylesspalace.tusky.entity.Relationship
|
||||
import com.squareup.moshi.Moshi
|
||||
import com.squareup.moshi.adapter
|
||||
import com.squareup.moshi.adapters.Rfc3339DateJsonAdapter
|
||||
import java.util.Date
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
|
||||
|
|
@ -11,6 +14,7 @@ class GuardedAdapterTest {
|
|||
|
||||
private val moshi = Moshi.Builder()
|
||||
.add(GuardedAdapter.ANNOTATION_FACTORY)
|
||||
.add(Date::class.java, Rfc3339DateJsonAdapter())
|
||||
.build()
|
||||
|
||||
@Test
|
||||
|
|
@ -131,4 +135,53 @@ class GuardedAdapterTest {
|
|||
moshi.adapter<Relationship>().fromJson(jsonInput)
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `should deserialize PreviewCard when attribute 'published_at' is invalid`() {
|
||||
// https://github.com/tuskyapp/Tusky/issues/4992
|
||||
val jsonInput = """{
|
||||
|
||||
"url": "https://www.cbc.ca/amp/1.7484477",
|
||||
"title": "Canada reconsidering F-35 purchase amid tensions with Washington, says minister",
|
||||
"description": "Canada is looking at cancelling a major portion of its purchase of U.S.-built F-35 stealth fighters and plans on opening talks with rival aircraft makers, Defence Minister Bill Blair said.",
|
||||
"language": "en",
|
||||
"type": "link",
|
||||
"author_name": "Murray Brewster",
|
||||
"author_url": "",
|
||||
"provider_name": "CBC",
|
||||
"provider_url": "",
|
||||
"html": "",
|
||||
"width": 0,
|
||||
"height": 0,
|
||||
"image": "https://files.mastodon.social/cache/preview_cards/images/137/231/445/original/0f63297db3ac7362.jpg",
|
||||
"image_description": "",
|
||||
"embed_url": "",
|
||||
"blurhash": "U74#eeXoK9nLrVWZS+nfXVaenKkXTOjErobx",
|
||||
"published_at": "57171-08-04T06:31:30.000Z",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Murray Brewster",
|
||||
"url": "",
|
||||
"account": null
|
||||
}
|
||||
]
|
||||
|
||||
}"""
|
||||
assertEquals(
|
||||
PreviewCard(
|
||||
url = "https://www.cbc.ca/amp/1.7484477",
|
||||
title = "Canada reconsidering F-35 purchase amid tensions with Washington, says minister",
|
||||
description = "Canada is looking at cancelling a major portion of its purchase of U.S.-built F-35 stealth fighters and plans on opening talks with rival aircraft makers, Defence Minister Bill Blair said.",
|
||||
type = "link",
|
||||
authorName = "Murray Brewster",
|
||||
providerName = "CBC",
|
||||
image = "https://files.mastodon.social/cache/preview_cards/images/137/231/445/original/0f63297db3ac7362.jpg",
|
||||
width = 0,
|
||||
height = 0,
|
||||
blurhash = "U74#eeXoK9nLrVWZS+nfXVaenKkXTOjErobx",
|
||||
publishedAt = null,
|
||||
),
|
||||
moshi.adapter<PreviewCard>().fromJson(jsonInput)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue