diff --git a/config/generation.config.js b/config/generation.config.js index 045389f962..195adafe67 100644 --- a/config/generation.config.js +++ b/config/generation.config.js @@ -31,6 +31,7 @@ module.exports = { '!clients/algoliasearch-client-javascript/packages/requester-*/**', '!clients/algoliasearch-client-javascript/packages/client-common/**', + 'tests/output/javascript/package.json', 'tests/output/javascript/src/methods/**', 'tests/output/javascript/src/client/**', diff --git a/generators/src/main/java/com/algolia/codegen/Utils.java b/generators/src/main/java/com/algolia/codegen/Utils.java index 23cafb22ae..8e3c14c767 100644 --- a/generators/src/main/java/com/algolia/codegen/Utils.java +++ b/generators/src/main/java/com/algolia/codegen/Utils.java @@ -174,4 +174,15 @@ public static String getPackageVersion(String language) ); } } + + public static JsonNode readJsonFile(String filePath) { + JsonNode json = null; + try { + json = Json.mapper().readTree(new File(filePath)); + } catch (IOException e) { + e.printStackTrace(); + System.exit(1); + } + return json; + } } diff --git a/generators/src/main/java/com/algolia/codegen/cts/AlgoliaCtsGenerator.java b/generators/src/main/java/com/algolia/codegen/cts/AlgoliaCtsGenerator.java index dd82f75760..a4039848c9 100644 --- a/generators/src/main/java/com/algolia/codegen/cts/AlgoliaCtsGenerator.java +++ b/generators/src/main/java/com/algolia/codegen/cts/AlgoliaCtsGenerator.java @@ -1,7 +1,10 @@ package com.algolia.codegen.cts; import com.algolia.codegen.Utils; +import com.algolia.codegen.cts.manager.CtsManager; +import com.algolia.codegen.cts.manager.CtsManagerFactory; import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonNode; import com.google.common.collect.ImmutableMap.Builder; @@ -22,6 +25,7 @@ public class AlgoliaCtsGenerator extends DefaultCodegen { private String language; private String client; private String packageName; + private CtsManager ctsManager; /** * Configures the type of generator. @@ -63,31 +67,34 @@ public void processOpts() { language = (String) additionalProperties.get("language"); client = (String) additionalProperties.get("client"); packageName = (String) additionalProperties.get("packageName"); + ctsManager = CtsManagerFactory.getManager(language); + JsonNode config = Utils.readJsonFile("config/clients.config.json"); + TestConfig testConfig = null; try { - JsonNode config = Json - .mapper() - .readTree(new File("config/clients.config.json")); - TestConfig testConfig = Json - .mapper() - .treeToValue(config.get(language).get("tests"), TestConfig.class); - - setTemplateDir("tests/CTS/methods/requests/templates/" + language); - setOutputDir("tests/output/" + language); - String clientName = language.equals("php") - ? Utils.createClientName(client, language) - : client; - supportingFiles.add( - new SupportingFile( - "requests.mustache", - testConfig.outputFolder + "/methods/requests", - clientName + testConfig.extension - ) - ); - } catch (IOException e) { + testConfig = + Json + .mapper() + .treeToValue(config.get(language).get("tests"), TestConfig.class); + } catch (JsonProcessingException e) { e.printStackTrace(); System.exit(1); } + + setTemplateDir("tests/CTS/methods/requests/templates/" + language); + setOutputDir("tests/output/" + language); + String clientName = language.equals("php") + ? Utils.createClientName(client, language) + : client; + supportingFiles.add( + new SupportingFile( + "requests.mustache", + testConfig.outputFolder + "/methods/requests", + clientName + testConfig.extension + ) + ); + + ctsManager.addSupportingFiles(supportingFiles); } @Override @@ -147,6 +154,7 @@ public Map postProcessSupportingFileData( bundle.put("hasRegionalHost", hasRegionalHost); bundle.put("defaultRegion", client.equals("predict") ? "ew" : "us"); bundle.put("lambda", lambda); + ctsManager.addDataToBundle(bundle); List blocks = new ArrayList<>(); ParametersWithDataType paramsType = new ParametersWithDataType( diff --git a/generators/src/main/java/com/algolia/codegen/cts/manager/CtsManager.java b/generators/src/main/java/com/algolia/codegen/cts/manager/CtsManager.java new file mode 100644 index 0000000000..16ab7eeef1 --- /dev/null +++ b/generators/src/main/java/com/algolia/codegen/cts/manager/CtsManager.java @@ -0,0 +1,75 @@ +package com.algolia.codegen.cts.manager; + +import com.algolia.codegen.Utils; +import com.fasterxml.jackson.databind.JsonNode; +import java.util.*; +import org.openapitools.codegen.SupportingFile; + +public abstract class CtsManager { + + public abstract void addSupportingFiles(List supportingFiles); + + public List getPackageDependencies() { + return null; + } + + protected void addExtraToBundle(Map bundle) {} + + public void addDataToBundle(Map bundle) { + bundle.put("packageDependencies", this.getPackageDependencies()); + this.addExtraToBundle(bundle); + } + + protected Object[] getFilteredPackageVersions(List packages) { + HashMap result = new HashMap<>(); + + // Read config/openapitools.js for JavaScript + JsonNode openApiToolsConfig = Utils.readJsonFile( + "config/openapitools.json" + ); + Iterator generatorIterator = openApiToolsConfig + .get("generator-cli") + .get("generators") + .elements(); + while (generatorIterator.hasNext()) { + JsonNode generator = generatorIterator.next(); + JsonNode additionalProperties = generator.get("additionalProperties"); + if (!additionalProperties.has("packageVersion")) { + continue; + } + String packageName = additionalProperties.get("packageName").asText(); + String packageVersion = additionalProperties + .get("packageVersion") + .asText(); + if (packages.contains(packageName)) { + result.put(packageName, packageVersion); + } + } + + JsonNode clientsConfig = Utils.readJsonFile("config/clients.config.json"); + Iterator clientsIterator = clientsConfig.elements(); + while (clientsIterator.hasNext()) { + JsonNode client = clientsIterator.next(); + + if (!client.has("packageVersion")) { + continue; + } + String packageName = client.get("packageName").asText(); + String packageVersion = client.get("packageVersion").asText(); + if (packages.contains(packageName)) { + result.put(packageName, packageVersion); + } + } + + return result + .entrySet() + .stream() + .map(entry -> { + Map newEntry = new HashMap<>(); + newEntry.put("packageName", entry.getKey()); + newEntry.put("packageVersion", entry.getValue()); + return newEntry; + }) + .toArray(Object[]::new); + } +} diff --git a/generators/src/main/java/com/algolia/codegen/cts/manager/CtsManagerFactory.java b/generators/src/main/java/com/algolia/codegen/cts/manager/CtsManagerFactory.java new file mode 100644 index 0000000000..0e9c97ed76 --- /dev/null +++ b/generators/src/main/java/com/algolia/codegen/cts/manager/CtsManagerFactory.java @@ -0,0 +1,18 @@ +package com.algolia.codegen.cts.manager; + +import java.util.*; + +public class CtsManagerFactory { + + public static CtsManager getManager(String language) { + switch (language) { + case "javascript": + return new JavaScriptCtsManager(); + case "java": + return new JavaCtsManager(); + case "php": + return new PhpCtsManager(); + } + return null; + } +} diff --git a/generators/src/main/java/com/algolia/codegen/cts/manager/JavaCtsManager.java b/generators/src/main/java/com/algolia/codegen/cts/manager/JavaCtsManager.java new file mode 100644 index 0000000000..5ab7457afe --- /dev/null +++ b/generators/src/main/java/com/algolia/codegen/cts/manager/JavaCtsManager.java @@ -0,0 +1,9 @@ +package com.algolia.codegen.cts.manager; + +import java.util.*; +import org.openapitools.codegen.SupportingFile; + +public class JavaCtsManager extends CtsManager { + + public void addSupportingFiles(List supportingFiles) {} +} diff --git a/generators/src/main/java/com/algolia/codegen/cts/manager/JavaScriptCtsManager.java b/generators/src/main/java/com/algolia/codegen/cts/manager/JavaScriptCtsManager.java new file mode 100644 index 0000000000..38b2e27750 --- /dev/null +++ b/generators/src/main/java/com/algolia/codegen/cts/manager/JavaScriptCtsManager.java @@ -0,0 +1,66 @@ +package com.algolia.codegen.cts.manager; + +import com.algolia.codegen.Utils; +import com.fasterxml.jackson.databind.JsonNode; +import java.util.*; +import org.openapitools.codegen.SupportingFile; + +public class JavaScriptCtsManager extends CtsManager { + + public void addSupportingFiles(List supportingFiles) { + supportingFiles.add( + new SupportingFile("package.mustache", ".", "package.json") + ); + } + + public List getPackageDependencies() { + List result = new ArrayList(); + + JsonNode openApiToolsConfig = Utils.readJsonFile( + "config/openapitools.json" + ); + Iterator> fieldIterator = openApiToolsConfig + .get("generator-cli") + .get("generators") + .fields(); + + while (fieldIterator.hasNext()) { + Map.Entry field = fieldIterator.next(); + if (!field.getKey().startsWith("javascript-")) { + continue; + } + JsonNode generator = field.getValue(); + JsonNode additionalProperties = generator.get("additionalProperties"); + String packageName = additionalProperties.get("packageName").asText(); + String packageVersion = additionalProperties + .get("packageVersion") + .asText(); + + Map newEntry = new HashMap<>(); + newEntry.put("packageName", packageName); + newEntry.put("packageVersion", packageVersion); + result.add(newEntry); + } + return result; + } + + protected void addExtraToBundle(Map bundle) { + bundle.put("utilsPackageVersion", this.getUtilsPackageVersion()); + } + + private String getUtilsPackageVersion() { + JsonNode openApiToolsConfig = Utils.readJsonFile( + "config/openapitools.json" + ); + + String utilsPackageVersion = openApiToolsConfig + .get("generator-cli") + .get("generators") + .get("javascript-search") + .get("additionalProperties") + .get("utilsPackageVersion") + .asText(); + + return utilsPackageVersion; + } +} diff --git a/generators/src/main/java/com/algolia/codegen/cts/manager/PhpCtsManager.java b/generators/src/main/java/com/algolia/codegen/cts/manager/PhpCtsManager.java new file mode 100644 index 0000000000..d3c2565bb9 --- /dev/null +++ b/generators/src/main/java/com/algolia/codegen/cts/manager/PhpCtsManager.java @@ -0,0 +1,9 @@ +package com.algolia.codegen.cts.manager; + +import java.util.*; +import org.openapitools.codegen.SupportingFile; + +public class PhpCtsManager extends CtsManager { + + public void addSupportingFiles(List supportingFiles) {} +} diff --git a/scripts/cts/generate.ts b/scripts/cts/generate.ts index 5473af1337..2178f45e77 100644 --- a/scripts/cts/generate.ts +++ b/scripts/cts/generate.ts @@ -46,10 +46,6 @@ export async function ctsGenerateMany( if (!getTestOutputFolder(lang)) { continue; } - await formatter( - lang, - toAbsolutePath(`tests/output/${lang}/${getTestOutputFolder(lang)}`), - verbose - ); + await formatter(lang, toAbsolutePath(`tests/output/${lang}`), verbose); } } diff --git a/scripts/formatter.ts b/scripts/formatter.ts index c9f375d6c7..9ab22aa214 100644 --- a/scripts/formatter.ts +++ b/scripts/formatter.ts @@ -13,7 +13,7 @@ export async function formatter( let cmd = ''; switch (language) { case 'javascript': - cmd = `yarn eslint --ext=ts ${folder} --fix --no-error-on-unmatched-pattern`; + cmd = `yarn eslint --ext=ts,json ${folder} --fix --no-error-on-unmatched-pattern`; break; case 'java': cmd = `find ${folder} -type f -name "*.java" | xargs java --add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \ diff --git a/tests/CTS/methods/requests/templates/javascript/package.mustache b/tests/CTS/methods/requests/templates/javascript/package.mustache new file mode 100644 index 0000000000..bb4a76c5ca --- /dev/null +++ b/tests/CTS/methods/requests/templates/javascript/package.mustache @@ -0,0 +1,22 @@ +{ + "name": "javascript-tests", + "version": "1.0.0", + "scripts": { + "test": "jest" + }, + "dependencies": { + {{#packageDependencies}} + "{{{packageName}}}": "{{packageVersion}}", + {{/packageDependencies}} + "@experimental-api-clients-automation/client-common": "{{utilsPackageVersion}}", + "@experimental-api-clients-automation/requester-node-http": "{{utilsPackageVersion}}" + }, + "devDependencies": { + "@types/jest": "27.4.1", + "@types/node": "16.11.26", + "jest": "27.5.1", + "ts-jest": "27.1.4", + "ts-node": "10.7.0", + "typescript": "4.6.3" + } +}