Skip to content

Commit 76f27e0

Browse files
committed
Make the BeanToJsonConverter avoid fields from java.lang.Object
I'm suspicious about the need for us to send "class" backwards and forwards, but it's apparently used by to reconstitute exceptions so we should probably leave it.
1 parent f176ae2 commit 76f27e0

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

java/client/src/org/openqa/selenium/json/BeanToJsonConverter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,9 @@ private JsonElement mapObject(Object toConvert, int maxDepth, boolean skipNulls)
245245
continue;
246246
}
247247

248+
// Only include methods not on java.lang.Object to stop things being super-noisy
248249
Method readMethod = pd.getReadMethod();
249-
if (readMethod == null) {
250+
if (readMethod == null || Object.class.equals(readMethod.getDeclaringClass())) {
250251
continue;
251252
}
252253

java/client/test/org/openqa/selenium/json/BeanToJsonConverterTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import static org.junit.Assert.assertThat;
2626
import static org.junit.Assert.assertTrue;
2727
import static org.junit.Assert.fail;
28+
import static org.openqa.selenium.json.Json.MAP_TYPE;
2829

2930
import com.google.common.collect.ImmutableMap;
3031
import com.google.common.collect.ImmutableSortedSet;
@@ -59,6 +60,10 @@
5960
import org.openqa.selenium.remote.SessionId;
6061

6162
import java.awt.*;
63+
import java.beans.FeatureDescriptor;
64+
import java.beans.IntrospectionException;
65+
import java.beans.Introspector;
66+
import java.beans.PropertyDescriptor;
6267
import java.io.StringReader;
6368
import java.net.MalformedURLException;
6469
import java.net.URL;
@@ -70,6 +75,7 @@
7075
import java.util.Set;
7176
import java.util.concurrent.TimeUnit;
7277
import java.util.logging.Level;
78+
import java.util.stream.Stream;
7379

7480

7581
@RunWith(JUnit4.class)
@@ -534,6 +540,20 @@ public void shouldConvertAUrlToAString() throws MalformedURLException {
534540
assertEquals(url.toExternalForm(), converted.get("url").getAsString());
535541
}
536542

543+
@Test
544+
public void shouldNotIncludePropertiesFromJavaLangObjectOtherThanClass()
545+
throws IntrospectionException {
546+
String json = new BeanToJsonConverter().convert(new SimpleBean());
547+
548+
JsonObject converted = new JsonParser().parse(json).getAsJsonObject();
549+
550+
Stream.of(SimplePropertyDescriptor.getPropertyDescriptors(Object.class))
551+
.filter(pd -> !"class".equals(pd.getName()))
552+
.map(SimplePropertyDescriptor::getName)
553+
.peek(System.out::println)
554+
.forEach(name -> assertFalse(name, converted.keySet().contains(name)));
555+
}
556+
537557
@SuppressWarnings("unused")
538558
private static class SimpleBean {
539559

0 commit comments

Comments
 (0)