Skip to content

Commit 82ccae3

Browse files
authored
Accept null file_size in AttachmentDto (#6463)
* Accept null file size in AttachmentDto * Accept null file_size in direct attachment parser * Drop file_size default in AttachmentDto and test null mapping
1 parent 1e060ec commit 82ccae3

8 files changed

Lines changed: 56 additions & 16 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
@@ -579,7 +579,7 @@ internal class DomainMapping(
579579
authorName = author_name,
580580
authorLink = author_link,
581581
fallback = fallback,
582-
fileSize = file_size,
582+
fileSize = file_size ?: 0,
583583
image = image,
584584
imageUrl = image_url,
585585
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/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/Mother.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ internal object Mother {
746746
authorName: String? = randomString(),
747747
authorLink: String? = randomString(),
748748
fallback: String? = randomString(),
749-
fileSize: Int = positiveRandomInt(),
749+
fileSize: Int? = positiveRandomInt(),
750750
image: String? = randomString(),
751751
imageUrl: String? = randomString(),
752752
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
@@ -550,7 +550,7 @@ internal class DomainMappingTest {
550550
authorName = attachmentDto.author_name,
551551
authorLink = attachmentDto.author_link,
552552
fallback = attachmentDto.fallback,
553-
fileSize = attachmentDto.file_size,
553+
fileSize = attachmentDto.file_size ?: 0,
554554
image = attachmentDto.image,
555555
imageUrl = attachmentDto.image_url,
556556
mimeType = attachmentDto.mime_type,
@@ -568,6 +568,16 @@ internal class DomainMappingTest {
568568
assertEquals(expected, attachment)
569569
}
570570

571+
@Test
572+
fun `AttachmentDto with null file_size falls back to 0`() {
573+
val attachmentDto = randomAttachmentDto(fileSize = null)
574+
val sut = Fixture().get()
575+
val attachment = with(sut) {
576+
attachmentDto.toDomain()
577+
}
578+
assertEquals(0, attachment.fileSize)
579+
}
580+
571581
@Test
572582
fun `BannedUserResponse is correctly mapped to BannedUser`() {
573583
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/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

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)