Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions java/src/org/openqa/selenium/json/CollectionCoercer.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,21 @@
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import org.jspecify.annotations.Nullable;
import org.openqa.selenium.internal.Require;

class CollectionCoercer<T extends Collection, I extends T> extends TypeCoercer<T> {
class CollectionCoercer<T extends Collection<?>, I extends T> extends TypeCoercer<T> {

private final Class<T> stereotype;
private final JsonTypeCoercer coercer;
private final Supplier<I> supplier;
private final Function<I, Consumer<Object>> consumerFactory;
private final Function<I, Consumer<@Nullable Object>> consumerFactory;

public CollectionCoercer(
Class<T> stereotype,
JsonTypeCoercer coercer,
Supplier<I> supplier,
Function<I, Consumer<Object>> consumerFactory) {
Function<I, Consumer<@Nullable Object>> consumerFactory) {
this.stereotype = Require.nonNull("Stereotype", stereotype);
this.coercer = Require.nonNull("Coercer", coercer);
this.supplier = Require.nonNull("Supplier", supplier);
Expand Down Expand Up @@ -65,7 +66,7 @@ public BiFunction<JsonInput, PropertySetting, T> apply(Type type) {
return (jsonInput, setting) -> {
jsonInput.beginArray();
I toReturn = supplier.get();
Consumer<Object> consumer = consumerFactory.apply(toReturn);
Consumer<@Nullable Object> consumer = consumerFactory.apply(toReturn);
while (jsonInput.hasNext()) {
consumer.accept(coercer.coerce(jsonInput, valueType, setting));
}
Expand Down
5 changes: 4 additions & 1 deletion java/src/org/openqa/selenium/json/ConstructorCoercer.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ public BiFunction<JsonInput, PropertySetting, Object> apply(Type type) {
List<ConstructorCandidate> candidates = getConstructorCandidates(type);

return (jsonInput, setting) -> {
Map<String, Object> properties = coercer.coerce(jsonInput, Json.MAP_TYPE, setting);
Map<String, Object> properties =
Require.nonNull(
"Properties for " + type, coercer.coerce(jsonInput, Json.MAP_TYPE, setting));
ConstructorCandidate candidate = findConstructor(type, candidates, properties.keySet());

return candidate.create(type, properties, setting);
Expand Down Expand Up @@ -228,6 +230,7 @@ private Map<String, Integer> getParameterIndexes(Parameter[] parameters) {
return indexes;
}

@Nullable
private Object coerceValue(Object value, Type type, PropertySetting setting) {
StringWriter rawJson = new StringWriter();
try (JsonOutput output = new JsonOutput(rawJson)) {
Comment on lines +233 to 236

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remediation recommended

2. Nullable param typed nonnull 🐞 Bug ≡ Correctness

ConstructorCoercer.coerceValue is now annotated as returning @Nullable, but its value
parameter remains non-null even though callers pass properties.get(...) which can be null,
creating a nullness-contract violation and warnings under @NullMarked.
Agent Prompt
### Issue description
`coerceValue` can be invoked with `null` (when the JSON map contains a key with a `null` value), but its signature requires a non-null `Object value`.

### Issue Context
This is in `org.openqa.selenium.json` which is `@NullMarked`, so unannotated parameters are non-null by default. The body already handles `null` correctly by writing it as JSON and re-coercing.

### Fix Focus Areas
- java/src/org/openqa/selenium/json/ConstructorCoercer.java[233-242]
- java/src/org/openqa/selenium/json/ConstructorCoercer.java[285-293]

### Suggested change
Update the signature to `private Object coerceValue(@Nullable Object value, Type type, PropertySetting setting)` (keeping the existing `@Nullable` return annotation).

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Expand Down
2 changes: 1 addition & 1 deletion java/src/org/openqa/selenium/json/EnumCoercer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.lang.reflect.Type;
import java.util.function.BiFunction;

public class EnumCoercer<T extends Enum> extends TypeCoercer<T> {
public class EnumCoercer<T extends Enum<T>> extends TypeCoercer<T> {
Comment thread
qodo-code-review[bot] marked this conversation as resolved.

@Override
public boolean test(Class<?> aClass) {
Expand Down
13 changes: 7 additions & 6 deletions java/src/org/openqa/selenium/json/InstanceCoercer.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Stream;
import org.jspecify.annotations.Nullable;
import org.openqa.selenium.internal.Require;

class InstanceCoercer extends TypeCoercer<Object> {
Expand Down Expand Up @@ -168,9 +169,9 @@ private static Class<?> getClss(Type type) {

private static class TypeAndWriter {
private final Type type;
private final BiConsumer<Object, Object> writer;
private final BiConsumer<Object, @Nullable Object> writer;

TypeAndWriter(Type type, BiConsumer<Object, Object> writer) {
TypeAndWriter(Type type, BiConsumer<Object, @Nullable Object> writer) {
this.type = type;
this.writer = writer;
}
Expand All @@ -189,15 +190,15 @@ public TypeAndWriter apply(Field field) {
}
}

private static class FieldWriter implements BiConsumer<Object, Object> {
private static class FieldWriter implements BiConsumer<Object, @Nullable Object> {
private final Field field;

FieldWriter(Field field) {
this.field = field;
}

@Override
public void accept(Object instance, Object value) {
public void accept(Object instance, @Nullable Object value) {
try {
field.set(instance, value);
} catch (IllegalAccessException e) {
Expand Down Expand Up @@ -226,7 +227,7 @@ public TypeAndWriter apply(SimplePropertyDescriptor desc) {
}
}

private static class SimplePropertyWriter implements BiConsumer<Object, Object> {
private static class SimplePropertyWriter implements BiConsumer<Object, @Nullable Object> {
private final SimplePropertyDescriptor desc;
private final Method method;

Expand All @@ -236,7 +237,7 @@ private static class SimplePropertyWriter implements BiConsumer<Object, Object>
}

@Override
public void accept(Object instance, Object value) {
public void accept(Object instance, @Nullable Object value) {
method.setAccessible(true);
try {
method.invoke(instance, value);
Expand Down
4 changes: 2 additions & 2 deletions java/src/org/openqa/selenium/json/JsonInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -540,8 +540,8 @@ public <T> T readMapElement(String key) {
* @throws JsonException if coercion of the next element to the specified type fails
* @throws UncheckedIOException if an I/O exception is encountered
*/
public <T> List<T> readArray(Type type) {
List<T> toReturn = new ArrayList<>();
public <T> List<@Nullable T> readArray(Type type) {
List<@Nullable T> toReturn = new ArrayList<>();

beginArray();
while (hasNext()) {
Expand Down
6 changes: 2 additions & 4 deletions java/src/org/openqa/selenium/json/JsonTypeCoercer.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.function.BiFunction;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.jspecify.annotations.Nullable;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.internal.Require;
Expand Down Expand Up @@ -113,11 +114,8 @@ private JsonTypeCoercer(Stream<TypeCoercer<?>> coercers) {
(caps) -> ((k, v) -> caps.setCapability((String) k, v))));

// Container types
//noinspection unchecked
builder.add(new CollectionCoercer<>(List.class, this, ArrayList::new, (list) -> list::add));
//noinspection unchecked
builder.add(new CollectionCoercer<>(Set.class, this, HashSet::new, (set) -> set::add));
//noinspection unchecked
builder.add(
new CollectionCoercer<>(
Collection.class, this, ArrayList::new, (collection) -> collection::add));
Expand All @@ -139,7 +137,7 @@ private JsonTypeCoercer(Stream<TypeCoercer<?>> coercers) {
this.coercers = Collections.unmodifiableSet(builder);
}

<T> T coerce(JsonInput json, Type typeOfT, PropertySetting setter) {
<T> @Nullable T coerce(JsonInput json, Type typeOfT, PropertySetting setter) {
BiFunction<JsonInput, PropertySetting, Object> coercer =
knownCoercers.computeIfAbsent(typeOfT, this::buildCoercer);

Expand Down
11 changes: 7 additions & 4 deletions java/src/org/openqa/selenium/json/MapCoercer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,28 @@

package org.openqa.selenium.json;

import static java.util.Objects.requireNonNull;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
import org.jspecify.annotations.Nullable;

class MapCoercer<T, I extends T> extends TypeCoercer<T> {

private final Class<T> stereotype;
private final JsonTypeCoercer coercer;
private final Supplier<I> supplier;
private final Function<I, BiConsumer<Object, Object>> consumerFactory;
private final Function<I, BiConsumer<Object, @Nullable Object>> consumerFactory;

public MapCoercer(
Class<T> stereotype,
JsonTypeCoercer coercer,
Supplier<I> supplier,
Function<I, BiConsumer<Object, Object>> consumerFactory) {
Function<I, BiConsumer<Object, @Nullable Object>> consumerFactory) {
this.stereotype = stereotype;
this.coercer = coercer;
this.supplier = supplier;
Expand Down Expand Up @@ -66,7 +69,7 @@ public BiFunction<JsonInput, PropertySetting, T> apply(Type type) {
return (jsonInput, setting) -> {
jsonInput.beginObject();
I toReturn = supplier.get();
BiConsumer<Object, Object> consumer = consumerFactory.apply(toReturn);
BiConsumer<Object, @Nullable Object> consumer = consumerFactory.apply(toReturn);
// JSON should always have a string key, so we can take the fastpath
boolean stringKey = String.class.equals(keyType);

Expand All @@ -76,7 +79,7 @@ public BiFunction<JsonInput, PropertySetting, T> apply(Type type) {
if (stringKey) {
key = jsonInput.nextName();
} else {
key = coercer.coerce(jsonInput, keyType, setting);
key = requireNonNull(coercer.coerce(jsonInput, keyType, setting));
}
Object value = coercer.coerce(jsonInput, valueType, setting);

Expand Down
3 changes: 2 additions & 1 deletion java/src/org/openqa/selenium/json/ObjectCoercer.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.lang.reflect.Type;
import java.util.List;
import java.util.function.BiFunction;
import org.jspecify.annotations.Nullable;
import org.openqa.selenium.internal.Require;

class ObjectCoercer extends TypeCoercer<Object> {
Expand All @@ -36,7 +37,7 @@ public boolean test(Class type) {
}

@Override
public BiFunction<JsonInput, PropertySetting, Object> apply(Type type) {
public BiFunction<JsonInput, PropertySetting, @Nullable Object> apply(Type type) {
return (jsonInput, setting) -> {
Type target;

Expand Down
6 changes: 4 additions & 2 deletions java/src/org/openqa/selenium/json/TypeCoercer.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
import org.jspecify.annotations.Nullable;

public abstract class TypeCoercer<T>
Comment thread
qodo-code-review[bot] marked this conversation as resolved.
implements Predicate<Class<?>>, Function<Type, BiFunction<JsonInput, PropertySetting, T>> {
implements Predicate<Class<?>>,
Function<Type, BiFunction<JsonInput, PropertySetting, @Nullable T>> {

@Override
public abstract boolean test(Class<?> aClass);

@Override
public abstract BiFunction<JsonInput, PropertySetting, T> apply(Type type);
public abstract BiFunction<JsonInput, PropertySetting, @Nullable T> apply(Type type);
Comment thread
qodo-code-review[bot] marked this conversation as resolved.
}
19 changes: 15 additions & 4 deletions java/test/org/openqa/selenium/json/JsonInputTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -234,7 +233,7 @@ void shouldDecodeUnicodeEscapesProperly() {
String raw = "{\"text\": \"\\u003Chtml\"}";

try (JsonInput in = new JsonInput(new StringReader(raw), new JsonTypeCoercer(), BY_NAME)) {
Map<String, Object> map = in.read(MAP_TYPE);
Map<String, Object> map = in.readMap();

assertThat(map.get("text")).isEqualTo("<html");
}
Expand All @@ -245,7 +244,8 @@ void shouldCallFromJsonWithJsonInputParameter() {
String raw = "{\"message\": \"Cheese!\"}";

try (JsonInput in = new JsonInput(new StringReader(raw), new JsonTypeCoercer(), BY_NAME)) {
HasFromJsonWithJsonInputParameter obj = in.read(HasFromJsonWithJsonInputParameter.class);
HasFromJsonWithJsonInputParameter obj =
in.readNonNull(HasFromJsonWithJsonInputParameter.class);

assertThat(obj.getMessage()).isEqualTo("Cheese!");
}
Expand All @@ -256,12 +256,23 @@ void canReadListOfType() {
String raw = " [ 1 , 2 , 3 , 4 ] ";

try (JsonInput in = new JsonInput(new StringReader(raw), new JsonTypeCoercer(), BY_NAME)) {
List<Integer> array = in.readArray(Integer.class);
var array = in.readArray(Integer.class);

assertThat(array).containsExactly(1, 2, 3, 4);
}
}

@Test
Comment thread
qodo-code-review[bot] marked this conversation as resolved.
void canReadListOfType_null() {
String raw = "[null, null]";

try (JsonInput in = new JsonInput(new StringReader(raw), new JsonTypeCoercer(), BY_NAME)) {
var array = in.readArray(Integer.class);

assertThat(array).containsExactly(null, null);
}
}

@Test
void shouldBeAbleToReadDataLongerThanReadBuffer() {
char[] chars = new char[] {'c', 'h', 'e', 's'};
Expand Down
7 changes: 7 additions & 0 deletions java/test/org/openqa/selenium/json/JsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ void canConstructASimpleString() {
assertThat(text).isEqualTo("cheese");
}

@Test
void toTypeReturnsNullForTopLevelJsonNull() {
String text = new Json().toType("null", String.class);

assertThat(text).isNull();
Comment on lines +171 to +174

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remediation recommended

1. Nullable assigned to nonnull 🐞 Bug ⚙ Maintainability

In a @NullMarked package, the new test assigns a value it expects to be null into a non-null
String, contradicting the nullness contract and triggering nullness-checker/IDE warnings in the
test itself.
Agent Prompt
### Issue description
`JsonTest.toTypeReturnsNullForTopLevelJsonNull` assigns a value it asserts is `null` into a non-null `String` within the `org.openqa.selenium.json` package, which is `@NullMarked`.

### Issue Context
This PR’s goal is to surface nullability correctly; tests should follow the same contract to avoid introducing warnings/errors into the build.

### Fix Focus Areas
- java/test/org/openqa/selenium/json/JsonTest.java[170-175]
- java/src/org/openqa/selenium/json/package-info.java[18-21]

### Suggested change
Change `String text = ...` to `@Nullable String text = ...` (or equivalent) so the test matches the intended nullability.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

}

@Test
void canPopulateAMap() {
String raw = "{\"cheese\": \"brie\", \"foodstuff\": \"cheese\"}";
Expand Down
Loading