[PM-19613] Save attachment error message update (#5013)

This commit is contained in:
André Bispo
2025-04-14 21:50:59 +01:00
committed by GitHub
parent 8f311861e3
commit d989c61bc2
13 changed files with 264 additions and 48 deletions

View File

@@ -45,7 +45,7 @@ interface CiphersApi {
suspend fun createAttachment(
@Path("cipherId") cipherId: String,
@Body body: AttachmentJsonRequest,
): NetworkResult<AttachmentJsonResponse>
): NetworkResult<AttachmentJsonResponse.Success>
/**
* Uploads the attachment associated with a cipher.

View File

@@ -6,17 +6,44 @@ import kotlinx.serialization.Serializable
/**
* Represents the JSON response from creating a new attachment.
*/
@Serializable
data class AttachmentJsonResponse(
@SerialName("attachmentId")
val attachmentId: String,
sealed class AttachmentJsonResponse {
/**
* Represents a successful response from create attachment request.
*
* @property attachmentId The ID of the attachment.
* @property url The URL of the attachment.
* @property fileUploadType The type of file upload.
* @property cipherResponse The cipher response associated with the attachment.
*/
@Serializable
data class Success(
@SerialName("attachmentId")
val attachmentId: String,
@SerialName("url")
val url: String,
@SerialName("url")
val url: String,
@SerialName("fileUploadType")
val fileUploadType: FileUploadType,
@SerialName("fileUploadType")
val fileUploadType: FileUploadType,
@SerialName("cipherResponse")
val cipherResponse: SyncResponseJson.Cipher,
)
@SerialName("cipherResponse")
val cipherResponse: SyncResponseJson.Cipher,
) : AttachmentJsonResponse()
/**
* Represents the json body of an invalid create request.
*
* @property message A general, user-displayable error message.
* @property validationErrors a map where each value is a list of error messages for each
* key. The values in the array should be used for display to the user, since the keys tend
* to come back as nonsense. (eg: empty string key)
*/
@Serializable
data class Invalid(
@SerialName("message")
override val message: String,
@SerialName("validationErrors")
override val validationErrors: Map<String, List<String>>?,
) : AttachmentJsonResponse(), InvalidJsonResponse
}

View File

@@ -0,0 +1,28 @@
package com.bitwarden.network.model
/**
* Represents the json body of an invalid send json request.
*/
sealed interface InvalidJsonResponse {
/**
* A general, user-displayable error message.
*/
val message: String
/**
* a map where each value is a list of error messages for each key.
* The values in the array should be used for display to the user, since the keys tend to come
* back as nonsense. (eg: empty string key)
*/
val validationErrors: Map<String, List<String>>?
/**
* Returns the first error message found in [validationErrors], or [message] if there are no
* [validationErrors] present.
*/
val firstValidationErrorMessage: String?
get() = validationErrors
?.flatMap { it.value }
?.first()
}

View File

@@ -7,7 +7,21 @@ fun createMockAttachmentJsonResponse(
number: Int,
fileUploadType: FileUploadType = FileUploadType.AZURE,
): AttachmentJsonResponse =
AttachmentJsonResponse(
AttachmentJsonResponse.Success(
attachmentId = "mockAttachmentId-$number",
url = "mockUrl-$number",
fileUploadType = fileUploadType,
cipherResponse = createMockCipher(number = number),
)
/**
* Create a mock [AttachmentJsonResponse.Success] with a given [number].
*/
fun createMockAttachmentResponse(
number: Int,
fileUploadType: FileUploadType = FileUploadType.AZURE,
): AttachmentJsonResponse.Success =
AttachmentJsonResponse.Success(
attachmentId = "mockAttachmentId-$number",
url = "mockUrl-$number",
fileUploadType = fileUploadType,