From 08d84cf6296f2fc349eac61e8d7d79c0b877823e Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Fri, 19 Feb 2021 18:54:06 -0800 Subject: [PATCH] chore: harden custom type mapping (#299) --- .../playwright/tools/ApiGenerator.java | 67 +++++-------------- 1 file changed, 17 insertions(+), 50 deletions(-) diff --git a/tools/api-generator/src/main/java/com/microsoft/playwright/tools/ApiGenerator.java b/tools/api-generator/src/main/java/com/microsoft/playwright/tools/ApiGenerator.java index 61fcb995..6bdc4b5a 100644 --- a/tools/api-generator/src/main/java/com/microsoft/playwright/tools/ApiGenerator.java +++ b/tools/api-generator/src/main/java/com/microsoft/playwright/tools/ApiGenerator.java @@ -111,9 +111,17 @@ class TypeRef extends Element { private static final Map customTypeNames = new HashMap<>(); static { - customTypeNames.put("cookies", "Cookie"); - customTypeNames.put("files", "FilePayload"); - customTypeNames.put("values", "SelectOption"); + customTypeNames.put("BrowserContext.addCookies.cookies", "Cookie"); + customTypeNames.put("BrowserContext.cookies", "Cookie"); + + customTypeNames.put("ElementHandle.selectOption.values", "SelectOption"); + customTypeNames.put("Frame.selectOption.values", "SelectOption"); + customTypeNames.put("Page.selectOption.values", "SelectOption"); + + customTypeNames.put("ElementHandle.setInputFiles.files", "FilePayload"); + customTypeNames.put("FileChooser.setFiles.files", "FilePayload"); + customTypeNames.put("Frame.setInputFiles.files", "FilePayload"); + customTypeNames.put("Page.setInputFiles.files", "FilePayload"); } TypeRef(Element parent, JsonElement jsonElement) { @@ -122,43 +130,6 @@ class TypeRef extends Element { createClassesAndEnums(jsonElement.getAsJsonObject()); } - private static String typeExpression(JsonObject jsonType) { - String typeName = jsonType.get("name").getAsString(); - if (jsonType.has("union")) { - List values = new ArrayList<>(); - for (JsonElement item : jsonType.getAsJsonArray("union")) { - values.add(typeExpression(item.getAsJsonObject())); - } - values.sort(String::compareTo); - String enumValues = String.join("|", values); - return typeName.isEmpty() ? enumValues : typeName + "<" + enumValues + ">"; - } - if ("function".equals(typeName)) { - if (!jsonType.has("args")) { - return typeName; - } - List args = new ArrayList<>(); - for (JsonElement item : jsonType.getAsJsonArray("args")) { - args.add(typeExpression(item.getAsJsonObject())); - } - String returnType = ""; - if (jsonType.has("returnType") && jsonType.get("returnType").isJsonObject()) { - returnType = ":" + typeExpression(jsonType.getAsJsonObject("returnType")); - } - return typeName + "(" + String.join(", ", args) + ")" + returnType; - } - List templateArgs = new ArrayList<>(); - if (jsonType.has("templates")) { - for (JsonElement item : jsonType.getAsJsonArray("templates")) { - templateArgs.add(typeExpression(item.getAsJsonObject())); - } - } - if (templateArgs.isEmpty()) { - return typeName; - } - return typeName + "<" + String.join(", ", templateArgs) + ">"; - } - private void createClassesAndEnums(JsonObject jsonObject) { if (jsonObject.has("union")) { if (jsonObject.get("name").getAsString().isEmpty()) { @@ -185,16 +156,16 @@ class TypeRef extends Element { // Same type maybe referenced as 'Object' in several union values, e.g. Object|Array return; } - if (parent instanceof Method || parent instanceof Field || (parent instanceof Param && !"options".equals(parent.jsonName))) { - if (customTypeNames.containsKey(parent.jsonName)) { - customType = customTypeNames.get(parent.jsonName); + if (parent instanceof Param && "options".equals(parent.jsonName)) { + customType = toTitle(parent.parent.jsonName) + toTitle(parent.jsonName); + typeScope().createNestedClass(customType, this, jsonObject); + } else { + if (customTypeNames.containsKey(jsonPath)) { + customType = customTypeNames.get(jsonPath); } else { customType = toTitle(parent.jsonName); } typeScope().createTopLevelClass(customType, this, jsonObject); - } else { - customType = toTitle(parent.parent.jsonName) + toTitle(parent.jsonName); - typeScope().createNestedClass(customType, this, jsonElement.getAsJsonObject()); } } } @@ -356,10 +327,6 @@ class TypeRef extends Element { if (customType != null) { return customType; } - String expression = typeExpression(jsonType); - if (!"Object".equals(expression) && !"Object".equals(expression)) { - throw new RuntimeException("Unexpected object type: " + typeExpression(jsonType)); - } return "Map<" + convertTemplateParams(jsonType) + ">"; } if ("Map".equals(name)) {