Skip to content

Commit f8924ca

Browse files
juangjsevaseva
authored andcommitted
If JsonParser.parse() fails to parse a string obtained from a reflective call to a toJson() method, assume it is a primitive string.
This is the case for, e.g., FirefoxProfile.toJson(), which returns a base64-encoded zip file. That string likely contains at least one '/', which the Gson parser rejects. Signed-off-by: Seva Lotoshnikov <[email protected]>
1 parent 07258f1 commit f8924ca

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

Diff for: java/client/src/org/openqa/selenium/remote/BeanToJsonConverter.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.gson.JsonNull;
2323
import com.google.gson.JsonObject;
2424
import com.google.gson.JsonParser;
25+
import com.google.gson.JsonParseException;
2526
import com.google.gson.JsonPrimitive;
2627

2728
import org.openqa.selenium.Capabilities;
@@ -122,7 +123,7 @@ private JsonElement convertObject(Object toConvert, int maxDepth) throws Excepti
122123
}
123124
return converted;
124125
}
125-
126+
126127
if (toConvert instanceof SessionLogs) {
127128
return convertObject(((SessionLogs)toConvert).getAll(), maxDepth - 1);
128129
}
@@ -203,7 +204,11 @@ private JsonElement convertObject(Object toConvert, int maxDepth) throws Excepti
203204
if (res instanceof JsonElement) {
204205
return (JsonElement) res;
205206
} else {
206-
return new JsonParser().parse((String) res);
207+
try {
208+
return new JsonParser().parse((String) res);
209+
} catch (JsonParseException e) {
210+
return new JsonPrimitive((String) res);
211+
}
207212
}
208213
} catch (IllegalArgumentException e) {
209214
throw new WebDriverException(e);

Diff for: java/client/test/org/openqa/selenium/remote/BeanToJsonConverterTest.java

+20-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.gson.JsonArray;
2323
import com.google.gson.JsonObject;
2424
import com.google.gson.JsonParser;
25+
import com.google.gson.JsonSyntaxException;
2526

2627
import org.junit.Test;
2728
import org.junit.runner.RunWith;
@@ -180,7 +181,7 @@ public void testShouldBeAbleToConvertASessionId() {
180181
assertEquals("some id", converted.get("value").getAsString());
181182
}
182183

183-
//@Test
184+
@Test
184185
public void testShouldBeAbleToConvertAJsonObject() {
185186
JsonObject obj = new JsonObject();
186187
obj.addProperty("key", "value");
@@ -258,10 +259,27 @@ public void testShouldConvertAProxyCorrectly() {
258259
@Test
259260
public void testShouldCallToJsonMethodIfPresent() {
260261
String json = new BeanToJsonConverter().convert(new JsonAware("converted"));
261-
262262
assertEquals("\"converted\"", json);
263263
}
264264

265+
@Test
266+
public void testConvertsToJsonMethodResultToPrimitiveIfItIsNotJson() {
267+
// We want this parsed as a string primitive, but JsonParser will reject it
268+
// as malformed because of the slash.
269+
String raw = "gnu/linux";
270+
271+
try {
272+
// Make sure that the parser does actually reject this so the test is
273+
// meaningful. If this stops failing, choose a different malformed JSON
274+
// string.
275+
new JsonParser().parse(raw).toString();
276+
fail("Expected a parser exception when parsing: " + raw);
277+
} catch (JsonSyntaxException expected) {
278+
}
279+
280+
String json = new BeanToJsonConverter().convert(new JsonAware(raw));
281+
assertEquals("\"gnu/linux\"", json);
282+
}
265283

266284
private void verifyStackTraceInJson(String json, StackTraceElement[] stackTrace) {
267285
int posOfLastStackTraceElement = 0;

0 commit comments

Comments
 (0)