Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FSSDK-9776] handle duplicate experiment key #523

Merged
merged 11 commits into from
Nov 30, 2023
7 changes: 6 additions & 1 deletion Sources/Optimizely/OptimizelyConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ struct OptimizelyConfigImp: OptimizelyConfig {
var attributes: [OptimizelyAttribute] = []
var audiences: [OptimizelyAudience] = []
var events: [OptimizelyEvent] = []

private var logger = OPTLoggerFactory.getLogger()
mikechu-optimizely marked this conversation as resolved.
Show resolved Hide resolved

init(projectConfig: ProjectConfig) {
guard let project = projectConfig.project else { return }

Expand Down Expand Up @@ -150,7 +151,11 @@ extension OptimizelyConfigImp {

func makeExperimentsMap(project: Project, experiments: [Experiment]) -> [String: Experiment] {
var map = [String: Experiment]()

experiments.forEach {
if map.keys.contains($0.key) {
logger.w("Duplicate experiment keys found in datafile: \($0.key)")
}
muzahidul-opti marked this conversation as resolved.
Show resolved Hide resolved
map[$0.key] = $0
}
return map
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,67 @@ class OptimizelyClientTests_OptimizelyConfig: XCTestCase {
let result = try? self.optimizely.getOptimizelyConfig()
XCTAssertNil(result)
}

func testOptimizelyConfigWithDuplicateKeys() {
let exp0: [String : Any] = [
"id": "10001",
"key": "duplicate_key",
"status": "Running",
"layerId": "22222",
"variations": [],
"trafficAllocation": [],
"audienceIds": ["33333"],
"audienceConditions": [],
"forcedVariations": ["12345": "1234567890"]
]

let exp1: [String : Any] = [
"id": "10005",
"key": "duplicate_key",
"status": "Running",
"layerId": "22222",
"variations": [],
"trafficAllocation": [],
"audienceIds": ["33333"],
"audienceConditions": [],
"forcedVariations": ["12345": "1234567890"]
]

var projectData: [String: Any] = [
"version": "4",
"projectId": "11111",
"experiments": [],
"audiences": [],
"groups": [],
"attributes": [],
"accountId": "1234567890",
"events": [],
"revision": "5",
"anonymizeIP": true,
"rollouts": [],
"typedAudiences": [],
"integrations": [],
"featureFlags": [],
"botFiltering": false,
"sendFlagDecisions": true
]

projectData["experiments"] = [exp0, exp1]
let model: Project = try! OTUtils.model(from: projectData)
let projectConfig = ProjectConfig()
projectConfig.project = model

optimizely = OptimizelyClient(
sdkKey: "demo_key"
)

optimizely.config = projectConfig

let optimizelyExpMap: [String: OptimizelyExperiment] = try! optimizely.getOptimizelyConfig().experimentsMap

XCTAssertEqual(optimizelyExpMap.count, 1)
XCTAssertEqual(optimizelyExpMap["duplicate_key"]?.id, "10005")
mikechu-optimizely marked this conversation as resolved.
Show resolved Hide resolved
}

}

Expand Down
2 changes: 1 addition & 1 deletion Tests/OptimizelyTests-DataModel/ProjectConfigTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class ProjectConfigTests: XCTestCase {
XCTAssertEqual(featureMap["1003"], ["2002"])
XCTAssertEqual(featureMap["1004"], ["2002"])
}
func testFlagVariations() {
let datafile = OTUtils.loadJSONDatafile("decide_datafile")!
let optimizely = OptimizelyClient(sdkKey: "12345",
Expand Down
Loading