Skip to content

Commit 2ecd899

Browse files
authored
fix(OptimizelyJSON): Send map instead of OptimizelyJSON object in decision notification. (#324)
1 parent 43e8632 commit 2ecd899

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

Sources/Optimizely/OptimizelyClient.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,23 +549,30 @@ open class OptimizelyClient: NSObject {
549549

550550
var type: Constants.VariableValueType?
551551
var valueParsed: T?
552+
var notificationValue: Any? = featureValue
552553

553554
switch T.self {
554555
case is String.Type:
555556
type = .string
556557
valueParsed = featureValue as? T
558+
notificationValue = valueParsed
557559
case is Int.Type:
558560
type = .integer
559561
valueParsed = Int(featureValue) as? T
562+
notificationValue = valueParsed
560563
case is Double.Type:
561564
type = .double
562565
valueParsed = Double(featureValue) as? T
566+
notificationValue = valueParsed
563567
case is Bool.Type:
564568
type = .boolean
565569
valueParsed = Bool(featureValue) as? T
570+
notificationValue = valueParsed
566571
case is OptimizelyJSON.Type:
567572
type = .json
568-
valueParsed = OptimizelyJSON(payload: featureValue) as? T
573+
let jsonValue = OptimizelyJSON(payload: featureValue)
574+
valueParsed = jsonValue as? T
575+
notificationValue = jsonValue?.toMap()
569576
default:
570577
break
571578
}
@@ -590,7 +597,7 @@ open class OptimizelyClient: NSObject {
590597
featureEnabled: featureEnabled,
591598
variableKey: variableKey,
592599
variableType: variable.type,
593-
variableValue: value)
600+
variableValue: notificationValue)
594601

595602
return value
596603
}

Tests/OptimizelyTests-Common/DecisionListenerTests.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ class DecisionListenerTests: XCTestCase {
3333
let kVariableValueInt = 42
3434
let kVariableValueDouble = 4.2
3535
let kVariableValueBool = true
36-
let kVariableValueJSON = "{\"value\":1}"
3736

3837
// MARK: - Properties
3938

@@ -159,7 +158,7 @@ class DecisionListenerTests: XCTestCase {
159158
_ = notificationCenter.addDecisionNotificationListener { (_, _, _, decisionInfo) in
160159
XCTAssertEqual(decisionInfo[Constants.DecisionInfoKeys.featureEnabled] as! Bool, false)
161160
XCTAssertEqual(decisionInfo[Constants.DecisionInfoKeys.source] as! String, Constants.DecisionSource.rollout.rawValue)
162-
XCTAssertEqual((decisionInfo[Constants.DecisionInfoKeys.variableValue] as! OptimizelyJSON).toString(), self.kVariableValueJSON)
161+
XCTAssertEqual((decisionInfo[Constants.DecisionInfoKeys.variableValue] as! [String: Any])["value"] as! Int, 1)
163162
XCTAssertEqual(decisionInfo[Constants.DecisionInfoKeys.variableType] as! String, Constants.VariableValueType.json.rawValue)
164163
XCTAssertNil(decisionInfo[Constants.ExperimentDecisionInfoKeys.experiment])
165164
XCTAssertNil(decisionInfo[Constants.ExperimentDecisionInfoKeys.variation])
@@ -359,7 +358,7 @@ class DecisionListenerTests: XCTestCase {
359358
_ = notificationCenter.addDecisionNotificationListener { (_, _, _, decisionInfo) in
360359
XCTAssertEqual(decisionInfo[Constants.DecisionInfoKeys.featureEnabled] as! Bool, true)
361360
XCTAssertEqual(decisionInfo[Constants.DecisionInfoKeys.source] as! String, Constants.DecisionSource.rollout.rawValue)
362-
XCTAssertEqual((decisionInfo[Constants.DecisionInfoKeys.variableValue] as! OptimizelyJSON).toString(), "{\"value\":2}")
361+
XCTAssertEqual((decisionInfo[Constants.DecisionInfoKeys.variableValue] as! [String: Any])["value"] as! Int, 2)
363362
XCTAssertEqual(decisionInfo[Constants.DecisionInfoKeys.variableType] as! String, Constants.VariableValueType.json.rawValue)
364363
XCTAssertNil(decisionInfo[Constants.ExperimentDecisionInfoKeys.experiment])
365364
XCTAssertNil(decisionInfo[Constants.ExperimentDecisionInfoKeys.variation])
@@ -376,7 +375,7 @@ class DecisionListenerTests: XCTestCase {
376375
_ = notificationCenter.addDecisionNotificationListener { (_, _, _, decisionInfo) in
377376
XCTAssertEqual(decisionInfo[Constants.DecisionInfoKeys.featureEnabled] as! Bool, false)
378377
XCTAssertEqual(decisionInfo[Constants.DecisionInfoKeys.source] as! String, Constants.DecisionSource.rollout.rawValue)
379-
XCTAssertEqual((decisionInfo[Constants.DecisionInfoKeys.variableValue] as! OptimizelyJSON).toString(), self.kVariableValueJSON)
378+
XCTAssertEqual((decisionInfo[Constants.DecisionInfoKeys.variableValue] as! [String: Any])["value"] as! Int, 1)
380379
XCTAssertEqual(decisionInfo[Constants.DecisionInfoKeys.variableType] as! String, Constants.VariableValueType.json.rawValue)
381380
XCTAssertNil(decisionInfo[Constants.ExperimentDecisionInfoKeys.experiment])
382381
XCTAssertNil(decisionInfo[Constants.ExperimentDecisionInfoKeys.variation])
@@ -652,7 +651,7 @@ class DecisionListenerTests: XCTestCase {
652651
_ = notificationCenter.addDecisionNotificationListener { (_, _, _, decisionInfo) in
653652
XCTAssertEqual(decisionInfo[Constants.DecisionInfoKeys.featureEnabled] as! Bool, true)
654653
XCTAssertEqual(decisionInfo[Constants.DecisionInfoKeys.source] as! String, Constants.DecisionSource.featureTest.rawValue)
655-
XCTAssertEqual((decisionInfo[Constants.DecisionInfoKeys.variableValue] as! OptimizelyJSON).toString(), "{\"value\":2}")
654+
XCTAssertEqual((decisionInfo[Constants.DecisionInfoKeys.variableValue] as! [String: Any])["value"] as! Int, 2)
656655
XCTAssertEqual(decisionInfo[Constants.DecisionInfoKeys.variableType] as! String, Constants.VariableValueType.json.rawValue)
657656
let sourceInfo: [String: Any] = decisionInfo[Constants.DecisionInfoKeys.sourceInfo]! as! [String: Any]
658657
XCTAssertNotNil(sourceInfo)
@@ -671,7 +670,7 @@ class DecisionListenerTests: XCTestCase {
671670
_ = notificationCenter.addDecisionNotificationListener { (_, _, _, decisionInfo) in
672671
XCTAssertEqual(decisionInfo[Constants.DecisionInfoKeys.featureEnabled] as! Bool, false)
673672
XCTAssertEqual(decisionInfo[Constants.DecisionInfoKeys.source] as! String, Constants.DecisionSource.featureTest.rawValue)
674-
XCTAssertEqual((decisionInfo[Constants.DecisionInfoKeys.variableValue] as! OptimizelyJSON).toString(), self.kVariableValueJSON)
673+
XCTAssertEqual((decisionInfo[Constants.DecisionInfoKeys.variableValue] as! [String: Any])["value"] as! Int, 1)
675674
XCTAssertEqual(decisionInfo[Constants.DecisionInfoKeys.variableType] as! String, Constants.VariableValueType.json.rawValue)
676675
let sourceInfo: [String: Any] = decisionInfo[Constants.DecisionInfoKeys.sourceInfo]! as! [String: Any]
677676
XCTAssertNotNil(sourceInfo)

0 commit comments

Comments
 (0)