Skip to content

Commit 6a0ace3

Browse files
change typedAudiences.conditions to be non-string encoded but still support audience.conditions as a string (#229)
1 parent da3b24f commit 6a0ace3

File tree

5 files changed

+26
-16
lines changed

5 files changed

+26
-16
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ public Audience deserialize(JsonElement json, Type typeOfT, JsonDeserializationC
5050
String id = jsonObject.get("id").getAsString();
5151
String name = jsonObject.get("name").getAsString();
5252

53-
JsonElement conditionsElement = parser.parse(jsonObject.get("conditions").getAsString());
53+
JsonElement conditionsElement = jsonObject.get("conditions");
54+
if (!conditionsElement.isJsonArray()) {
55+
conditionsElement = parser.parse(jsonObject.get("conditions").getAsString());
56+
}
5457
List<Object> rawObjectList = gson.fromJson(conditionsElement, List.class);
5558
Condition conditions = parseConditions(rawObjectList);
5659

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ public Audience deserialize(JsonParser parser, DeserializationContext context) t
4848
String id = node.get("id").textValue();
4949
String name = node.get("name").textValue();
5050

51-
String conditionsJson = node.get("conditions").textValue();
52-
JsonNode conditionsTree = objectMapper.readTree(conditionsJson);
53-
Condition conditions = parseConditions(conditionsTree);
51+
JsonNode conditionsJson = node.get("conditions");
52+
if (conditionsJson.isTextual()) {
53+
conditionsJson = objectMapper.readTree(conditionsJson.textValue());
54+
}
55+
Condition conditions = parseConditions(conditionsJson);
5456

5557
return new Audience(id, name, conditions);
5658
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,12 @@ private List<Audience> parseAudiences(JSONArray audienceJson) {
281281
JSONObject audienceObject = (JSONObject)obj;
282282
String id = audienceObject.getString("id");
283283
String key = audienceObject.getString("name");
284-
String conditionString = audienceObject.getString("conditions");
284+
Object conditionsObject = audienceObject.get("conditions");
285+
// audience.conditions will still be a string and is parsed here.
286+
// typedAudiences.conditions is not a string and is also parsed here.
287+
JSONArray conditionJson = (conditionsObject instanceof String) ?
288+
new JSONArray((String)conditionsObject) : (JSONArray)conditionsObject;
285289

286-
JSONArray conditionJson = new JSONArray(conditionString);
287290
Condition conditions = parseConditions(conditionJson);
288291
audiences.add(new Audience(id, key, conditions));
289292
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,11 @@ private List<Audience> parseAudiences(JSONArray audienceJson) throws ParseExcept
289289
JSONObject audienceObject = (JSONObject)obj;
290290
String id = (String)audienceObject.get("id");
291291
String key = (String)audienceObject.get("name");
292-
String conditionString = (String)audienceObject.get("conditions");
293-
294-
JSONArray conditionJson = (JSONArray)parser.parse(conditionString);
292+
Object conditionObject = audienceObject.get("conditions");
293+
// typedAudiences.conditions is a array.
294+
// audience.conditions is a string.
295+
JSONArray conditionJson = conditionObject instanceof String ?
296+
(JSONArray)parser.parse((String)conditionObject) : (JSONArray)conditionObject;
295297
Condition conditions = parseConditions(conditionJson);
296298
audiences.add(new Audience(id, key, conditions));
297299
}

core-api/src/test/resources/config/valid-project-config-v4.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,37 +31,37 @@
3131
{
3232
"id": "3468206643",
3333
"name": "BOOL",
34-
"conditions": "[\"and\", [\"or\", [\"or\", {\"name\": \"booleanKey\", \"type\": \"custom_attribute\", \"match\":\"exact\", \"value\":true}]]]"
34+
"conditions": ["and", ["or", ["or", {"name": "booleanKey", "type": "custom_attribute", "match":"exact", "value":true}]]]
3535
},
3636
{
3737
"id": "3468206644",
3838
"name": "INT",
39-
"conditions": "[\"and\", [\"or\", [\"or\", {\"name\": \"integerKey\", \"type\": \"custom_attribute\", \"match\":\"gt\", \"value\":1.0}]]]"
39+
"conditions": ["and", ["or", ["or", {"name": "integerKey", "type": "custom_attribute", "match":"gt", "value":1.0}]]]
4040
},
4141
{
4242
"id": "3468206645",
4343
"name": "DOUBLE",
44-
"conditions": "[\"and\", [\"or\", [\"or\", {\"name\": \"doubleKey\", \"type\": \"custom_attribute\", \"match\":\"lt\", \"value\":100.0}]]]"
44+
"conditions": ["and", ["or", ["or", {"name": "doubleKey", "type": "custom_attribute", "match":"lt", "value":100.0}]]]
4545
},
4646
{
4747
"id": "3468206642",
4848
"name": "Gryffindors",
49-
"conditions": "[\"and\", [\"or\", [\"or\", {\"name\": \"house\", \"type\": \"custom_attribute\", \"match\":\"exact\", \"value\":\"Gryffindor\"}]]]"
49+
"conditions": ["and", ["or", ["or", {"name": "house", "type": "custom_attribute", "match":"exact", "value":"Gryffindor"}]]]
5050
},
5151
{
5252
"id": "3988293898",
5353
"name": "Slytherins",
54-
"conditions": "[\"and\", [\"or\", [\"or\", {\"name\": \"house\", \"type\": \"custom_attribute\", \"match\":\"substring\", \"value\":\"Slytherin\"}]]]"
54+
"conditions": ["and", ["or", ["or", {"name": "house", "type": "custom_attribute", "match":"substring", "value":"Slytherin"}]]]
5555
},
5656
{
5757
"id": "4194404272",
5858
"name": "english_citizens",
59-
"conditions": "[\"and\", [\"or\", [\"or\", {\"name\": \"nationality\", \"type\": \"custom_attribute\", \"match\":\"exact\", \"value\":\"English\"}]]]"
59+
"conditions": ["and", ["or", ["or", {"name": "nationality", "type": "custom_attribute", "match":"exact", "value":"English"}]]]
6060
},
6161
{
6262
"id": "2196265320",
6363
"name": "audience_with_missing_value",
64-
"conditions": "[\"and\", [\"or\", [\"or\", {\"name\": \"nationality\", \"type\": \"custom_attribute\", \"value\": \"English\"}, {\"name\": \"nationality\", \"type\": \"custom_attribute\"}]]]"
64+
"conditions": ["and", ["or", ["or", {"name": "nationality", "type": "custom_attribute", "value": "English"}, {"name": "nationality", "type": "custom_attribute"}]]]
6565
}
6666
],
6767
"attributes": [

0 commit comments

Comments
 (0)