Skip to content

Commit

Permalink
[java] fixed events without parameters #13109
Browse files Browse the repository at this point in the history
  • Loading branch information
joerg1985 committed Nov 8, 2023
1 parent 16c335f commit a5591ef
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
10 changes: 10 additions & 0 deletions java/src/org/openqa/selenium/bidi/ConverterFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,14 @@ public static <X> Function<JsonInput, X> map(final String keyName, Type typeOfX)
return value;
};
}

public static Function<JsonInput, Void> empty() {
return input -> {
// expects an empty object
input.beginObject();
input.endObject();

return null;
};
}
}
7 changes: 4 additions & 3 deletions java/src/org/openqa/selenium/devtools/CdpClientGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,9 @@ public BodyDeclaration<?> toMethodDeclaration() {
.get()
.addStatement(
String.format(
"return new Event<>(\"%s.%s\", input -> null);", domain.name, name));
} else if (type instanceof ObjectType) {
"return new Event<>(\"%s.%s\", input -> ConverterFunctions.empty());",
domain.name, name));
} else if (type instanceof ObjectType || type instanceof ArrayType) {
methodDecl
.getBody()
.get()
Expand Down Expand Up @@ -659,7 +660,7 @@ public MethodDeclaration toMethodDeclaration() {
body.addStatement(
String.format(
"return new Command<>(\"%s.%s\", Map.copyOf(params));", domain.name, name));
} else if (type instanceof ObjectType) {
} else if (type instanceof ObjectType || type instanceof ArrayType) {
body.addStatement(
String.format(
"return new Command<>(\"%s.%s\", Map.copyOf(params), input -> %s);",
Expand Down
19 changes: 11 additions & 8 deletions java/src/org/openqa/selenium/devtools/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,14 @@ private void handle(CharSequence data) {
// TODO: This is grossly inefficient. I apologise, and we should fix this.
try (StringReader reader = new StringReader(asString);
JsonInput input = JSON.newInput(reader)) {
Object value = null;
boolean hasParams = false;
Object params = null;
input.beginObject();
while (input.hasNext()) {
switch (input.nextName()) {
case "params":
value = event.getKey().getMapper().apply(input);
hasParams = true;
params = event.getKey().getMapper().apply(input);
break;

default:
Expand All @@ -314,21 +316,22 @@ private void handle(CharSequence data) {
}
input.endObject();

if (value == null) {
// Do nothing.
if (!hasParams) {
LOG.fine(
"suppressed event '"
+ event.getKey().getMethod()
+ "', no params in input");
return;
}

final Object finalValue = value;

for (Consumer<?> action : event.getValue()) {
@SuppressWarnings("unchecked")
Consumer<Object> obj = (Consumer<Object>) action;
LOG.log(
getDebugLogLevel(),
"Calling callback for {0} using {1} being passed {2}",
new Object[] {event.getKey(), obj, finalValue});
obj.accept(finalValue);
new Object[] {event.getKey(), obj, params});
obj.accept(params);
}
}
});
Expand Down

0 comments on commit a5591ef

Please sign in to comment.