Skip to content

Commit 22abbc6

Browse files
AR Abdul Azeezcursoragent
andcommitted
fix: [SDK-5615] do not retry a composite login merge that fails for the payload
A 400 would fail the same way without the anonymous id. Conflict checks now go through OneSignalError, and only an alias conflict that does not name onesignal_id is final. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 5b17ce1 commit 22abbc6

2 files changed

Lines changed: 80 additions & 24 deletions

File tree

‎OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/operations/impl/executors/LoginUserOperationExecutor.kt‎

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.onesignal.user.internal.operations.impl.executors
22

33
import android.os.Build
4+
import com.onesignal.OneSignalError
45
import com.onesignal.common.AndroidUtils
56
import com.onesignal.common.DeviceUtils
67
import com.onesignal.common.IDManager
@@ -46,8 +47,6 @@ import com.onesignal.user.internal.subscriptions.SubscriptionModel
4647
import com.onesignal.user.internal.subscriptions.SubscriptionModelStore
4748
import com.onesignal.user.internal.subscriptions.SubscriptionStatus
4849
import com.onesignal.user.internal.subscriptions.SubscriptionType
49-
import org.json.JSONException
50-
import org.json.JSONObject
5150

5251
@Suppress("TooManyFunctions")
5352
internal class LoginUserOperationExecutor(
@@ -483,32 +482,29 @@ internal class LoginUserOperationExecutor(
483482
return op.existingOnesignalId?.takeUnless { IDManager.isLocalId(it) }
484483
}
485484

486-
// Drop the anonymous id and create the identified user. A readable 409 retries only
487-
// when onesignal_id collided, or the code is user-2. An alias conflict is final.
485+
// Drop the anonymous id only when it can be why Create User failed.
486+
// A 400 is the payload, so a second request would fail the same way.
488487
private fun shouldAbandonMerge(response: ExecutionResponse): Boolean {
489488
if (response.result != ExecutionResult.FAIL_NORETRY) return false
490-
if (response.httpStatusCode != HTTP_CONFLICT) return true
491-
return onesignalIdConflict(response.httpResponse) != false
489+
val status = response.httpStatusCode?.let { NetworkUtils.getResponseStatusType(it) }
490+
return status == NetworkUtils.ResponseStatusType.MISSING ||
491+
(status == NetworkUtils.ResponseStatusType.CONFLICT && onesignalIdConflict(response.httpResponse) != false)
492492
}
493493

494-
/** False when the body is a conflict that does not name onesignal_id. Null when it cannot be read. */
495-
private fun onesignalIdConflict(body: String?): Boolean? {
496-
if (body.isNullOrBlank()) return null
497-
return try {
498-
val errors = JSONObject(body).optJSONArray("errors") ?: return null
499-
var sawConflict = false
500-
for (index in 0 until errors.length()) {
501-
val error = errors.optJSONObject(index) ?: continue
502-
sawConflict = true
503-
if (error.optString("code") == ALIAS_CLAIMED_BY_ANOTHER_USER) return true
504-
val aliases = error.optJSONObject("meta")?.optJSONObject("conflicting_aliases")
505-
if (aliases?.has(IdentityConstants.ONESIGNAL_ID) == true) return true
506-
}
507-
if (sawConflict) false else null
508-
} catch (_: JSONException) {
509-
null
510-
}
511-
}
494+
/** False when a parsed error does not name onesignal_id. Null when the body has none. */
495+
private fun onesignalIdConflict(body: String?): Boolean? =
496+
OneSignalError.fromBackendResponse(HTTP_CONFLICT, body)
497+
.details
498+
.filter { it.backendCode != null || conflictingAliases(it).isNotEmpty() }
499+
.takeIf { it.isNotEmpty() }
500+
?.any { namesMergedUser(it) }
501+
502+
private fun namesMergedUser(detail: OneSignalError.Detail): Boolean =
503+
detail.backendCode == ALIAS_CLAIMED_BY_ANOTHER_USER ||
504+
conflictingAliases(detail).containsKey(IdentityConstants.ONESIGNAL_ID)
505+
506+
private fun conflictingAliases(detail: OneSignalError.Detail): Map<*, *> =
507+
detail.meta?.get(CONFLICTING_ALIASES) as? Map<*, *> ?: emptyMap<String, Any?>()
512508

513509
private fun backendExecutionResponse(
514510
result: ExecutionResult,
@@ -524,6 +520,7 @@ internal class LoginUserOperationExecutor(
524520
const val LOGIN_USER = "login-user"
525521
private const val HTTP_CONFLICT = 409
526522
private const val ALIAS_CLAIMED_BY_ANOTHER_USER = "user-2"
523+
private const val CONFLICTING_ALIASES = "conflicting_aliases"
527524
}
528525
}
529526

‎OneSignalSDK/onesignal/core/src/test/java/com/onesignal/user/internal/operations/LoginUserOperationExecutorTests.kt‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1734,6 +1734,65 @@ class LoginUserOperationExecutorTests : FunSpec({
17341734
coVerify(exactly = 1) { mockUserBackendService.createUser(any(), any(), any(), any()) }
17351735
}
17361736

1737+
test("composite login does not retry a merge when the payload is invalid") {
1738+
val mockUserBackendService = mockk<IUserBackendService>()
1739+
coEvery { mockUserBackendService.createUser(any(), any(), any(), any()) } throws BackendException(400, "INVALID EMAIL")
1740+
1741+
val response =
1742+
profileExecutor(mockUserBackendService).execute(
1743+
listOf(
1744+
LoginUserOperation(
1745+
appId,
1746+
localOneSignalId,
1747+
"externalId",
1748+
"existing-osid",
1749+
OneSignalUserProfile(email = "not-an-email"),
1750+
),
1751+
),
1752+
)
1753+
1754+
response.result shouldBe ExecutionResult.FAIL_NORETRY
1755+
response.httpStatusCode shouldBe 400
1756+
coVerify(exactly = 1) { mockUserBackendService.createUser(any(), any(), any(), any()) }
1757+
}
1758+
1759+
test("composite login retries without onesignal_id when the merge 409 is user-2") {
1760+
val mockUserBackendService = mockk<IUserBackendService>()
1761+
val mergeIdentities = mapOf(IdentityConstants.EXTERNAL_ID to "externalId", IdentityConstants.ONESIGNAL_ID to "existing-osid")
1762+
val plainIdentities = mapOf(IdentityConstants.EXTERNAL_ID to "externalId")
1763+
coEvery { mockUserBackendService.createUser(appId, mergeIdentities, any(), any()) } throws
1764+
BackendException(409, """{"errors":[{"code":"user-2","title":"One or more Aliases claimed by another User"}]}""")
1765+
coEvery { mockUserBackendService.createUser(appId, plainIdentities, any(), any()) } returns
1766+
CreateUserResponse(mapOf(IdentityConstants.ONESIGNAL_ID to remoteOneSignalId), PropertiesObject(), listOf())
1767+
1768+
val response =
1769+
profileExecutor(mockUserBackendService).execute(
1770+
listOf(LoginUserOperation(appId, localOneSignalId, "externalId", "existing-osid", OneSignalUserProfile(tags = mapOf("plan" to "pro")))),
1771+
)
1772+
1773+
response.result shouldBe ExecutionResult.SUCCESS
1774+
coVerify(exactly = 1) { mockUserBackendService.createUser(appId, mergeIdentities, any(), any()) }
1775+
coVerify(exactly = 1) { mockUserBackendService.createUser(appId, plainIdentities, any(), any()) }
1776+
}
1777+
1778+
test("composite login retries without onesignal_id when the 409 body cannot be read") {
1779+
val mockUserBackendService = mockk<IUserBackendService>()
1780+
val mergeIdentities = mapOf(IdentityConstants.EXTERNAL_ID to "externalId", IdentityConstants.ONESIGNAL_ID to "existing-osid")
1781+
val plainIdentities = mapOf(IdentityConstants.EXTERNAL_ID to "externalId")
1782+
coEvery { mockUserBackendService.createUser(appId, mergeIdentities, any(), any()) } throws BackendException(409, "CONFLICT")
1783+
coEvery { mockUserBackendService.createUser(appId, plainIdentities, any(), any()) } returns
1784+
CreateUserResponse(mapOf(IdentityConstants.ONESIGNAL_ID to remoteOneSignalId), PropertiesObject(), listOf())
1785+
1786+
val response =
1787+
profileExecutor(mockUserBackendService).execute(
1788+
listOf(LoginUserOperation(appId, localOneSignalId, "externalId", "existing-osid", OneSignalUserProfile(tags = mapOf("plan" to "pro")))),
1789+
)
1790+
1791+
response.result shouldBe ExecutionResult.SUCCESS
1792+
coVerify(exactly = 1) { mockUserBackendService.createUser(appId, mergeIdentities, any(), any()) }
1793+
coVerify(exactly = 1) { mockUserBackendService.createUser(appId, plainIdentities, any(), any()) }
1794+
}
1795+
17371796
test("composite login does not merge when the anonymous user is still local, or under IV") {
17381797
for ((existingId, ivActive) in listOf(IDManager.createLocalId() to false, "existing-osid" to true)) {
17391798
val mockUserBackendService = mockk<IUserBackendService>()

0 commit comments

Comments
 (0)