Skip to content

Commit 5edb6a3

Browse files
authored
Accept null file_size in AttachmentDto (#6462)
* Accept null file size in AttachmentDto * Drop file_size default in AttachmentDto and test null mapping
1 parent 0b68a9e commit 5edb6a3

6 files changed

Lines changed: 47 additions & 4 deletions

File tree

stream-chat-android-client/src/main/java/io/getstream/chat/android/client/api2/mapping/DomainMapping.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ internal class DomainMapping(
585585
authorName = author_name,
586586
authorLink = author_link,
587587
fallback = fallback,
588-
fileSize = file_size,
588+
fileSize = file_size ?: 0,
589589
image = image,
590590
imageUrl = image_url,
591591
mimeType = mime_type,

stream-chat-android-client/src/main/java/io/getstream/chat/android/client/api2/model/dto/AttachmentDto.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ internal data class AttachmentDto(
3333
val author_name: String?,
3434
val author_link: String?,
3535
val fallback: String?,
36-
val file_size: Int = 0,
36+
val file_size: Int?,
3737
val image: String?,
3838
val image_url: String?,
3939
val mime_type: String?,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ internal object Mother {
750750
authorName: String? = randomString(),
751751
authorLink: String? = randomString(),
752752
fallback: String? = randomString(),
753-
fileSize: Int = positiveRandomInt(),
753+
fileSize: Int? = positiveRandomInt(),
754754
image: String? = randomString(),
755755
imageUrl: String? = randomString(),
756756
mimeType: String? = randomString(),

stream-chat-android-client/src/test/java/io/getstream/chat/android/client/api2/mapping/DomainMappingTest.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ internal class DomainMappingTest {
558558
authorName = attachmentDto.author_name,
559559
authorLink = attachmentDto.author_link,
560560
fallback = attachmentDto.fallback,
561-
fileSize = attachmentDto.file_size,
561+
fileSize = attachmentDto.file_size ?: 0,
562562
image = attachmentDto.image,
563563
imageUrl = attachmentDto.image_url,
564564
mimeType = attachmentDto.mime_type,
@@ -576,6 +576,16 @@ internal class DomainMappingTest {
576576
assertEquals(expected, attachment)
577577
}
578578

579+
@Test
580+
fun `AttachmentDto with null file_size falls back to 0`() {
581+
val attachmentDto = randomAttachmentDto(fileSize = null)
582+
val sut = Fixture().get()
583+
val attachment = with(sut) {
584+
attachmentDto.toDomain()
585+
}
586+
assertEquals(0, attachment.fileSize)
587+
}
588+
579589
@Test
580590
fun `BannedUserResponse is correctly mapped to BannedUser`() {
581591
val bannedUserResponse = randomBannedUserResponse()

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,10 @@ internal class AttachmentDtoAdapterTest {
4949
val jsonString = parser.toJson(AttachmentDtoTestData.attachmentWithoutExtraData)
5050
jsonString.shouldEqualJson(AttachmentDtoTestData.jsonWithoutExtraData)
5151
}
52+
53+
@Test
54+
fun `Deserialize JSON attachment with null file_size`() {
55+
val attachment = parser.fromJson(AttachmentDtoTestData.jsonWithNullFileSize, AttachmentDto::class.java)
56+
attachment shouldBeEqualTo AttachmentDtoTestData.attachmentWithNullFileSize
57+
}
5258
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,33 @@ internal object AttachmentDtoTestData {
6565
extraData = mapOf("draft" to true),
6666
)
6767

68+
@Language("JSON")
69+
val jsonWithNullFileSize =
70+
"""{
71+
"file_size": null
72+
}
73+
""".withoutWhitespace()
74+
val attachmentWithNullFileSize = AttachmentDto(
75+
asset_url = null,
76+
author_name = null,
77+
author_link = null,
78+
fallback = null,
79+
file_size = null,
80+
image = null,
81+
image_url = null,
82+
mime_type = null,
83+
name = null,
84+
og_scrape_url = null,
85+
text = null,
86+
thumb_url = null,
87+
title = null,
88+
title_link = null,
89+
type = null,
90+
original_width = null,
91+
original_height = null,
92+
extraData = emptyMap(),
93+
)
94+
6895
@Language("JSON")
6996
val jsonWithoutExtraData =
7097
"""{

0 commit comments

Comments
 (0)