Skip to content

Commit 7dbd6ca

Browse files
authored
chore: use langAliases from api.json in api generator (#1907)
1 parent c3e4b92 commit 7dbd6ca

File tree

9 files changed

+43
-65
lines changed

9 files changed

+43
-65
lines changed

playwright/src/main/java/com/microsoft/playwright/Browser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,7 +1448,7 @@ default Page newPage() {
14481448
* @param title Title of the browser server, used for identification.
14491449
* @since v1.59
14501450
*/
1451-
default Bind bind(String title) {
1451+
default BindResult bind(String title) {
14521452
return bind(title, null);
14531453
}
14541454
/**
@@ -1457,7 +1457,7 @@ default Bind bind(String title) {
14571457
* @param title Title of the browser server, used for identification.
14581458
* @since v1.59
14591459
*/
1460-
Bind bind(String title, BindOptions options);
1460+
BindResult bind(String title, BindOptions options);
14611461
/**
14621462
* <strong>NOTE:</strong> This API controls <a href="https://www.chromium.org/developers/how-tos/trace-event-profiling-tool">Chromium Tracing</a>
14631463
* which is a low-level chromium-specific debugging tool. API to control <a

playwright/src/main/java/com/microsoft/playwright/Debugger.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public interface Debugger {
3939
*
4040
* @since v1.59
4141
*/
42-
PausedDetails pausedDetails();
42+
DebuggerPausedDetails pausedDetails();
4343
/**
4444
* Configures the debugger to pause before the next action is executed.
4545
*

playwright/src/main/java/com/microsoft/playwright/impl/BrowserImpl.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import com.google.gson.JsonElement;
2121
import com.google.gson.JsonObject;
2222
import com.microsoft.playwright.*;
23-
import com.microsoft.playwright.options.Bind;
23+
import com.microsoft.playwright.options.BindResult;
2424

2525
import java.io.IOException;
2626
import java.nio.charset.StandardCharsets;
@@ -197,7 +197,7 @@ public void startTracing(Page page, StartTracingOptions options) {
197197
}
198198

199199
@Override
200-
public Bind bind(String title, BindOptions options) {
200+
public BindResult bind(String title, BindOptions options) {
201201
JsonObject params = new JsonObject();
202202
params.addProperty("title", title);
203203
if (options != null) {
@@ -212,9 +212,9 @@ public Bind bind(String title, BindOptions options) {
212212
}
213213
}
214214
JsonObject result = sendMessage("startServer", params, NO_TIMEOUT).getAsJsonObject();
215-
Bind bind = new Bind();
216-
bind.endpoint = result.get("endpoint").getAsString();
217-
return bind;
215+
BindResult bindResult = new BindResult();
216+
bindResult.endpoint = result.get("endpoint").getAsString();
217+
return bindResult;
218218
}
219219

220220
@Override

playwright/src/main/java/com/microsoft/playwright/impl/DebuggerImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import com.google.gson.JsonObject;
2020
import com.microsoft.playwright.Debugger;
2121
import com.microsoft.playwright.options.Location;
22-
import com.microsoft.playwright.options.PausedDetails;
22+
import com.microsoft.playwright.options.DebuggerPausedDetails;
2323

2424
import java.util.ArrayList;
2525
import java.util.List;
@@ -28,7 +28,7 @@
2828

2929
class DebuggerImpl extends ChannelOwner implements Debugger {
3030
private final List<Runnable> pausedStateChangedHandlers = new ArrayList<>();
31-
private PausedDetails pausedDetails;
31+
private DebuggerPausedDetails pausedDetails;
3232

3333
DebuggerImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) {
3434
super(parent, type, guid, initializer);
@@ -38,7 +38,7 @@ class DebuggerImpl extends ChannelOwner implements Debugger {
3838
protected void handleEvent(String event, JsonObject params) {
3939
if ("pausedStateChanged".equals(event)) {
4040
if (params.has("pausedDetails") && !params.get("pausedDetails").isJsonNull()) {
41-
pausedDetails = gson().fromJson(params.get("pausedDetails"), PausedDetails.class);
41+
pausedDetails = gson().fromJson(params.get("pausedDetails"), DebuggerPausedDetails.class);
4242
} else {
4343
pausedDetails = null;
4444
}
@@ -59,7 +59,7 @@ public void offPausedStateChanged(Runnable handler) {
5959
}
6060

6161
@Override
62-
public PausedDetails pausedDetails() {
62+
public DebuggerPausedDetails pausedDetails() {
6363
return pausedDetails;
6464
}
6565

playwright/src/main/java/com/microsoft/playwright/options/Bind.java renamed to playwright/src/main/java/com/microsoft/playwright/options/BindResult.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package com.microsoft.playwright.options;
1818

19-
public class Bind {
19+
public class BindResult {
2020
public String endpoint;
2121

2222
}

playwright/src/main/java/com/microsoft/playwright/options/PausedDetails.java renamed to playwright/src/main/java/com/microsoft/playwright/options/DebuggerPausedDetails.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package com.microsoft.playwright.options;
1818

19-
public class PausedDetails {
19+
public class DebuggerPausedDetails {
2020
public Location location;
2121
public String title;
2222

playwright/src/test/java/com/microsoft/playwright/TestBrowserBind.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616

1717
package com.microsoft.playwright;
1818

19-
import com.microsoft.playwright.options.Bind;
19+
import com.microsoft.playwright.options.BindResult;
2020
import org.junit.jupiter.api.Test;
2121

2222
import static org.junit.jupiter.api.Assertions.*;
2323

2424
public class TestBrowserBind extends TestBase {
2525
@Test
2626
void shouldBindAndUnbindBrowser() {
27-
Bind serverInfo = browser.bind("default");
27+
BindResult serverInfo = browser.bind("default");
2828
try {
2929
assertNotNull(serverInfo);
3030
assertNotNull(serverInfo.endpoint);
@@ -36,7 +36,7 @@ void shouldBindAndUnbindBrowser() {
3636

3737
@Test
3838
void shouldBindWithCustomTitleAndOptions() {
39-
Bind serverInfo = browser.bind("my-title",
39+
BindResult serverInfo = browser.bind("my-title",
4040
new Browser.BindOptions().setHost("127.0.0.1").setPort(0));
4141
try {
4242
assertNotNull(serverInfo);

playwright/src/test/java/com/microsoft/playwright/TestDebugger.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package com.microsoft.playwright;
1818

19-
import com.microsoft.playwright.options.PausedDetails;
19+
import com.microsoft.playwright.options.DebuggerPausedDetails;
2020
import org.junit.jupiter.api.Test;
2121

2222
import static org.junit.jupiter.api.Assertions.*;
@@ -40,7 +40,7 @@ void shouldPauseAtNextAndResume() {
4040
dbg.onPausedStateChanged(() -> {
4141
if (!paused[0]) {
4242
paused[0] = true;
43-
PausedDetails details = dbg.pausedDetails();
43+
DebuggerPausedDetails details = dbg.pausedDetails();
4444
assertNotNull(details);
4545
assertTrue(details.title.contains("Click"), "title: " + details.title);
4646
dbg.resume();
@@ -63,7 +63,7 @@ void shouldStepWithNext() {
6363
dbg.onPausedStateChanged(() -> {
6464
if (!paused[0]) {
6565
paused[0] = true;
66-
PausedDetails details = dbg.pausedDetails();
66+
DebuggerPausedDetails details = dbg.pausedDetails();
6767
assertNotNull(details);
6868
assertTrue(details.title.contains("Click"), "title: " + details.title);
6969
dbg.next();
@@ -88,7 +88,7 @@ void shouldPauseAtPauseCall() {
8888
dbg.onPausedStateChanged(() -> {
8989
if (!paused[0]) {
9090
paused[0] = true;
91-
PausedDetails details = dbg.pausedDetails();
91+
DebuggerPausedDetails details = dbg.pausedDetails();
9292
assertNotNull(details);
9393
assertTrue(details.title.contains("Pause"), "title: " + details.title);
9494
dbg.resume();

tools/api-generator/src/main/java/com/microsoft/playwright/tools/ApiGenerator.java

Lines changed: 22 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -283,40 +283,20 @@ private static String wrapText(String text, int maxColumns, String prefix) {
283283
class TypeRef extends Element {
284284
String customType;
285285

286-
private static final Map<String, String> customTypeNames = new HashMap<>();
287-
static {
288-
customTypeNames.put("APIRequest.newContext.options.clientCertificates", "ClientCertificate");
289-
customTypeNames.put("Browser.newContext.options.clientCertificates", "ClientCertificate");
290-
customTypeNames.put("Browser.newPage.options.clientCertificates", "ClientCertificate");
291-
customTypeNames.put("BrowserType.launchPersistentContext.options.clientCertificates", "ClientCertificate");
292-
293-
customTypeNames.put("BrowserContext.addCookies.cookies", "Cookie");
294-
customTypeNames.put("BrowserContext.cookies", "Cookie");
295-
296-
customTypeNames.put("Request.headersArray", "HttpHeader");
297-
customTypeNames.put("Response.headersArray", "HttpHeader");
298-
customTypeNames.put("APIResponse.headersArray", "HttpHeader");
299-
300-
customTypeNames.put("Locator.selectOption.values", "SelectOption");
301-
customTypeNames.put("ElementHandle.selectOption.values", "SelectOption");
302-
customTypeNames.put("Frame.selectOption.values", "SelectOption");
303-
customTypeNames.put("Page.selectOption.values", "SelectOption");
304-
305-
customTypeNames.put("Locator.setInputFiles.files", "FilePayload");
306-
customTypeNames.put("ElementHandle.setInputFiles.files", "FilePayload");
307-
customTypeNames.put("FileChooser.setFiles.files", "FilePayload");
308-
customTypeNames.put("Frame.setInputFiles.files", "FilePayload");
309-
customTypeNames.put("Page.setInputFiles.files", "FilePayload");
310-
customTypeNames.put("Page.setInputFiles.files", "FilePayload");
311-
customTypeNames.put("FormData.append.value", "FilePayload");
312-
customTypeNames.put("FormData.set.value", "FilePayload");
313-
314-
customTypeNames.put("Locator.dragTo.options.sourcePosition", "Position");
315-
customTypeNames.put("Page.dragAndDrop.options.sourcePosition", "Position");
316-
customTypeNames.put("Frame.dragAndDrop.options.sourcePosition", "Position");
317-
customTypeNames.put("Locator.dragTo.options.targetPosition", "Position");
318-
customTypeNames.put("Page.dragAndDrop.options.targetPosition", "Position");
319-
customTypeNames.put("Frame.dragAndDrop.options.targetPosition", "Position");
286+
// Returns the Java-specific type alias declared in the api docs (e.g. `alias-java: Cookie`),
287+
// falling back to the language-agnostic `alias` if no Java-specific override is provided.
288+
private static String javaAlias(JsonObject jsonType) {
289+
if (!jsonType.has("langAliases")) {
290+
return null;
291+
}
292+
JsonObject langAliases = jsonType.getAsJsonObject("langAliases");
293+
if (langAliases.has("java")) {
294+
return langAliases.get("java").getAsString();
295+
}
296+
if (langAliases.has("default")) {
297+
return langAliases.get("default").getAsString();
298+
}
299+
return null;
320300
}
321301

322302
TypeRef(Element parent, JsonElement jsonElement) {
@@ -355,8 +335,9 @@ private void createClassesAndEnums(JsonObject jsonObject) {
355335
customType = toTitle(parent.parent.jsonName) + toTitle(parent.jsonName);
356336
typeScope().createNestedClass(customType, this, jsonObject);
357337
} else {
358-
if (customTypeNames.containsKey(jsonPath)) {
359-
customType = customTypeNames.get(jsonPath);
338+
String alias = javaAlias(jsonObject);
339+
if (alias != null) {
340+
customType = alias;
360341
} else {
361342
customType = toTitle(parent.jsonName);
362343
}
@@ -534,15 +515,12 @@ private String convertBuiltinType(JsonObject jsonType) {
534515
return convertTemplateParams(jsonType);
535516
}
536517
if ("function".equals(name)) {
518+
String alias = javaAlias(jsonType);
519+
if (alias != null) {
520+
return alias;
521+
}
537522
if (!jsonType.has("args")) {
538-
switch (jsonPath) {
539-
case "BrowserContext.exposeBinding.callback": return "BindingCallback";
540-
case "BrowserContext.exposeFunction.callback": return "FunctionCallback";
541-
case "Page.exposeBinding.callback": return "BindingCallback";
542-
case "Page.exposeFunction.callback": return "FunctionCallback";
543-
default:
544-
throw new RuntimeException("Missing mapping for " + jsonPath);
545-
}
523+
throw new RuntimeException("Missing mapping for " + jsonPath);
546524
}
547525
if ("WebSocketRoute.onClose.handler".equals(jsonPath)) {
548526
return "BiConsumer<Integer, String>";

0 commit comments

Comments
 (0)