Skip to content

Commit

Permalink
Merge pull request #1845 from OneSignal/fix/crash_on_login_from_user_sub
Browse files Browse the repository at this point in the history
Fix crash from missing operationExecutor in OperationModelStore create
  • Loading branch information
emawby committed Sep 21, 2023
2 parents 9f7f9a0 + b65c008 commit cbfaeb9
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface IModelStore<TModel> :
*
* @param jsonObject The optional [JSONObject] to initialize the new model with.
*/
fun create(jsonObject: JSONObject? = null): TModel
fun create(jsonObject: JSONObject? = null): TModel?

/**
* List the models that are owned by this model store.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ abstract class ModelStore<TModel>(
val str = _prefs.getString(PreferenceStores.ONESIGNAL, PreferenceOneSignalKeys.MODEL_STORE_PREFIX + name, "[]")
val jsonArray = JSONArray(str)
for (index in 0 until jsonArray.length()) {
val newModel = create(jsonArray.getJSONObject(index))
val newModel = create(jsonArray.getJSONObject(index)) ?: continue
_models.add(newModel)
// listen for changes to this model
newModel.subscribe(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ open class SingletonModelStore<TModel>(
return model
}

val createdModel = store.create()
val createdModel = store.create() ?: throw Exception("Unable to initialize model from store $store")
createdModel.id = _singletonId
store.add(createdModel)
return createdModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package com.onesignal.core.internal.operations.impl
import com.onesignal.common.modeling.ModelStore
import com.onesignal.core.internal.operations.Operation
import com.onesignal.core.internal.preferences.IPreferencesService
import com.onesignal.debug.internal.logging.Logging
import com.onesignal.user.internal.operations.CreateSubscriptionOperation
import com.onesignal.user.internal.operations.DeleteAliasOperation
import com.onesignal.user.internal.operations.DeleteSubscriptionOperation
import com.onesignal.user.internal.operations.DeleteTagOperation
import com.onesignal.user.internal.operations.LoginUserOperation
import com.onesignal.user.internal.operations.LoginUserFromSubscriptionOperation
import com.onesignal.user.internal.operations.RefreshUserOperation
import com.onesignal.user.internal.operations.SetAliasOperation
import com.onesignal.user.internal.operations.SetPropertyOperation
Expand All @@ -18,6 +20,7 @@ import com.onesignal.user.internal.operations.TrackSessionStartOperation
import com.onesignal.user.internal.operations.TransferSubscriptionOperation
import com.onesignal.user.internal.operations.UpdateSubscriptionOperation
import com.onesignal.user.internal.operations.impl.executors.IdentityOperationExecutor
import com.onesignal.user.internal.operations.impl.executors.LoginUserFromSubscriptionOperationExecutor
import com.onesignal.user.internal.operations.impl.executors.LoginUserOperationExecutor
import com.onesignal.user.internal.operations.impl.executors.RefreshUserOperationExecutor
import com.onesignal.user.internal.operations.impl.executors.SubscriptionOperationExecutor
Expand All @@ -30,13 +33,15 @@ internal class OperationModelStore(prefs: IPreferencesService) : ModelStore<Oper
load()
}

override fun create(jsonObject: JSONObject?): Operation {
override fun create(jsonObject: JSONObject?): Operation? {
if (jsonObject == null) {
throw NullPointerException("jsonObject")
Logging.error("null jsonObject sent to OperationModelStore.create")
return null
}

if (!jsonObject.has(Operation::name.name)) {
throw IllegalArgumentException("jsonObject must have '${Operation::name.name}' attribute")
Logging.error("jsonObject must have '${Operation::name.name}' attribute")
return null
}

// Determine the type of operation based on the name property in the json
Expand All @@ -48,6 +53,7 @@ internal class OperationModelStore(prefs: IPreferencesService) : ModelStore<Oper
SubscriptionOperationExecutor.DELETE_SUBSCRIPTION -> DeleteSubscriptionOperation()
SubscriptionOperationExecutor.TRANSFER_SUBSCRIPTION -> TransferSubscriptionOperation()
LoginUserOperationExecutor.LOGIN_USER -> LoginUserOperation()
LoginUserFromSubscriptionOperationExecutor.LOGIN_USER_FROM_SUBSCRIPTION_USER -> LoginUserFromSubscriptionOperation()
RefreshUserOperationExecutor.REFRESH_USER -> RefreshUserOperation()
UpdateUserOperationExecutor.SET_TAG -> SetTagOperation()
UpdateUserOperationExecutor.DELETE_TAG -> DeleteTagOperation()
Expand Down

0 comments on commit cbfaeb9

Please sign in to comment.