Simplify error response models (#4775)

This commit is contained in:
David Perez
2025-02-24 14:52:00 -06:00
committed by GitHub
parent a651d9b1fc
commit eadfac5ea8
3 changed files with 14 additions and 31 deletions

View File

@@ -1,7 +1,9 @@
package com.x8bit.bitwarden.data.auth.datasource.network.model package com.x8bit.bitwarden.data.auth.datasource.network.model
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.SerialName import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonNames
import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.JsonObject
/** /**
@@ -92,20 +94,21 @@ sealed class GetTokenResponseJson {
/** /**
* Models json body of an invalid request. * Models json body of an invalid request.
*
* This model supports older versions of the error response model that used lower-case keys.
*/ */
@OptIn(ExperimentalSerializationApi::class)
@Serializable @Serializable
data class Invalid( data class Invalid(
@JsonNames("errorModel")
@SerialName("ErrorModel") @SerialName("ErrorModel")
val errorModel: ErrorModel?, private val errorModel: ErrorModel?,
@SerialName("errorModel")
val legacyErrorModel: LegacyErrorModel?,
) : GetTokenResponseJson() { ) : GetTokenResponseJson() {
/** /**
* The error message returned from the server, or null. * The error message returned from the server, or null.
*/ */
val errorMessage: String? val errorMessage: String? get() = errorModel?.errorMessage
get() = errorModel?.errorMessage ?: legacyErrorModel?.errorMessage
/** /**
* The type of invalid responses that can be received. * The type of invalid responses that can be received.
@@ -131,24 +134,16 @@ sealed class GetTokenResponseJson {
/** /**
* The error body of an invalid request containing a message. * The error body of an invalid request containing a message.
*
* This model supports older versions of the error response model that used lower-case
* keys.
*/ */
@Serializable @Serializable
data class ErrorModel( data class ErrorModel(
@JsonNames("message")
@SerialName("Message") @SerialName("Message")
val errorMessage: String, val errorMessage: String,
) )
/**
* The legacy error body of an invalid request containing a message.
*
* This model is used to support older versions of the error response model that used
* lower-case keys.
*/
@Serializable
data class LegacyErrorModel(
@SerialName("message")
val errorMessage: String,
)
} }
/** /**

View File

@@ -287,7 +287,7 @@ class IdentityServiceTest : BaseServiceTest() {
captchaToken = null, captchaToken = null,
uniqueAppId = UNIQUE_APP_ID, uniqueAppId = UNIQUE_APP_ID,
) )
assertEquals(LEGACY_INVALID_LOGIN.asSuccess(), result) assertEquals(INVALID_LOGIN.asSuccess(), result)
} }
@Suppress("MaxLineLength") @Suppress("MaxLineLength")
@@ -651,7 +651,7 @@ private const val INVALID_LOGIN_JSON = """
private const val LEGACY_INVALID_LOGIN_JSON = """ private const val LEGACY_INVALID_LOGIN_JSON = """
{ {
"errorModel": { "errorModel": {
"message": "Legacy-123" "message": "123"
} }
} }
""" """
@@ -688,14 +688,6 @@ private val INVALID_LOGIN = GetTokenResponseJson.Invalid(
errorModel = GetTokenResponseJson.Invalid.ErrorModel( errorModel = GetTokenResponseJson.Invalid.ErrorModel(
errorMessage = "123", errorMessage = "123",
), ),
legacyErrorModel = null,
)
private val LEGACY_INVALID_LOGIN = GetTokenResponseJson.Invalid(
errorModel = null,
legacyErrorModel = GetTokenResponseJson.Invalid.LegacyErrorModel(
errorMessage = "Legacy-123",
),
) )
private val SEND_VERIFICATION_EMAIL_REQUEST = SendVerificationEmailRequestJson( private val SEND_VERIFICATION_EMAIL_REQUEST = SendVerificationEmailRequestJson(

View File

@@ -1574,7 +1574,6 @@ class AuthRepositoryTest {
errorModel = GetTokenResponseJson.Invalid.ErrorModel( errorModel = GetTokenResponseJson.Invalid.ErrorModel(
errorMessage = "mock_error_message", errorMessage = "mock_error_message",
), ),
legacyErrorModel = null,
) )
.asSuccess() .asSuccess()
@@ -1617,7 +1616,6 @@ class AuthRepositoryTest {
errorModel = GetTokenResponseJson.Invalid.ErrorModel( errorModel = GetTokenResponseJson.Invalid.ErrorModel(
errorMessage = "new device verification required", errorMessage = "new device verification required",
), ),
legacyErrorModel = null,
) )
.asSuccess() .asSuccess()
@@ -2401,7 +2399,6 @@ class AuthRepositoryTest {
errorModel = GetTokenResponseJson.Invalid.ErrorModel( errorModel = GetTokenResponseJson.Invalid.ErrorModel(
errorMessage = "mock_error_message", errorMessage = "mock_error_message",
), ),
legacyErrorModel = null,
) )
.asSuccess() .asSuccess()
@@ -2870,7 +2867,6 @@ class AuthRepositoryTest {
errorModel = GetTokenResponseJson.Invalid.ErrorModel( errorModel = GetTokenResponseJson.Invalid.ErrorModel(
errorMessage = "mock_error_message", errorMessage = "mock_error_message",
), ),
legacyErrorModel = null,
) )
.asSuccess() .asSuccess()