Skip to content

Commit c3ef230

Browse files
committed
Accept null file_size in direct attachment parser
1 parent 29a1025 commit c3ef230

2 files changed

Lines changed: 9 additions & 12 deletions

File tree

stream-chat-android-client/src/main/java/io/getstream/chat/android/client/parser2/direct/AttachmentAdapter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ internal class AttachmentAdapter : JsonAdapter<Attachment>() {
5454
"author_name" -> authorName = JsonParsingUtils.readNullableString(reader)
5555
"author_link" -> authorLink = JsonParsingUtils.readNullableString(reader)
5656
"fallback" -> fallback = JsonParsingUtils.readNullableString(reader)
57-
"file_size" -> fileSize = reader.nextInt()
57+
"file_size" -> fileSize = JsonParsingUtils.readNullableInt(reader) ?: 0
5858
"image" -> image = JsonParsingUtils.readNullableString(reader)
5959
"image_url" -> imageUrl = JsonParsingUtils.readNullableString(reader)
6060
"mime_type" -> mimeType = JsonParsingUtils.readNullableString(reader)

stream-chat-android-client/src/test/java/io/getstream/chat/android/client/parser2/AttachmentParsingTest.kt

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package io.getstream.chat.android.client.parser2
1818

19-
import com.squareup.moshi.JsonDataException
2019
import io.getstream.chat.android.client.api2.mapping.DomainMapping
2120
import io.getstream.chat.android.client.api2.model.dto.AttachmentDto
2221
import io.getstream.chat.android.client.parser2.direct.AttachmentAdapter
@@ -26,7 +25,6 @@ import io.getstream.chat.android.models.NoOpMessageTransformer
2625
import io.getstream.chat.android.models.NoOpUserTransformer
2726
import org.junit.jupiter.api.Assertions.assertEquals
2827
import org.junit.jupiter.api.Test
29-
import org.junit.jupiter.api.assertThrows
3028

3129
internal class AttachmentParsingTest {
3230

@@ -92,20 +90,19 @@ internal class AttachmentParsingTest {
9290

9391
// endregion
9492

95-
// region file_size: null (non-nullable Int — both paths must throw)
93+
// region file_size: null — coalesces to 0 on both paths
9694

9795
@Test
98-
fun `DTO path - throws on file_size null`() {
99-
assertThrows<JsonDataException> {
100-
parser.fromJson(AttachmentTestData.jsonWithFileSizeNull, AttachmentDto::class.java)
101-
}
96+
fun `DTO path - file_size null coalesces to 0`() {
97+
val dto = parser.fromJson(AttachmentTestData.jsonWithFileSizeNull, AttachmentDto::class.java)
98+
val attachment = with(domainMapping) { dto.toDomain() }
99+
assertEquals(0, attachment.fileSize)
102100
}
103101

104102
@Test
105-
fun `Direct path - throws on file_size null`() {
106-
assertThrows<JsonDataException> {
107-
attachmentAdapter.fromJson(AttachmentTestData.jsonWithFileSizeNull)
108-
}
103+
fun `Direct path - file_size null coalesces to 0`() {
104+
val attachment = attachmentAdapter.fromJson(AttachmentTestData.jsonWithFileSizeNull)
105+
assertEquals(0, attachment?.fileSize)
109106
}
110107

111108
// endregion

0 commit comments

Comments
 (0)