Skip to content

Commit fa1171b

Browse files
older versions of JSONArray do not support iteration. In order to avoid crashes in this case, we use the get(index) instead (#283)
1 parent 8b4a1d2 commit fa1171b

File tree

2 files changed

+38
-24
lines changed

2 files changed

+38
-24
lines changed

core-api/src/main/java/com/optimizely/ab/config/parser/JsonConfigParser.java

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ private List<Experiment> parseExperiments(JSONArray experimentJson) {
125125
private List<Experiment> parseExperiments(JSONArray experimentJson, String groupId) {
126126
List<Experiment> experiments = new ArrayList<Experiment>(experimentJson.length());
127127

128-
for (Object obj : experimentJson) {
128+
for (int i = 0; i < experimentJson.length(); i++) {
129+
Object obj = experimentJson.get(i);
129130
JSONObject experimentObject = (JSONObject) obj;
130131
String id = experimentObject.getString("id");
131132
String key = experimentObject.getString("key");
@@ -136,7 +137,8 @@ private List<Experiment> parseExperiments(JSONArray experimentJson, String group
136137
JSONArray audienceIdsJson = experimentObject.getJSONArray("audienceIds");
137138
List<String> audienceIds = new ArrayList<String>(audienceIdsJson.length());
138139

139-
for (Object audienceIdObj : audienceIdsJson) {
140+
for (int j = 0; j < audienceIdsJson.length(); j++) {
141+
Object audienceIdObj = audienceIdsJson.get(j);
140142
audienceIds.add((String) audienceIdObj);
141143
}
142144

@@ -163,7 +165,8 @@ private List<Experiment> parseExperiments(JSONArray experimentJson, String group
163165
private List<String> parseExperimentIds(JSONArray experimentIdsJson) {
164166
ArrayList<String> experimentIds = new ArrayList<String>(experimentIdsJson.length());
165167

166-
for (Object experimentIdObj : experimentIdsJson) {
168+
for (int i = 0; i < experimentIdsJson.length(); i++) {
169+
Object experimentIdObj = experimentIdsJson.get(i);
167170
experimentIds.add((String) experimentIdObj);
168171
}
169172

@@ -173,7 +176,8 @@ private List<String> parseExperimentIds(JSONArray experimentIdsJson) {
173176
private List<FeatureFlag> parseFeatureFlags(JSONArray featureFlagJson) {
174177
List<FeatureFlag> featureFlags = new ArrayList<FeatureFlag>(featureFlagJson.length());
175178

176-
for (Object obj : featureFlagJson) {
179+
for (int i = 0; i < featureFlagJson.length();i++) {
180+
Object obj = featureFlagJson.get(i);
177181
JSONObject featureFlagObject = (JSONObject) obj;
178182
String id = featureFlagObject.getString("id");
179183
String key = featureFlagObject.getString("key");
@@ -198,7 +202,8 @@ private List<FeatureFlag> parseFeatureFlags(JSONArray featureFlagJson) {
198202
private List<Variation> parseVariations(JSONArray variationJson) {
199203
List<Variation> variations = new ArrayList<Variation>(variationJson.length());
200204

201-
for (Object obj : variationJson) {
205+
for (int i = 0; i < variationJson.length(); i++) {
206+
Object obj = variationJson.get(i);
202207
JSONObject variationObject = (JSONObject) obj;
203208
String id = variationObject.getString("id");
204209
String key = variationObject.getString("key");
@@ -234,7 +239,8 @@ private Map<String, String> parseForcedVariations(JSONObject forcedVariationJson
234239
private List<TrafficAllocation> parseTrafficAllocation(JSONArray trafficAllocationJson) {
235240
List<TrafficAllocation> trafficAllocation = new ArrayList<TrafficAllocation>(trafficAllocationJson.length());
236241

237-
for (Object obj : trafficAllocationJson) {
242+
for (int i = 0; i < trafficAllocationJson.length();i++) {
243+
Object obj = trafficAllocationJson.get(i);
238244
JSONObject allocationObject = (JSONObject) obj;
239245
String entityId = allocationObject.getString("entityId");
240246
int endOfRange = allocationObject.getInt("endOfRange");
@@ -248,7 +254,8 @@ private List<TrafficAllocation> parseTrafficAllocation(JSONArray trafficAllocati
248254
private List<Attribute> parseAttributes(JSONArray attributeJson) {
249255
List<Attribute> attributes = new ArrayList<Attribute>(attributeJson.length());
250256

251-
for (Object obj : attributeJson) {
257+
for (int i = 0; i < attributeJson.length();i++) {
258+
Object obj = attributeJson.get(i);
252259
JSONObject attributeObject = (JSONObject) obj;
253260
String id = attributeObject.getString("id");
254261
String key = attributeObject.getString("key");
@@ -262,7 +269,8 @@ private List<Attribute> parseAttributes(JSONArray attributeJson) {
262269
private List<EventType> parseEvents(JSONArray eventJson) {
263270
List<EventType> events = new ArrayList<EventType>(eventJson.length());
264271

265-
for (Object obj : eventJson) {
272+
for (int i = 0; i < eventJson.length(); i++) {
273+
Object obj = eventJson.get(i);
266274
JSONObject eventObject = (JSONObject) obj;
267275
List<String> experimentIds = parseExperimentIds(eventObject.getJSONArray("experimentIds"));
268276

@@ -278,7 +286,8 @@ private List<EventType> parseEvents(JSONArray eventJson) {
278286
private List<Audience> parseAudiences(JSONArray audienceJson) {
279287
List<Audience> audiences = new ArrayList<Audience>(audienceJson.length());
280288

281-
for (Object obj : audienceJson) {
289+
for (int i = 0; i < audienceJson.length(); i++) {
290+
Object obj = audienceJson.get(i);
282291
JSONObject audienceObject = (JSONObject) obj;
283292
String id = audienceObject.getString("id");
284293
String key = audienceObject.getString("name");
@@ -304,7 +313,8 @@ private List<Audience> parseAudiences(JSONArray audienceJson) {
304313
private List<Audience> parseTypedAudiences(JSONArray audienceJson) {
305314
List<Audience> audiences = new ArrayList<Audience>(audienceJson.length());
306315

307-
for (Object obj : audienceJson) {
316+
for (int i = 0; i < audienceJson.length(); i++) {
317+
Object obj = audienceJson.get(i);
308318
JSONObject audienceObject = (JSONObject) obj;
309319
String id = audienceObject.getString("id");
310320
String key = audienceObject.getString("name");
@@ -320,7 +330,8 @@ private List<Audience> parseTypedAudiences(JSONArray audienceJson) {
320330
private List<Group> parseGroups(JSONArray groupJson) {
321331
List<Group> groups = new ArrayList<Group>(groupJson.length());
322332

323-
for (Object obj : groupJson) {
333+
for (int i = 0; i < groupJson.length(); i++) {
334+
Object obj = groupJson.get(i);
324335
JSONObject groupObject = (JSONObject) obj;
325336
String id = groupObject.getString("id");
326337
String policy = groupObject.getString("policy");
@@ -334,10 +345,11 @@ private List<Group> parseGroups(JSONArray groupJson) {
334345
return groups;
335346
}
336347

337-
private List<FeatureVariable> parseFeatureVariables(JSONArray FeatureVariablesJson) {
338-
List<FeatureVariable> FeatureVariables = new ArrayList<FeatureVariable>(FeatureVariablesJson.length());
348+
private List<FeatureVariable> parseFeatureVariables(JSONArray featureVariablesJson) {
349+
List<FeatureVariable> featureVariables = new ArrayList<FeatureVariable>(featureVariablesJson.length());
339350

340-
for (Object obj : FeatureVariablesJson) {
351+
for (int i = 0; i < featureVariablesJson.length();i++) {
352+
Object obj = featureVariablesJson.get(i);
341353
JSONObject FeatureVariableObject = (JSONObject) obj;
342354
String id = FeatureVariableObject.getString("id");
343355
String key = FeatureVariableObject.getString("key");
@@ -348,19 +360,20 @@ private List<FeatureVariable> parseFeatureVariables(JSONArray FeatureVariablesJs
348360
status = FeatureVariable.VariableStatus.fromString(FeatureVariableObject.getString("status"));
349361
}
350362

351-
FeatureVariables.add(new FeatureVariable(id, key, defaultValue, status, type));
363+
featureVariables.add(new FeatureVariable(id, key, defaultValue, status, type));
352364
}
353365

354-
return FeatureVariables;
366+
return featureVariables;
355367
}
356368

357-
private List<FeatureVariableUsageInstance> parseFeatureVariableInstances(JSONArray FeatureVariableInstancesJson) {
358-
List<FeatureVariableUsageInstance> featureVariableUsageInstances = new ArrayList<FeatureVariableUsageInstance>(FeatureVariableInstancesJson.length());
369+
private List<FeatureVariableUsageInstance> parseFeatureVariableInstances(JSONArray featureVariableInstancesJson) {
370+
List<FeatureVariableUsageInstance> featureVariableUsageInstances = new ArrayList<FeatureVariableUsageInstance>(featureVariableInstancesJson.length());
359371

360-
for (Object obj : FeatureVariableInstancesJson) {
361-
JSONObject FeatureVariableInstanceObject = (JSONObject) obj;
362-
String id = FeatureVariableInstanceObject.getString("id");
363-
String value = FeatureVariableInstanceObject.getString("value");
372+
for (int i = 0; i < featureVariableInstancesJson.length(); i++) {
373+
Object obj = featureVariableInstancesJson.get(i);
374+
JSONObject featureVariableInstanceObject = (JSONObject) obj;
375+
String id = featureVariableInstanceObject.getString("id");
376+
String value = featureVariableInstanceObject.getString("value");
364377

365378
featureVariableUsageInstances.add(new FeatureVariableUsageInstance(id, value));
366379
}
@@ -371,7 +384,8 @@ private List<FeatureVariableUsageInstance> parseFeatureVariableInstances(JSONArr
371384
private List<Rollout> parseRollouts(JSONArray rolloutsJson) {
372385
List<Rollout> rollouts = new ArrayList<Rollout>(rolloutsJson.length());
373386

374-
for (Object obj : rolloutsJson) {
387+
for (int i = 0; i < rolloutsJson.length(); i++) {
388+
Object obj = rolloutsJson.get(i);
375389
JSONObject rolloutObject = (JSONObject) obj;
376390
String id = rolloutObject.getString("id");
377391
List<Experiment> experiments = parseExperiments(rolloutObject.getJSONArray("experiments"));

core-api/src/main/java/com/optimizely/ab/event/internal/serializer/JacksonSerializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
class JacksonSerializer implements Serializer {
2525

26-
private final ObjectMapper mapper =
26+
private ObjectMapper mapper =
2727
new ObjectMapper().setPropertyNamingStrategy(
2828
PropertyNamingStrategy.SNAKE_CASE);
2929

0 commit comments

Comments
 (0)