Skip to content

Commit

Permalink
Make the BeanToJsonConverter avoid fields from java.lang.Object
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
shs96c committed Mar 6, 2018
1 parent f176ae2 commit 76f27e0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,9 @@ private JsonElement mapObject(Object toConvert, int maxDepth, boolean skipNulls)
continue;
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.openqa.selenium.json.Json.MAP_TYPE;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSortedSet;
Expand Down Expand Up @@ -59,6 +60,10 @@
import org.openqa.selenium.remote.SessionId;

import java.awt.*;
import java.beans.FeatureDescriptor;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;
Expand All @@ -70,6 +75,7 @@
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.stream.Stream;


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

@Test
public void shouldNotIncludePropertiesFromJavaLangObjectOtherThanClass()
throws IntrospectionException {
String json = new BeanToJsonConverter().convert(new SimpleBean());

JsonObject converted = new JsonParser().parse(json).getAsJsonObject();

Stream.of(SimplePropertyDescriptor.getPropertyDescriptors(Object.class))
.filter(pd -> !"class".equals(pd.getName()))
.map(SimplePropertyDescriptor::getName)
.peek(System.out::println)
.forEach(name -> assertFalse(name, converted.keySet().contains(name)));
}

@SuppressWarnings("unused")
private static class SimpleBean {

Expand Down

0 comments on commit 76f27e0

Please sign in to comment.