From 36af3cb60244465ec42148cc03066502ca87d41f Mon Sep 17 00:00:00 2001 From: Pierre Millot Date: Wed, 6 Apr 2022 11:14:04 +0200 Subject: [PATCH 1/7] style(generator): format on the CI --- .github/actions/setup/action.yml | 4 ++++ .github/workflows/check.yml | 25 +++++++++++++++++++++---- scripts/index.ts | 11 +++++++++++ 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/.github/actions/setup/action.yml b/.github/actions/setup/action.yml index 151af1480a..17b13a9bc2 100644 --- a/.github/actions/setup/action.yml +++ b/.github/actions/setup/action.yml @@ -176,6 +176,10 @@ outputs: description: Determine if the `scripts` job should run value: ${{ steps.diff.outputs.GITHUB_ACTIONS_CHANGED > 0 || steps.diff.outputs.SCRIPTS_CHANGED > 0 }} + RUN_GENERATORS: + description: Determine if the `generators` job should run + value: ${{ steps.diff.outputs.GENERATORS_CHANGED > 0 }} + SPECS_MATRIX: description: The generated `specs` matrix value: ${{ steps.spec-matrix.outputs.MATRIX }} diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 5fe0f1875e..403d1f99c8 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -29,6 +29,7 @@ jobs: outputs: RUN_SCRIPTS: ${{ steps.setup.outputs.RUN_SCRIPTS }} + RUN_GENERATORS: ${{ steps.setup.outputs.RUN_GENERATORS }} RUN_SPECS: ${{ steps.setup.outputs.RUN_SPECS }} SPECS_MATRIX: ${{ steps.setup.outputs.SPECS_MATRIX }} @@ -51,15 +52,12 @@ jobs: scripts: runs-on: ubuntu-20.04 - timeout-minutes: 20 + timeout-minutes: 10 needs: setup if: ${{ needs.setup.outputs.RUN_SCRIPTS == 'true' }} steps: - uses: actions/checkout@v2 - - name: Restore cache - uses: ./.github/actions/cache - - name: Check script linting run: yarn scripts:lint @@ -69,6 +67,25 @@ jobs: - name: Test custom eslint plugin run: yarn workspace eslint-plugin-automation-custom test + generators: + runs-on: ubuntu-20.04 + timeout-minutes: 5 + needs: setup + if: ${{ needs.setup.outputs.RUN_GENERATORS == 'true' }} + steps: + - uses: actions/checkout@v2 + + - name: Check generators linting + run: | + yarn cli format java generators + + - name: Check diff with pushed generators + run: | + diff=$(git status --porcelain ./generators | wc -l) + if [[ $diff > 0 ]]; then + echo "Format the generators folder by running 'yarn docker format java generators'" + exit $diff + specs: runs-on: ubuntu-20.04 timeout-minutes: 10 diff --git a/scripts/index.ts b/scripts/index.ts index d63daa021f..abdf7e5884 100644 --- a/scripts/index.ts +++ b/scripts/index.ts @@ -16,6 +16,7 @@ import { } from './common'; import { ctsGenerateMany } from './cts/generate'; import { runCts } from './cts/runCts'; +import { formatter } from './formatter'; import { generate } from './generate'; import { playground } from './playground'; import type { Generator } from './types'; @@ -290,4 +291,14 @@ program } ); +program + .command('format') + .description('Format the specified folder for a specific language') + .addArgument(new Argument('language', 'The language').choices(LANGUAGES)) + .argument('folder', 'The folder to format') + .option('-v, --verbose', 'make the formatting verbose') + .action(async (language: string, folder: string, { verbose }) => { + await formatter(language, folder, verbose); + }); + program.parse(); From 4363aa1ff8da1f213fdf20b223e5f6d57481e51c Mon Sep 17 00:00:00 2001 From: Pierre Millot Date: Wed, 6 Apr 2022 11:19:15 +0200 Subject: [PATCH 2/7] test on ci --- .github/workflows/check.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 403d1f99c8..fcedbf56a1 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -71,7 +71,6 @@ jobs: runs-on: ubuntu-20.04 timeout-minutes: 5 needs: setup - if: ${{ needs.setup.outputs.RUN_GENERATORS == 'true' }} steps: - uses: actions/checkout@v2 From 4d8b62b756ae65d466aff56da5ec00b78c01a9ba Mon Sep 17 00:00:00 2001 From: Pierre Millot Date: Wed, 6 Apr 2022 11:31:53 +0200 Subject: [PATCH 3/7] need cache --- .github/workflows/check.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index fcedbf56a1..e4d0ab7ae1 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -58,6 +58,9 @@ jobs: steps: - uses: actions/checkout@v2 + - name: Restore cache + uses: ./.github/actions/cache + - name: Check script linting run: yarn scripts:lint @@ -74,6 +77,9 @@ jobs: steps: - uses: actions/checkout@v2 + - name: Restore cache + uses: ./.github/actions/cache + - name: Check generators linting run: | yarn cli format java generators From c827974ca3c007548a4d9821c709d5f3caecd632 Mon Sep 17 00:00:00 2001 From: Pierre Millot Date: Wed, 6 Apr 2022 11:40:18 +0200 Subject: [PATCH 4/7] cache need java --- .github/workflows/check.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index e4d0ab7ae1..7248bfc743 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -79,6 +79,8 @@ jobs: - name: Restore cache uses: ./.github/actions/cache + with: + language: java - name: Check generators linting run: | From 817f36259f1659b26bcca75ed3fcdbce881616a3 Mon Sep 17 00:00:00 2001 From: Pierre Millot Date: Wed, 6 Apr 2022 11:48:15 +0200 Subject: [PATCH 5/7] oops --- .github/workflows/check.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 7248bfc743..f9b23e3148 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -91,6 +91,7 @@ jobs: diff=$(git status --porcelain ./generators | wc -l) if [[ $diff > 0 ]]; then echo "Format the generators folder by running 'yarn docker format java generators'" + fi exit $diff specs: From 768373d38867d1e302d3e26e35e72a70b7e13efa Mon Sep 17 00:00:00 2001 From: Pierre Millot Date: Wed, 6 Apr 2022 12:54:40 +0200 Subject: [PATCH 6/7] remove job --- .github/actions/setup/action.yml | 7 +++---- .github/workflows/check.yml | 34 +++++++++----------------------- 2 files changed, 12 insertions(+), 29 deletions(-) diff --git a/.github/actions/setup/action.yml b/.github/actions/setup/action.yml index 17b13a9bc2..e176501322 100644 --- a/.github/actions/setup/action.yml +++ b/.github/actions/setup/action.yml @@ -34,6 +34,9 @@ runs: - name: Restore cache uses: ./.github/actions/cache + with: + # We want to load the java formatter + language: java - name: Setting diff outputs variables if: inputs.type != 'minimal' @@ -176,10 +179,6 @@ outputs: description: Determine if the `scripts` job should run value: ${{ steps.diff.outputs.GITHUB_ACTIONS_CHANGED > 0 || steps.diff.outputs.SCRIPTS_CHANGED > 0 }} - RUN_GENERATORS: - description: Determine if the `generators` job should run - value: ${{ steps.diff.outputs.GENERATORS_CHANGED > 0 }} - SPECS_MATRIX: description: The generated `specs` matrix value: ${{ steps.spec-matrix.outputs.MATRIX }} diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index f9b23e3148..97575a7717 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -27,9 +27,17 @@ jobs: - name: Lint GitHub actions run: yarn github-actions:lint + - name: Lint generators + run: | + yarn cli format java generators + diff=$(git status --porcelain ./generators | wc -l) + if [[ $diff > 0 ]]; then + echo "Format the generators folder by running 'yarn docker format java generators'" + fi + exit $diff + outputs: RUN_SCRIPTS: ${{ steps.setup.outputs.RUN_SCRIPTS }} - RUN_GENERATORS: ${{ steps.setup.outputs.RUN_GENERATORS }} RUN_SPECS: ${{ steps.setup.outputs.RUN_SPECS }} SPECS_MATRIX: ${{ steps.setup.outputs.SPECS_MATRIX }} @@ -70,30 +78,6 @@ jobs: - name: Test custom eslint plugin run: yarn workspace eslint-plugin-automation-custom test - generators: - runs-on: ubuntu-20.04 - timeout-minutes: 5 - needs: setup - steps: - - uses: actions/checkout@v2 - - - name: Restore cache - uses: ./.github/actions/cache - with: - language: java - - - name: Check generators linting - run: | - yarn cli format java generators - - - name: Check diff with pushed generators - run: | - diff=$(git status --porcelain ./generators | wc -l) - if [[ $diff > 0 ]]; then - echo "Format the generators folder by running 'yarn docker format java generators'" - fi - exit $diff - specs: runs-on: ubuntu-20.04 timeout-minutes: 10 From 5b958399586f99ed33b1a2f15fdde871cf54dff4 Mon Sep 17 00:00:00 2001 From: Pierre Millot Date: Wed, 6 Apr 2022 13:00:04 +0200 Subject: [PATCH 7/7] good format --- .../algolia/codegen/AlgoliaJavaGenerator.java | 122 ++++++---- .../algolia/codegen/AlgoliaPhpGenerator.java | 18 +- .../algolia/codegen/GenerationException.java | 7 +- .../main/java/com/algolia/codegen/Utils.java | 16 +- .../codegen/cts/AlgoliaCtsGenerator.java | 92 +++++--- .../com/algolia/codegen/cts/CTSException.java | 1 + .../codegen/cts/EscapeQuotesLambda.java | 9 +- .../codegen/cts/ParametersWithDataType.java | 210 +++++++++++++----- .../java/com/algolia/codegen/cts/Request.java | 10 +- .../com/algolia/codegen/cts/TestConfig.java | 1 + 10 files changed, 330 insertions(+), 156 deletions(-) diff --git a/generators/src/main/java/com/algolia/codegen/AlgoliaJavaGenerator.java b/generators/src/main/java/com/algolia/codegen/AlgoliaJavaGenerator.java index ff701d74b0..05c7c247b2 100644 --- a/generators/src/main/java/com/algolia/codegen/AlgoliaJavaGenerator.java +++ b/generators/src/main/java/com/algolia/codegen/AlgoliaJavaGenerator.java @@ -1,24 +1,22 @@ package com.algolia.codegen; +import io.swagger.v3.oas.models.Operation; +import io.swagger.v3.oas.models.media.Schema; +import io.swagger.v3.oas.models.servers.Server; +import java.io.FileInputStream; +import java.net.URL; +import java.util.*; import org.openapitools.codegen.*; import org.openapitools.codegen.languages.JavaClientCodegen; import org.openapitools.codegen.utils.ModelUtils; import org.yaml.snakeyaml.Yaml; -import java.util.*; -import java.io.FileInputStream; -import java.net.URL; - -import io.swagger.v3.oas.models.Operation; -import io.swagger.v3.oas.models.media.Schema; -import io.swagger.v3.oas.models.servers.Server; - @SuppressWarnings("unchecked") public class AlgoliaJavaGenerator extends JavaClientCodegen { + /** - * Configures a friendly name for the generator. This will be used by the - * generator - * to select the library with the -g flag. + * Configures a friendly name for the generator. This will be used by the generator to select the + * library with the -g flag. * * @return the friendly name for the generator */ @@ -27,15 +25,17 @@ public String getName() { return "algolia-java"; } - /** - * Inject server info into the client to generate the right URL - */ + /** Inject server info into the client to generate the right URL */ private void generateServer(Map client) { String clientName = (String) client.get("pathPrefix"); Yaml yaml = new Yaml(); try { - Map spec = yaml.load(new FileInputStream("specs/" + clientName + "/spec.yml")); - List> servers = (List>) spec.get("servers"); + Map spec = yaml.load( + new FileInputStream("specs/" + clientName + "/spec.yml") + ); + List> servers = (List>) spec.get( + "servers" + ); boolean hasRegionalHost = false; boolean fallbackToAliasHost = false; @@ -47,19 +47,28 @@ private void generateServer(Map client) { for (Map server : servers) { if (!server.containsKey("url")) { - throw new GenerationException("Invalid server, does not contains 'url'"); + throw new GenerationException( + "Invalid server, does not contains 'url'" + ); } if (!server.containsKey("variables")) { continue; } - Map> variables = (Map>) server.get("variables"); + Map> variables = (Map>) server.get( + "variables" + ); - if (!variables.containsKey("region") || !variables.get("region").containsKey("enum")) { + if ( + !variables.containsKey("region") || + !variables.get("region").containsKey("enum") + ) { continue; } - ArrayList enums = (ArrayList) variables.get("region").get("enum"); + ArrayList enums = (ArrayList) variables + .get("region") + .get("enum"); hasRegionalHost = true; URL url = new URL((String) server.get("url")); @@ -95,18 +104,30 @@ private void generateServer(Map client) { } @Override - public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, List servers) { - return Utils.specifyCustomRequest(super.fromOperation(path, httpMethod, operation, servers)); + public CodegenOperation fromOperation( + String path, + String httpMethod, + Operation operation, + List servers + ) { + return Utils.specifyCustomRequest( + super.fromOperation(path, httpMethod, operation, servers) + ); } - /** - * Provides an opportunity to inspect and modify operation data before the code - * is generated. - */ + /** Provides an opportunity to inspect and modify operation data before the code is generated. */ @Override - public Map postProcessOperationsWithModels(Map objs, List allModels) { - Map results = super.postProcessOperationsWithModels(objs, allModels); - Map client = (Map) results.get("operations"); + public Map postProcessOperationsWithModels( + Map objs, + List allModels + ) { + Map results = super.postProcessOperationsWithModels( + objs, + allModels + ); + Map client = (Map) results.get( + "operations" + ); generateServer(client); @@ -118,7 +139,11 @@ public Map postProcessAllModels(Map objs) { Map models = super.postProcessAllModels(objs); for (Object modelContainer : models.values()) { - CodegenModel model = ((Map>>) modelContainer).get("models").get(0) + CodegenModel model = + ((Map>>) modelContainer).get( + "models" + ) + .get(0) .get("model"); if (!model.oneOf.isEmpty()) { List> listOneOf = new ArrayList(); @@ -127,7 +152,10 @@ public Map postProcessAllModels(Map objs) { HashMap hashMapOneOf = new HashMap(); hashMapOneOf.put("type", iterateModel); - hashMapOneOf.put("name", iterateModel.replace("<", "").replace(">", "")); + hashMapOneOf.put( + "name", + iterateModel.replace("<", "").replace(">", "") + ); listOneOf.add(hashMapOneOf); } @@ -141,23 +169,33 @@ public Map postProcessAllModels(Map objs) { } @Override - public Map postProcessSupportingFileData(Map objs) { + public Map postProcessSupportingFileData( + Map objs + ) { Map bundle = super.postProcessSupportingFileData(objs); - List> apis = ((Map>>) bundle.get("apiInfo")).get("apis"); + List> apis = + ((Map>>) bundle.get("apiInfo")).get( + "apis" + ); for (Map api : apis) { - List operations = ((Map>) api.get("operations")) - .get("operation"); + List operations = + ((Map>) api.get("operations")).get( + "operation" + ); for (CodegenOperation ope : operations) { - ope.returnType = ope.returnType.replace("Map<", "HashMap<").replace("List<", "ArrayList<"); + ope.returnType = + ope.returnType + .replace("Map<", "HashMap<") + .replace("List<", "ArrayList<"); } } return bundle; } /** - * Returns human-friendly help for the generator. Provide the consumer with help - * tips, parameters here + * Returns human-friendly help for the generator. Provide the consumer with help tips, parameters + * here * * @return A string value for the help message */ @@ -170,9 +208,13 @@ public String getHelp() { public void processOpts() { super.processOpts(); - supportingFiles.add(new SupportingFile("EchoResponse.mustache", + supportingFiles.add( + new SupportingFile( + "EchoResponse.mustache", "algoliasearch-core/com/algolia/utils/echo", - "EchoResponse.java")); + "EchoResponse.java" + ) + ); // Prevent all useless file to generate apiTestTemplateFiles.clear(); diff --git a/generators/src/main/java/com/algolia/codegen/AlgoliaPhpGenerator.java b/generators/src/main/java/com/algolia/codegen/AlgoliaPhpGenerator.java index 4d7700e0d6..e1a2fb3096 100644 --- a/generators/src/main/java/com/algolia/codegen/AlgoliaPhpGenerator.java +++ b/generators/src/main/java/com/algolia/codegen/AlgoliaPhpGenerator.java @@ -1,21 +1,27 @@ package com.algolia.codegen; +import io.swagger.v3.oas.models.Operation; +import io.swagger.v3.oas.models.servers.Server; import java.util.List; - import org.openapitools.codegen.CodegenOperation; import org.openapitools.codegen.languages.PhpClientCodegen; -import io.swagger.v3.oas.models.Operation; -import io.swagger.v3.oas.models.servers.Server; - public class AlgoliaPhpGenerator extends PhpClientCodegen { + @Override public String getName() { return "algolia-php"; } @Override - public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, List servers) { - return Utils.specifyCustomRequest(super.fromOperation(path, httpMethod, operation, servers)); + public CodegenOperation fromOperation( + String path, + String httpMethod, + Operation operation, + List servers + ) { + return Utils.specifyCustomRequest( + super.fromOperation(path, httpMethod, operation, servers) + ); } } diff --git a/generators/src/main/java/com/algolia/codegen/GenerationException.java b/generators/src/main/java/com/algolia/codegen/GenerationException.java index 52d34b335c..c33e5e2ad8 100644 --- a/generators/src/main/java/com/algolia/codegen/GenerationException.java +++ b/generators/src/main/java/com/algolia/codegen/GenerationException.java @@ -1,7 +1,8 @@ package com.algolia.codegen; public class GenerationException extends Exception { - public GenerationException(String message) { - super(message); - } + + public GenerationException(String message) { + super(message); + } } diff --git a/generators/src/main/java/com/algolia/codegen/Utils.java b/generators/src/main/java/com/algolia/codegen/Utils.java index 829c5338fc..1639d0f49c 100644 --- a/generators/src/main/java/com/algolia/codegen/Utils.java +++ b/generators/src/main/java/com/algolia/codegen/Utils.java @@ -1,21 +1,25 @@ package com.algolia.codegen; -import java.util.Set; - import com.google.common.collect.Sets; - +import java.util.Set; import org.openapitools.codegen.CodegenOperation; public class Utils { - public static final Set CUSTOM_METHOD = Sets.newHashSet("del", "get", "post", "put"); + + public static final Set CUSTOM_METHOD = Sets.newHashSet( + "del", + "get", + "post", + "put" + ); public static String capitalize(String str) { return str.substring(0, 1).toUpperCase() + str.substring(1); } /** - * Will add the boolean `vendorExtensions.x-is-custom-request` to operations if - * they should not escape '/' in the path variable + * Will add the boolean `vendorExtensions.x-is-custom-request` to operations if they should not + * escape '/' in the path variable */ public static CodegenOperation specifyCustomRequest(CodegenOperation ope) { if (CUSTOM_METHOD.contains(ope.nickname)) { 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 f7ebeb486a..b4e88bfc27 100644 --- a/generators/src/main/java/com/algolia/codegen/cts/AlgoliaCtsGenerator.java +++ b/generators/src/main/java/com/algolia/codegen/cts/AlgoliaCtsGenerator.java @@ -1,24 +1,21 @@ package com.algolia.codegen.cts; -import org.openapitools.codegen.*; - -import java.util.*; -import java.util.Map.Entry; - import com.algolia.codegen.Utils; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonNode; import com.google.common.collect.ImmutableMap.Builder; import com.samskivert.mustache.Mustache.Lambda; - +import io.swagger.v3.core.util.Json; import java.io.File; import java.io.IOException; - -import io.swagger.v3.core.util.Json; +import java.util.*; +import java.util.Map.Entry; +import org.openapitools.codegen.*; @SuppressWarnings("unchecked") public class AlgoliaCtsGenerator extends DefaultCodegen { + // cache the models private final Map models = new HashMap<>(); private String language; @@ -38,9 +35,8 @@ public CodegenType getTag() { } /** - * Configures a friendly name for the generator. This will be used by the - * generator - * to select the library with the -g flag. + * Configures a friendly name for the generator. This will be used by the generator to select the + * library with the -g flag. * * @return the friendly name for the generator */ @@ -50,8 +46,8 @@ public String getName() { } /** - * Returns human-friendly help for the generator. Provide the consumer with help - * tips, parameters here + * Returns human-friendly help for the generator. Provide the consumer with help tips, parameters + * here * * @return A string value for the help message */ @@ -67,17 +63,26 @@ public void processOpts() { language = (String) additionalProperties.get("language"); client = (String) additionalProperties.get("client"); packageName = (String) additionalProperties.get("packageName"); - hasRegionalHost = additionalProperties.get("hasRegionalHost").equals("true"); + hasRegionalHost = + additionalProperties.get("hasRegionalHost").equals("true"); try { - JsonNode config = Json.mapper().readTree(new File("config/clients.config.json")); - TestConfig testConfig = Json.mapper().treeToValue(config.get(language).get("tests"), TestConfig.class); + 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); - supportingFiles - .add(new SupportingFile("requests.mustache", testConfig.outputFolder + "/methods/requests", - client + testConfig.extension)); + supportingFiles.add( + new SupportingFile( + "requests.mustache", + testConfig.outputFolder + "/methods/requests", + client + testConfig.extension + ) + ); } catch (IOException e) { e.printStackTrace(); System.exit(1); @@ -88,9 +93,13 @@ public void processOpts() { public Map postProcessAllModels(Map objs) { Map mod = super.postProcessAllModels(objs); for (Entry entry : mod.entrySet()) { - List innerModel = ((Map>) entry.getValue()).get("models"); + List innerModel = + ((Map>) entry.getValue()).get("models"); if (!innerModel.isEmpty()) { - models.put(entry.getKey(), (CodegenModel) ((Map) innerModel.get(0)).get("model")); + models.put( + entry.getKey(), + (CodegenModel) ((Map) innerModel.get(0)).get("model") + ); } } return mod; @@ -105,7 +114,9 @@ protected Builder addMustacheLambdas() { } @Override - public Map postProcessSupportingFileData(Map objs) { + public Map postProcessSupportingFileData( + Map objs + ) { Map cts = null; try { cts = loadCTS(); @@ -131,13 +142,19 @@ public Map postProcessSupportingFileData(Map obj for (Entry entry : cts.entrySet()) { String operationId = entry.getKey(); if (!operations.containsKey(operationId)) { - throw new CTSException("operationId " + operationId + " does not exist in the spec"); + throw new CTSException( + "operationId " + operationId + " does not exist in the spec" + ); } CodegenOperation op = operations.get(operationId); List tests = new ArrayList<>(); for (int i = 0; i < entry.getValue().length; i++) { - Map test = paramsType.buildJSONForRequest(entry.getValue()[i], op, i); + Map test = paramsType.buildJSONForRequest( + entry.getValue()[i], + op, + i + ); tests.add(test); } Map testObj = new HashMap<>(); @@ -160,29 +177,40 @@ public Map postProcessSupportingFileData(Map obj return null; } - private Map loadCTS() throws JsonParseException, JsonMappingException, IOException, CTSException { + private Map loadCTS() + throws JsonParseException, JsonMappingException, IOException, CTSException { TreeMap cts = new TreeMap<>(); File dir = new File("tests/CTS/methods/requests/" + client); if (!dir.exists()) { throw new CTSException("CTS not found at " + dir.getAbsolutePath(), true); } for (File f : dir.listFiles()) { - cts.put(f.getName().replace(".json", ""), Json.mapper().readValue(f, Request[].class)); + cts.put( + f.getName().replace(".json", ""), + Json.mapper().readValue(f, Request[].class) + ); } return cts; } // operationId -> CodegenOperation - private HashMap buildOperations(Map objs) { + private HashMap buildOperations( + Map objs + ) { HashMap result = new HashMap<>(); - List> apis = ((Map>>) objs.get("apiInfo")).get("apis"); + List> apis = + ((Map>>) objs.get("apiInfo")).get( + "apis" + ); for (Map api : apis) { String apiName = ((String) api.get("baseName")).toLowerCase(); if (!apiName.equals(client.replace("-", ""))) { continue; } - List operations = ((Map>) api.get("operations")) - .get("operation"); + List operations = + ((Map>) api.get("operations")).get( + "operation" + ); for (CodegenOperation ope : operations) { result.put(ope.operationId, ope); } @@ -209,8 +237,8 @@ private String createClientName() { } /** - * override with any special text escaping logic to handle unsafe - * characters so as to avoid code injection + * override with any special text escaping logic to handle unsafe characters so as to avoid code + * injection * * @param input String to be cleaned up * @return string with unsafe characters removed or escaped diff --git a/generators/src/main/java/com/algolia/codegen/cts/CTSException.java b/generators/src/main/java/com/algolia/codegen/cts/CTSException.java index 8a965af88c..57b6b4f422 100644 --- a/generators/src/main/java/com/algolia/codegen/cts/CTSException.java +++ b/generators/src/main/java/com/algolia/codegen/cts/CTSException.java @@ -1,6 +1,7 @@ package com.algolia.codegen.cts; public class CTSException extends Exception { + private boolean skipable; public CTSException(String message) { diff --git a/generators/src/main/java/com/algolia/codegen/cts/EscapeQuotesLambda.java b/generators/src/main/java/com/algolia/codegen/cts/EscapeQuotesLambda.java index 57ab52c6a8..525ad93e5d 100644 --- a/generators/src/main/java/com/algolia/codegen/cts/EscapeQuotesLambda.java +++ b/generators/src/main/java/com/algolia/codegen/cts/EscapeQuotesLambda.java @@ -1,14 +1,15 @@ package com.algolia.codegen.cts; -import java.io.IOException; -import java.io.Writer; - import com.samskivert.mustache.Mustache; import com.samskivert.mustache.Template; +import java.io.IOException; +import java.io.Writer; public class EscapeQuotesLambda implements Mustache.Lambda { + @Override - public void execute(Template.Fragment fragment, Writer writer) throws IOException { + public void execute(Template.Fragment fragment, Writer writer) + throws IOException { String text = fragment.execute(); writer.write(text.replace("\"", "\\\"")); } diff --git a/generators/src/main/java/com/algolia/codegen/cts/ParametersWithDataType.java b/generators/src/main/java/com/algolia/codegen/cts/ParametersWithDataType.java index bc5b42d8b4..ae4ade5780 100644 --- a/generators/src/main/java/com/algolia/codegen/cts/ParametersWithDataType.java +++ b/generators/src/main/java/com/algolia/codegen/cts/ParametersWithDataType.java @@ -1,12 +1,11 @@ package com.algolia.codegen.cts; -import java.util.*; -import java.util.Map.Entry; - import com.algolia.codegen.Utils; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonMappingException; - +import io.swagger.util.Json; +import java.util.*; +import java.util.Map.Entry; import org.openapitools.codegen.CodegenModel; import org.openapitools.codegen.CodegenOperation; import org.openapitools.codegen.CodegenParameter; @@ -14,18 +13,20 @@ import org.openapitools.codegen.CodegenResponse; import org.openapitools.codegen.IJsonSchemaValidationProperties; -import io.swagger.util.Json; - @SuppressWarnings("unchecked") public class ParametersWithDataType { + private final Map models; public ParametersWithDataType(Map models) { this.models = models; } - public Map buildJSONForRequest(Request req, CodegenOperation ope, int testIndex) - throws CTSException, JsonMappingException, JsonProcessingException { + public Map buildJSONForRequest( + Request req, + CodegenOperation ope, + int testIndex + ) throws CTSException, JsonMappingException, JsonProcessingException { Map test = new HashMap<>(); test.put("method", req.method); test.put("testName", req.testName == null ? req.method : req.testName); @@ -43,8 +44,20 @@ public Map buildJSONForRequest(Request req, CodegenOperation ope List parametersWithDataType = new ArrayList<>(); // special case if there is only bodyParam which is not an array - if (ope.allParams.size() == 1 && ope.bodyParams.size() == 1 && !ope.bodyParam.isArray) { - parametersWithDataType.add(traverseParams(ope.bodyParam.paramName, req.parameters, ope.bodyParam, "", 0)); + if ( + ope.allParams.size() == 1 && + ope.bodyParams.size() == 1 && + !ope.bodyParam.isArray + ) { + parametersWithDataType.add( + traverseParams( + ope.bodyParam.paramName, + req.parameters, + ope.bodyParam, + "", + 0 + ) + ); } else { for (Entry param : req.parameters.entrySet()) { CodegenParameter specParam = null; @@ -55,9 +68,13 @@ public Map buildJSONForRequest(Request req, CodegenOperation ope } } if (specParam == null) { - throw new CTSException("Parameter " + param.getKey() + " not found in the root parameter"); + throw new CTSException( + "Parameter " + param.getKey() + " not found in the root parameter" + ); } - parametersWithDataType.add(traverseParams(param.getKey(), param.getValue(), specParam, "", 0)); + parametersWithDataType.add( + traverseParams(param.getKey(), param.getValue(), specParam, "", 0) + ); } } @@ -65,12 +82,18 @@ public Map buildJSONForRequest(Request req, CodegenOperation ope return test; } - private Map traverseParams(String paramName, Object param, IJsonSchemaValidationProperties spec, - String parent, int suffix) - throws CTSException { + private Map traverseParams( + String paramName, + Object param, + IJsonSchemaValidationProperties spec, + String parent, + int suffix + ) throws CTSException { String baseType = getTypeName(spec); if (baseType == null) { - throw new CTSException("Cannot determine type of " + paramName + " (value: " + param + ")"); + throw new CTSException( + "Cannot determine type of " + paramName + " (value: " + param + ")" + ); } boolean isCodegenModel = spec instanceof CodegenModel; @@ -78,7 +101,8 @@ private Map traverseParams(String paramName, Object param, IJson if (!isCodegenModel) { // don't overwrite it if it's already a model // sometimes it's in lowercase for some reason - String lowerBaseType = baseType.substring(0, 1).toLowerCase() + baseType.substring(1); + String lowerBaseType = + baseType.substring(0, 1).toLowerCase() + baseType.substring(1); if (models.containsKey(baseType)) { // get the real model if possible spec = models.get(baseType); @@ -136,13 +160,26 @@ private Map createDefaultOutput() { return testOutput; } - private void handleArray(String paramName, Object param, Map testOutput, - IJsonSchemaValidationProperties spec, int suffix) throws CTSException { + private void handleArray( + String paramName, + Object param, + Map testOutput, + IJsonSchemaValidationProperties spec, + int suffix + ) throws CTSException { List items = (List) param; List values = new ArrayList<>(); for (int i = 0; i < items.size(); i++) { - values.add(traverseParams(paramName + "_" + i, items.get(i), spec.getItems(), paramName, suffix + 1)); + values.add( + traverseParams( + paramName + "_" + i, + items.get(i), + spec.getItems(), + paramName, + suffix + 1 + ) + ); } testOutput.put("isArray", true); @@ -154,22 +191,36 @@ private void handleEnum(Object param, Map testOutput) { testOutput.put("value", param); } - private void handleModel(String paramName, Object param, Map testOutput, - IJsonSchemaValidationProperties spec, String baseType, String parent, int suffix) throws CTSException { + private void handleModel( + String paramName, + Object param, + Map testOutput, + IJsonSchemaValidationProperties spec, + String baseType, + String parent, + int suffix + ) throws CTSException { assert (spec.getHasVars()); assert (spec.getItems() == null); - if (spec instanceof CodegenModel && ((CodegenModel) spec).oneOf.size() > 0) { + if ( + spec instanceof CodegenModel && ((CodegenModel) spec).oneOf.size() > 0 + ) { // find a discriminator to handle oneOf CodegenModel model = (CodegenModel) spec; IJsonSchemaValidationProperties match = findMatchingOneOf(param, model); testOutput.clear(); - testOutput.putAll(traverseParams(paramName, param, match, parent, suffix)); + testOutput.putAll( + traverseParams(paramName, param, match, parent, suffix) + ); HashMap hashMapOneOfModel = new HashMap(); hashMapOneOfModel.put("classname", baseType); - hashMapOneOfModel.put("name", getTypeName(match).replace("<", "").replace(">", "")); + hashMapOneOfModel.put( + "name", + getTypeName(match).replace("<", "").replace(">", "") + ); testOutput.put("oneOfModel", hashMapOneOfModel); @@ -187,18 +238,37 @@ private void handleModel(String paramName, Object param, Map tes } } if (varSpec == null) { - throw new CTSException("Parameter " + entry.getKey() + " not found in " + paramName - + ". You might have a type conflict in the spec for " + baseType); + throw new CTSException( + "Parameter " + + entry.getKey() + + " not found in " + + paramName + + ". You might have a type conflict in the spec for " + + baseType + ); } - values.add(traverseParams(entry.getKey(), entry.getValue(), varSpec, paramName, suffix + 1)); + values.add( + traverseParams( + entry.getKey(), + entry.getValue(), + varSpec, + paramName, + suffix + 1 + ) + ); } testOutput.put("isObject", true); testOutput.put("value", values); } - private void handleObject(String paramName, Object param, Map testOutput, - IJsonSchemaValidationProperties spec, int suffix) throws CTSException { + private void handleObject( + String paramName, + Object param, + Map testOutput, + IJsonSchemaValidationProperties spec, + int suffix + ) throws CTSException { assert (!spec.getHasVars()); assert (spec.getItems() == null); @@ -208,15 +278,28 @@ private void handleObject(String paramName, Object param, Map te for (Entry entry : vars.entrySet()) { CodegenParameter objSpec = new CodegenParameter(); objSpec.dataType = inferDataType(entry.getValue(), objSpec, null); - values.add(traverseParams(entry.getKey(), entry.getValue(), objSpec, paramName, suffix + 1)); + values.add( + traverseParams( + entry.getKey(), + entry.getValue(), + objSpec, + paramName, + suffix + 1 + ) + ); } testOutput.put("isFreeFormObject", true); testOutput.put("value", values); } - private void handleMap(String paramName, Object param, Map testOutput, - IJsonSchemaValidationProperties spec, int suffix) throws CTSException { + private void handleMap( + String paramName, + Object param, + Map testOutput, + IJsonSchemaValidationProperties spec, + int suffix + ) throws CTSException { assert (!spec.getHasVars()); assert (spec.getItems() != null); @@ -224,14 +307,23 @@ private void handleMap(String paramName, Object param, Map testO List values = new ArrayList<>(); for (Entry entry : vars.entrySet()) { - values.add(traverseParams(entry.getKey(), entry.getValue(), spec.getItems(), paramName, suffix + 1)); + values.add( + traverseParams( + entry.getKey(), + entry.getValue(), + spec.getItems(), + paramName, + suffix + 1 + ) + ); } testOutput.put("isFreeFormObject", true); testOutput.put("value", values); } - private void handlePrimitive(Object param, Map testOutput) throws CTSException { + private void handlePrimitive(Object param, Map testOutput) + throws CTSException { inferDataType(param, null, testOutput); testOutput.put("value", param); } @@ -265,44 +357,43 @@ private boolean isEnum(IJsonSchemaValidationProperties param) { return false; } - private String inferDataType(Object param, CodegenParameter spec, Map output) throws CTSException { + private String inferDataType( + Object param, + CodegenParameter spec, + Map output + ) throws CTSException { switch (param.getClass().getSimpleName()) { case "String": - if (spec != null) - spec.setIsString(true); - if (output != null) - output.put("isString", true); + if (spec != null) spec.setIsString(true); + if (output != null) output.put("isString", true); return "String"; case "Integer": - if (spec != null) - spec.setIsNumber(true); - if (output != null) - output.put("isInteger", true); + if (spec != null) spec.setIsNumber(true); + if (output != null) output.put("isInteger", true); return "Integer"; case "Long": - if (spec != null) - spec.setIsNumber(true); - if (output != null) - output.put("isInteger", true); + if (spec != null) spec.setIsNumber(true); + if (output != null) output.put("isInteger", true); return "Long"; case "Double": - if (spec != null) - spec.setIsNumber(true); - if (output != null) - output.put("isDouble", true); + if (spec != null) spec.setIsNumber(true); + if (output != null) output.put("isDouble", true); return "Double"; case "Boolean": - if (spec != null) - spec.setIsBoolean(true); - if (output != null) - output.put("isBoolean", true); + if (spec != null) spec.setIsBoolean(true); + if (output != null) output.put("isBoolean", true); return "Boolean"; default: - throw new CTSException("Unknown type: " + param.getClass().getSimpleName()); + throw new CTSException( + "Unknown type: " + param.getClass().getSimpleName() + ); } } - private IJsonSchemaValidationProperties findMatchingOneOf(Object param, CodegenModel model) throws CTSException { + private IJsonSchemaValidationProperties findMatchingOneOf( + Object param, + CodegenModel model + ) throws CTSException { if (param instanceof Map) { // for object, check which has the most of property in common int maxCount = 0; @@ -343,8 +434,7 @@ private IJsonSchemaValidationProperties findMatchingOneOf(Object param, CodegenM } } for (CodegenModel oneOf : model.interfaceModels) { - if (oneOf.dataType.equals(paramType)) - return oneOf; + if (oneOf.dataType.equals(paramType)) return oneOf; } return null; } diff --git a/generators/src/main/java/com/algolia/codegen/cts/Request.java b/generators/src/main/java/com/algolia/codegen/cts/Request.java index eaa4f1cdd6..91a1996513 100644 --- a/generators/src/main/java/com/algolia/codegen/cts/Request.java +++ b/generators/src/main/java/com/algolia/codegen/cts/Request.java @@ -1,16 +1,16 @@ package com.algolia.codegen.cts; -import java.io.IOException; -import java.util.Map; - import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.TreeNode; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonDeserializer; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import java.io.IOException; +import java.util.Map; public class Request { + public String testName; public String method; @@ -32,6 +32,7 @@ public String toString() { } class RequestProp { + public String path; public String method; @@ -59,8 +60,7 @@ class RawDeserializer extends JsonDeserializer { @Override public String deserialize(JsonParser jp, DeserializationContext ctxt) - throws IOException, JsonProcessingException { - + throws IOException, JsonProcessingException { TreeNode tree = jp.getCodec().readTree(jp); return tree.toString(); } diff --git a/generators/src/main/java/com/algolia/codegen/cts/TestConfig.java b/generators/src/main/java/com/algolia/codegen/cts/TestConfig.java index eb4589d565..23de12c884 100644 --- a/generators/src/main/java/com/algolia/codegen/cts/TestConfig.java +++ b/generators/src/main/java/com/algolia/codegen/cts/TestConfig.java @@ -1,6 +1,7 @@ package com.algolia.codegen.cts; public class TestConfig { + public String extension; public String outputFolder; }