Skip to content

Commit 77c4cfc

Browse files
[FSSDK-12337] Move experiment type validation from constructor to parsers
Move the experiment type validation out of the DatafileProjectConfig constructor (which cannot declare throws ConfigParseException) into each of the 4 config parsers where the exception is already handled.
1 parent 2204fcd commit 77c4cfc

File tree

5 files changed

+94
-16
lines changed

5 files changed

+94
-16
lines changed

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -195,18 +195,6 @@ public DatafileProjectConfig(String accountId,
195195
allExperiments.addAll(experiments);
196196
allExperiments.addAll(aggregateGroupExperiments(groups));
197197

198-
Set<String> validExperimentTypes = new HashSet<>(Arrays.asList(
199-
Experiment.TYPE_AB, Experiment.TYPE_MAB, Experiment.TYPE_CMAB,
200-
Experiment.TYPE_TD, Experiment.TYPE_FR
201-
));
202-
for (Experiment experiment : allExperiments) {
203-
if (experiment.getType() != null && !validExperimentTypes.contains(experiment.getType())) {
204-
throw new ConfigParseException(
205-
String.format("Experiment \"%s\" has invalid type \"%s\". Valid types: %s.",
206-
experiment.getKey(), experiment.getType(), validExperimentTypes));
207-
}
208-
}
209-
210198
// Inject "everyone else" variation into feature_rollout experiments
211199
allExperiments = injectFeatureRolloutVariations(allExperiments, this.featureFlags, this.rollouts);
212200

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
import com.optimizely.ab.config.audience.TypedAudience;
2828

2929
import java.lang.reflect.Type;
30-
import java.util.Collections;
31-
import java.util.List;
30+
import java.util.*;
3231

3332
/**
3433
* GSON {@link DatafileProjectConfig} deserializer to allow the constructor to be used.
@@ -127,6 +126,28 @@ public ProjectConfig deserialize(JsonElement json, Type typeOfT, JsonDeserializa
127126
region = jsonObject.get("region").getAsString();
128127
}
129128

129+
// Validate experiment types
130+
Set<String> validExperimentTypes = new HashSet<>(Arrays.asList(
131+
Experiment.TYPE_AB, Experiment.TYPE_MAB, Experiment.TYPE_CMAB,
132+
Experiment.TYPE_TD, Experiment.TYPE_FR
133+
));
134+
for (Experiment experiment : experiments) {
135+
if (experiment.getType() != null && !validExperimentTypes.contains(experiment.getType())) {
136+
throw new JsonParseException(
137+
String.format("Experiment \"%s\" has invalid type \"%s\". Valid types: %s.",
138+
experiment.getKey(), experiment.getType(), validExperimentTypes));
139+
}
140+
}
141+
for (Group group : groups) {
142+
for (Experiment experiment : group.getExperiments()) {
143+
if (experiment.getType() != null && !validExperimentTypes.contains(experiment.getType())) {
144+
throw new JsonParseException(
145+
String.format("Experiment \"%s\" has invalid type \"%s\". Valid types: %s.",
146+
experiment.getKey(), experiment.getType(), validExperimentTypes));
147+
}
148+
}
149+
}
150+
130151
return new DatafileProjectConfig(
131152
accountId,
132153
anonymizeIP,

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626
import com.optimizely.ab.config.audience.TypedAudience;
2727

2828
import java.io.IOException;
29-
import java.util.Collections;
30-
import java.util.List;
29+
import java.util.*;
3130

3231
class DatafileJacksonDeserializer extends JsonDeserializer<DatafileProjectConfig> {
3332
@Override
@@ -101,6 +100,28 @@ public DatafileProjectConfig deserialize(JsonParser parser, DeserializationConte
101100
region = node.get("region").textValue();
102101
}
103102

103+
// Validate experiment types
104+
Set<String> validExperimentTypes = new HashSet<>(Arrays.asList(
105+
Experiment.TYPE_AB, Experiment.TYPE_MAB, Experiment.TYPE_CMAB,
106+
Experiment.TYPE_TD, Experiment.TYPE_FR
107+
));
108+
for (Experiment experiment : experiments) {
109+
if (experiment.getType() != null && !validExperimentTypes.contains(experiment.getType())) {
110+
throw new IOException(
111+
String.format("Experiment \"%s\" has invalid type \"%s\". Valid types: %s.",
112+
experiment.getKey(), experiment.getType(), validExperimentTypes));
113+
}
114+
}
115+
for (Group group : groups) {
116+
for (Experiment experiment : group.getExperiments()) {
117+
if (experiment.getType() != null && !validExperimentTypes.contains(experiment.getType())) {
118+
throw new IOException(
119+
String.format("Experiment \"%s\" has invalid type \"%s\". Valid types: %s.",
120+
experiment.getKey(), experiment.getType(), validExperimentTypes));
121+
}
122+
}
123+
}
124+
104125
return new DatafileProjectConfig(
105126
accountId,
106127
anonymizeIP,

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,28 @@ public ProjectConfig parseProjectConfig(@Nonnull String json) throws ConfigParse
105105
String regionString = rootObject.getString("region");
106106
}
107107

108+
// Validate experiment types
109+
Set<String> validExperimentTypes = new HashSet<>(Arrays.asList(
110+
Experiment.TYPE_AB, Experiment.TYPE_MAB, Experiment.TYPE_CMAB,
111+
Experiment.TYPE_TD, Experiment.TYPE_FR
112+
));
113+
for (Experiment experiment : experiments) {
114+
if (experiment.getType() != null && !validExperimentTypes.contains(experiment.getType())) {
115+
throw new ConfigParseException(
116+
String.format("Experiment \"%s\" has invalid type \"%s\". Valid types: %s.",
117+
experiment.getKey(), experiment.getType(), validExperimentTypes));
118+
}
119+
}
120+
for (Group group : groups) {
121+
for (Experiment experiment : group.getExperiments()) {
122+
if (experiment.getType() != null && !validExperimentTypes.contains(experiment.getType())) {
123+
throw new ConfigParseException(
124+
String.format("Experiment \"%s\" has invalid type \"%s\". Valid types: %s.",
125+
experiment.getKey(), experiment.getType(), validExperimentTypes));
126+
}
127+
}
128+
}
129+
108130
return new DatafileProjectConfig(
109131
accountId,
110132
anonymizeIP,
@@ -127,6 +149,8 @@ public ProjectConfig parseProjectConfig(@Nonnull String json) throws ConfigParse
127149
rollouts,
128150
integrations
129151
);
152+
} catch (ConfigParseException e) {
153+
throw e;
130154
} catch (RuntimeException e) {
131155
throw new ConfigParseException("Unable to parse datafile: " + json, e);
132156
} catch (Exception e) {

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,28 @@ public ProjectConfig parseProjectConfig(@Nonnull String json) throws ConfigParse
108108
String regionString = (String) rootObject.get("region");
109109
}
110110

111+
// Validate experiment types
112+
Set<String> validExperimentTypes = new HashSet<>(Arrays.asList(
113+
Experiment.TYPE_AB, Experiment.TYPE_MAB, Experiment.TYPE_CMAB,
114+
Experiment.TYPE_TD, Experiment.TYPE_FR
115+
));
116+
for (Experiment experiment : experiments) {
117+
if (experiment.getType() != null && !validExperimentTypes.contains(experiment.getType())) {
118+
throw new ConfigParseException(
119+
String.format("Experiment \"%s\" has invalid type \"%s\". Valid types: %s.",
120+
experiment.getKey(), experiment.getType(), validExperimentTypes));
121+
}
122+
}
123+
for (Group group : groups) {
124+
for (Experiment experiment : group.getExperiments()) {
125+
if (experiment.getType() != null && !validExperimentTypes.contains(experiment.getType())) {
126+
throw new ConfigParseException(
127+
String.format("Experiment \"%s\" has invalid type \"%s\". Valid types: %s.",
128+
experiment.getKey(), experiment.getType(), validExperimentTypes));
129+
}
130+
}
131+
}
132+
111133
return new DatafileProjectConfig(
112134
accountId,
113135
anonymizeIP,
@@ -130,6 +152,8 @@ public ProjectConfig parseProjectConfig(@Nonnull String json) throws ConfigParse
130152
rollouts,
131153
integrations
132154
);
155+
} catch (ConfigParseException e) {
156+
throw e;
133157
} catch (RuntimeException ex) {
134158
throw new ConfigParseException("Unable to parse datafile: " + json, ex);
135159
} catch (Exception e) {

0 commit comments

Comments
 (0)