diff --git a/stream-chat-android-client/src/main/java/io/getstream/chat/android/client/api2/mapping/DomainMapping.kt b/stream-chat-android-client/src/main/java/io/getstream/chat/android/client/api2/mapping/DomainMapping.kt index 133c069aaa4..344ec999ccc 100644 --- a/stream-chat-android-client/src/main/java/io/getstream/chat/android/client/api2/mapping/DomainMapping.kt +++ b/stream-chat-android-client/src/main/java/io/getstream/chat/android/client/api2/mapping/DomainMapping.kt @@ -585,7 +585,7 @@ internal class DomainMapping( authorName = author_name, authorLink = author_link, fallback = fallback, - fileSize = file_size, + fileSize = file_size ?: 0, image = image, imageUrl = image_url, mimeType = mime_type, diff --git a/stream-chat-android-client/src/main/java/io/getstream/chat/android/client/api2/model/dto/AttachmentDto.kt b/stream-chat-android-client/src/main/java/io/getstream/chat/android/client/api2/model/dto/AttachmentDto.kt index b9d3ca66c7f..a9965eda3eb 100644 --- a/stream-chat-android-client/src/main/java/io/getstream/chat/android/client/api2/model/dto/AttachmentDto.kt +++ b/stream-chat-android-client/src/main/java/io/getstream/chat/android/client/api2/model/dto/AttachmentDto.kt @@ -33,7 +33,7 @@ internal data class AttachmentDto( val author_name: String?, val author_link: String?, val fallback: String?, - val file_size: Int = 0, + val file_size: Int?, val image: String?, val image_url: String?, val mime_type: String?, diff --git a/stream-chat-android-client/src/test/java/io/getstream/chat/android/client/Mother.kt b/stream-chat-android-client/src/test/java/io/getstream/chat/android/client/Mother.kt index 8fccd64aabd..f61160b869a 100644 --- a/stream-chat-android-client/src/test/java/io/getstream/chat/android/client/Mother.kt +++ b/stream-chat-android-client/src/test/java/io/getstream/chat/android/client/Mother.kt @@ -750,7 +750,7 @@ internal object Mother { authorName: String? = randomString(), authorLink: String? = randomString(), fallback: String? = randomString(), - fileSize: Int = positiveRandomInt(), + fileSize: Int? = positiveRandomInt(), image: String? = randomString(), imageUrl: String? = randomString(), mimeType: String? = randomString(), diff --git a/stream-chat-android-client/src/test/java/io/getstream/chat/android/client/api2/mapping/DomainMappingTest.kt b/stream-chat-android-client/src/test/java/io/getstream/chat/android/client/api2/mapping/DomainMappingTest.kt index d4d4ba07ff5..e4740a9db82 100644 --- a/stream-chat-android-client/src/test/java/io/getstream/chat/android/client/api2/mapping/DomainMappingTest.kt +++ b/stream-chat-android-client/src/test/java/io/getstream/chat/android/client/api2/mapping/DomainMappingTest.kt @@ -558,7 +558,7 @@ internal class DomainMappingTest { authorName = attachmentDto.author_name, authorLink = attachmentDto.author_link, fallback = attachmentDto.fallback, - fileSize = attachmentDto.file_size, + fileSize = attachmentDto.file_size ?: 0, image = attachmentDto.image, imageUrl = attachmentDto.image_url, mimeType = attachmentDto.mime_type, @@ -576,6 +576,16 @@ internal class DomainMappingTest { assertEquals(expected, attachment) } + @Test + fun `AttachmentDto with null file_size falls back to 0`() { + val attachmentDto = randomAttachmentDto(fileSize = null) + val sut = Fixture().get() + val attachment = with(sut) { + attachmentDto.toDomain() + } + assertEquals(0, attachment.fileSize) + } + @Test fun `BannedUserResponse is correctly mapped to BannedUser`() { val bannedUserResponse = randomBannedUserResponse() diff --git a/stream-chat-android-client/src/test/java/io/getstream/chat/android/client/parser2/AttachmentDtoAdapterTest.kt b/stream-chat-android-client/src/test/java/io/getstream/chat/android/client/parser2/AttachmentDtoAdapterTest.kt index 61bc340ed4e..cb6367f1352 100644 --- a/stream-chat-android-client/src/test/java/io/getstream/chat/android/client/parser2/AttachmentDtoAdapterTest.kt +++ b/stream-chat-android-client/src/test/java/io/getstream/chat/android/client/parser2/AttachmentDtoAdapterTest.kt @@ -49,4 +49,10 @@ internal class AttachmentDtoAdapterTest { val jsonString = parser.toJson(AttachmentDtoTestData.attachmentWithoutExtraData) jsonString.shouldEqualJson(AttachmentDtoTestData.jsonWithoutExtraData) } + + @Test + fun `Deserialize JSON attachment with null file_size`() { + val attachment = parser.fromJson(AttachmentDtoTestData.jsonWithNullFileSize, AttachmentDto::class.java) + attachment shouldBeEqualTo AttachmentDtoTestData.attachmentWithNullFileSize + } } diff --git a/stream-chat-android-client/src/test/java/io/getstream/chat/android/client/parser2/testdata/AttachmentDtoTestData.kt b/stream-chat-android-client/src/test/java/io/getstream/chat/android/client/parser2/testdata/AttachmentDtoTestData.kt index 20e1f11ffa8..0504b4bfd7b 100644 --- a/stream-chat-android-client/src/test/java/io/getstream/chat/android/client/parser2/testdata/AttachmentDtoTestData.kt +++ b/stream-chat-android-client/src/test/java/io/getstream/chat/android/client/parser2/testdata/AttachmentDtoTestData.kt @@ -65,6 +65,33 @@ internal object AttachmentDtoTestData { extraData = mapOf("draft" to true), ) + @Language("JSON") + val jsonWithNullFileSize = + """{ + "file_size": null + } + """.withoutWhitespace() + val attachmentWithNullFileSize = AttachmentDto( + asset_url = null, + author_name = null, + author_link = null, + fallback = null, + file_size = null, + image = null, + image_url = null, + mime_type = null, + name = null, + og_scrape_url = null, + text = null, + thumb_url = null, + title = null, + title_link = null, + type = null, + original_width = null, + original_height = null, + extraData = emptyMap(), + ) + @Language("JSON") val jsonWithoutExtraData = """{