Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ internal class AttachmentAdapter : JsonAdapter<Attachment>() {
"author_name" -> authorName = JsonParsingUtils.readNullableString(reader)
"author_link" -> authorLink = JsonParsingUtils.readNullableString(reader)
"fallback" -> fallback = JsonParsingUtils.readNullableString(reader)
"file_size" -> fileSize = reader.nextInt()
"file_size" -> fileSize = JsonParsingUtils.readNullableInt(reader) ?: 0
"image" -> image = JsonParsingUtils.readNullableString(reader)
"image_url" -> imageUrl = JsonParsingUtils.readNullableString(reader)
"mime_type" -> mimeType = JsonParsingUtils.readNullableString(reader)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,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,
Expand All @@ -568,6 +568,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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

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

import com.squareup.moshi.JsonDataException
import io.getstream.chat.android.client.api2.mapping.DomainMapping
import io.getstream.chat.android.client.api2.model.dto.AttachmentDto
import io.getstream.chat.android.client.parser2.direct.AttachmentAdapter
Expand All @@ -26,7 +25,6 @@ import io.getstream.chat.android.models.NoOpMessageTransformer
import io.getstream.chat.android.models.NoOpUserTransformer
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows

internal class AttachmentParsingTest {

Expand Down Expand Up @@ -92,20 +90,19 @@ internal class AttachmentParsingTest {

// endregion

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

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

@Test
fun `Direct path - throws on file_size null`() {
assertThrows<JsonDataException> {
attachmentAdapter.fromJson(AttachmentTestData.jsonWithFileSizeNull)
}
fun `Direct path - file_size null coalesces to 0`() {
val attachment = attachmentAdapter.fromJson(AttachmentTestData.jsonWithFileSizeNull)
assertEquals(0, attachment?.fileSize)
}

// endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
"""{
Expand Down
Loading