From ec36c17bb767ac9e2a36bfc3f7563222f7c9a809 Mon Sep 17 00:00:00 2001 From: Pierre Millot Date: Thu, 18 Jan 2024 16:03:23 +0100 Subject: [PATCH] fix(java): correct url for monitoring client --- generators/build.gradle | 1 - .../codegen/AlgoliaCSharpGenerator.java | 12 ++-- .../algolia/codegen/AlgoliaDartGenerator.java | 8 ++- .../algolia/codegen/AlgoliaGoGenerator.java | 8 ++- .../algolia/codegen/AlgoliaJavaGenerator.java | 9 ++- .../codegen/AlgoliaJavascriptGenerator.java | 8 ++- .../codegen/AlgoliaKotlinGenerator.java | 16 ++---- .../algolia/codegen/AlgoliaPhpGenerator.java | 8 ++- .../codegen/AlgoliaPythonGenerator.java | 12 ++-- .../algolia/codegen/AlgoliaRubyGenerator.java | 14 ++--- .../codegen/AlgoliaScalaGenerator.java | 8 ++- .../codegen/AlgoliaSwiftGenerator.java | 8 ++- .../com/algolia/codegen/utils/Helpers.java | 55 ++++++++----------- templates/java/api.mustache | 14 ++++- 14 files changed, 107 insertions(+), 74 deletions(-) diff --git a/generators/build.gradle b/generators/build.gradle index 70a5a12db9..efdb0ff9cd 100644 --- a/generators/build.gradle +++ b/generators/build.gradle @@ -12,7 +12,6 @@ repositories { dependencies { compileOnly 'org.openapitools:openapi-generator:7.2.0' - compileOnly 'org.yaml:snakeyaml:2.2' } tasks.withType(JavaCompile) { diff --git a/generators/src/main/java/com/algolia/codegen/AlgoliaCSharpGenerator.java b/generators/src/main/java/com/algolia/codegen/AlgoliaCSharpGenerator.java index 1a0929620f..3d8be9be86 100644 --- a/generators/src/main/java/com/algolia/codegen/AlgoliaCSharpGenerator.java +++ b/generators/src/main/java/com/algolia/codegen/AlgoliaCSharpGenerator.java @@ -3,6 +3,7 @@ import com.algolia.codegen.exceptions.*; import com.algolia.codegen.utils.*; import com.algolia.codegen.utils.OneOf; +import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.Operation; import io.swagger.v3.oas.models.servers.Server; import java.io.IOException; @@ -121,13 +122,12 @@ public void processOpts() { supportingFiles.add(new SupportingFile("netcore_project.mustache", "Algolia.Search.csproj")); supportingFiles.add(new SupportingFile("AbstractOpenAPISchema.mustache", "Models", "AbstractSchema.cs")); supportingFiles.add(new SupportingFile("gitignore.mustache", "../", ".gitignore")); + } - try { - Helpers.generateServer(CLIENT, additionalProperties); - } catch (GeneratorException e) { - e.printStackTrace(); - System.exit(1); - } + @Override + public void processOpenAPI(OpenAPI openAPI) { + super.processOpenAPI(openAPI); + Helpers.generateServers(super.fromServers(openAPI.getServers()), additionalProperties); } @Override diff --git a/generators/src/main/java/com/algolia/codegen/AlgoliaDartGenerator.java b/generators/src/main/java/com/algolia/codegen/AlgoliaDartGenerator.java index 20c064256c..5cedf8ce8a 100644 --- a/generators/src/main/java/com/algolia/codegen/AlgoliaDartGenerator.java +++ b/generators/src/main/java/com/algolia/codegen/AlgoliaDartGenerator.java @@ -3,6 +3,7 @@ import static org.apache.commons.lang3.StringUtils.*; import com.algolia.codegen.utils.*; +import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.Operation; import io.swagger.v3.oas.models.servers.Server; import java.util.*; @@ -102,9 +103,12 @@ public void processOpts() { // Search config additionalProperties.put("isSearchClient", client.equals("search")); additionalProperties.put("packageVersion", version); + } - // Generate server info - Helpers.generateServer(client, additionalProperties); + @Override + public void processOpenAPI(OpenAPI openAPI) { + super.processOpenAPI(openAPI); + Helpers.generateServers(super.fromServers(openAPI.getServers()), additionalProperties); } @Override diff --git a/generators/src/main/java/com/algolia/codegen/AlgoliaGoGenerator.java b/generators/src/main/java/com/algolia/codegen/AlgoliaGoGenerator.java index a7ae97abe3..8d89728c29 100644 --- a/generators/src/main/java/com/algolia/codegen/AlgoliaGoGenerator.java +++ b/generators/src/main/java/com/algolia/codegen/AlgoliaGoGenerator.java @@ -3,6 +3,7 @@ import com.algolia.codegen.exceptions.*; import com.algolia.codegen.utils.*; import com.algolia.codegen.utils.OneOf; +import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.Operation; import io.swagger.v3.oas.models.servers.Server; import java.io.File; @@ -53,7 +54,6 @@ public void processOpts() { supportingFiles.add(new SupportingFile("client.mustache", "", "client.go")); try { - Helpers.generateServer(client, additionalProperties); additionalProperties.put("packageVersion", Helpers.getClientConfigField("go", "packageVersion")); } catch (GeneratorException e) { e.printStackTrace(); @@ -61,6 +61,12 @@ public void processOpts() { } } + @Override + public void processOpenAPI(OpenAPI openAPI) { + super.processOpenAPI(openAPI); + Helpers.generateServers(super.fromServers(openAPI.getServers()), additionalProperties); + } + @Override public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, List servers) { return Helpers.specifyCustomRequest(super.fromOperation(path, httpMethod, operation, servers)); diff --git a/generators/src/main/java/com/algolia/codegen/AlgoliaJavaGenerator.java b/generators/src/main/java/com/algolia/codegen/AlgoliaJavaGenerator.java index 65d3ddbee4..b77d30fdc8 100644 --- a/generators/src/main/java/com/algolia/codegen/AlgoliaJavaGenerator.java +++ b/generators/src/main/java/com/algolia/codegen/AlgoliaJavaGenerator.java @@ -4,6 +4,7 @@ import com.algolia.codegen.utils.*; import com.algolia.codegen.utils.OneOf; import com.samskivert.mustache.Mustache; +import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.Operation; import io.swagger.v3.oas.models.media.Schema; import io.swagger.v3.oas.models.servers.Server; @@ -52,8 +53,6 @@ public void processOpts() { additionalProperties.put("lambda.type-to-name", (Mustache.Lambda) (fragment, writer) -> writer.write(typeToName(fragment.execute()))); try { - Helpers.generateServer(client, additionalProperties); - additionalProperties.put("packageVersion", Helpers.getClientConfigField("java", "packageVersion")); } catch (GeneratorException e) { e.printStackTrace(); @@ -61,6 +60,12 @@ public void processOpts() { } } + @Override + public void processOpenAPI(OpenAPI openAPI) { + super.processOpenAPI(openAPI); + Helpers.generateServers(super.fromServers(openAPI.getServers()), additionalProperties); + } + @Override protected void addAdditionPropertiesToCodeGenModel(CodegenModel codegenModel, Schema schema) { // this is needed to preserve additionalProperties: true diff --git a/generators/src/main/java/com/algolia/codegen/AlgoliaJavascriptGenerator.java b/generators/src/main/java/com/algolia/codegen/AlgoliaJavascriptGenerator.java index 947521e1c7..109982fae8 100644 --- a/generators/src/main/java/com/algolia/codegen/AlgoliaJavascriptGenerator.java +++ b/generators/src/main/java/com/algolia/codegen/AlgoliaJavascriptGenerator.java @@ -2,6 +2,7 @@ import com.algolia.codegen.exceptions.*; import com.algolia.codegen.utils.*; +import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.Operation; import io.swagger.v3.oas.models.servers.Server; import java.util.List; @@ -160,6 +161,12 @@ private void setDefaultGeneratorOptions() { } } + @Override + public void processOpenAPI(OpenAPI openAPI) { + super.processOpenAPI(openAPI); + Helpers.generateServers(super.fromServers(openAPI.getServers()), additionalProperties); + } + @Override public Map postProcessAllModels(Map objs) { Map models = super.postProcessAllModels(objs); @@ -174,7 +181,6 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List", "").replace(", ", "").replace(".", ""); } - private void hostForKotlin() { + @Override + public void processOpenAPI(OpenAPI openAPI) { + super.processOpenAPI(openAPI); + Helpers.generateServers(super.fromServers(openAPI.getServers()), additionalProperties); + String host = (String) additionalProperties.get("regionalHost"); if (host != null) { String hostForKotlin = host.replaceAll("\\{([^}]+)}", "\\$$1"); diff --git a/generators/src/main/java/com/algolia/codegen/AlgoliaPhpGenerator.java b/generators/src/main/java/com/algolia/codegen/AlgoliaPhpGenerator.java index a2f79641d5..d3a4559e95 100644 --- a/generators/src/main/java/com/algolia/codegen/AlgoliaPhpGenerator.java +++ b/generators/src/main/java/com/algolia/codegen/AlgoliaPhpGenerator.java @@ -2,6 +2,7 @@ import com.algolia.codegen.exceptions.*; import com.algolia.codegen.utils.*; +import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.Operation; import io.swagger.v3.oas.models.servers.Server; import java.util.List; @@ -49,7 +50,6 @@ public void processOpts() { setDefaultGeneratorOptions(client); try { - Helpers.generateServer(client, additionalProperties); additionalProperties.put("packageVersion", Helpers.getClientConfigField("php", "packageVersion")); } catch (GeneratorException e) { e.printStackTrace(); @@ -57,6 +57,12 @@ public void processOpts() { } } + @Override + public void processOpenAPI(OpenAPI openAPI) { + super.processOpenAPI(openAPI); + Helpers.generateServers(super.fromServers(openAPI.getServers()), additionalProperties); + } + @Override public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, List servers) { return Helpers.specifyCustomRequest(super.fromOperation(path, httpMethod, operation, servers)); diff --git a/generators/src/main/java/com/algolia/codegen/AlgoliaPythonGenerator.java b/generators/src/main/java/com/algolia/codegen/AlgoliaPythonGenerator.java index 07365886c9..f8e0f6bd1e 100644 --- a/generators/src/main/java/com/algolia/codegen/AlgoliaPythonGenerator.java +++ b/generators/src/main/java/com/algolia/codegen/AlgoliaPythonGenerator.java @@ -2,6 +2,7 @@ import com.algolia.codegen.exceptions.*; import com.algolia.codegen.utils.*; +import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.Operation; import io.swagger.v3.oas.models.servers.Server; import java.util.*; @@ -83,13 +84,12 @@ public void processOpts() { supportingFiles.add(new SupportingFile("__init__.mustache", packageName + "/models", "__init__.py")); supportingFiles.add(new SupportingFile("__init__.mustache", "http", "__init__.py")); supportingFiles.add(new SupportingFile("config.mustache", packageName, "config.py")); + } - try { - Helpers.generateServer(CLIENT, additionalProperties); - } catch (GeneratorException e) { - e.printStackTrace(); - System.exit(1); - } + @Override + public void processOpenAPI(OpenAPI openAPI) { + super.processOpenAPI(openAPI); + Helpers.generateServers(super.fromServers(openAPI.getServers()), additionalProperties); } @Override diff --git a/generators/src/main/java/com/algolia/codegen/AlgoliaRubyGenerator.java b/generators/src/main/java/com/algolia/codegen/AlgoliaRubyGenerator.java index f48027c6ba..804d5e810d 100644 --- a/generators/src/main/java/com/algolia/codegen/AlgoliaRubyGenerator.java +++ b/generators/src/main/java/com/algolia/codegen/AlgoliaRubyGenerator.java @@ -2,6 +2,7 @@ import com.algolia.codegen.exceptions.*; import com.algolia.codegen.utils.*; +import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.Operation; import io.swagger.v3.oas.models.servers.Server; import java.util.*; @@ -60,15 +61,12 @@ public void processOpts() { file.getTemplateFile().equals("spec_helper.mustache") || file.getTemplateFile().equals("rubocop.mustache") ); + } - // repository - - try { - Helpers.generateServer(CLIENT, additionalProperties); - } catch (GeneratorException e) { - e.printStackTrace(); - System.exit(1); - } + @Override + public void processOpenAPI(OpenAPI openAPI) { + super.processOpenAPI(openAPI); + Helpers.generateServers(super.fromServers(openAPI.getServers()), additionalProperties); } @Override diff --git a/generators/src/main/java/com/algolia/codegen/AlgoliaScalaGenerator.java b/generators/src/main/java/com/algolia/codegen/AlgoliaScalaGenerator.java index c8fb42f008..e0af02f4ec 100644 --- a/generators/src/main/java/com/algolia/codegen/AlgoliaScalaGenerator.java +++ b/generators/src/main/java/com/algolia/codegen/AlgoliaScalaGenerator.java @@ -3,6 +3,7 @@ import com.algolia.codegen.exceptions.GeneratorException; import com.algolia.codegen.utils.*; import com.samskivert.mustache.Mustache; +import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.Operation; import io.swagger.v3.oas.models.servers.Server; import java.io.File; @@ -77,7 +78,6 @@ public void processOpts() { nameMapping.putAll(NAME_MAPPING); try { - Helpers.generateServer(client, additionalProperties); additionalProperties.put("packageVersion", Helpers.getClientConfigField("scala", "packageVersion")); } catch (GeneratorException e) { logger.severe(e.getMessage()); @@ -90,6 +90,12 @@ private String typeToName(String content) { return content.trim().replace("[", "Of").replace("]", "").replace(".", "").replace(", ", ""); } + @Override + public void processOpenAPI(OpenAPI openAPI) { + super.processOpenAPI(openAPI); + Helpers.generateServers(super.fromServers(openAPI.getServers()), additionalProperties); + } + @Override public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, List servers) { CodegenOperation ope = super.fromOperation(path, httpMethod, operation, servers); diff --git a/generators/src/main/java/com/algolia/codegen/AlgoliaSwiftGenerator.java b/generators/src/main/java/com/algolia/codegen/AlgoliaSwiftGenerator.java index 56190f299e..cb1be9e73a 100644 --- a/generators/src/main/java/com/algolia/codegen/AlgoliaSwiftGenerator.java +++ b/generators/src/main/java/com/algolia/codegen/AlgoliaSwiftGenerator.java @@ -7,6 +7,7 @@ import com.algolia.codegen.utils.Helpers; import com.algolia.codegen.utils.OneOf; import com.samskivert.mustache.Mustache; +import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.media.ArraySchema; import io.swagger.v3.oas.models.media.ComposedSchema; import io.swagger.v3.oas.models.media.Schema; @@ -116,7 +117,6 @@ public void processOpts() { Helpers.setGenerationBanner(additionalProperties); try { - Helpers.generateServer(CLIENT, additionalProperties); additionalProperties.put("packageVersion", Helpers.getClientConfigField("swift", "packageVersion")); additionalProperties.put("packageList", Helpers.getClientListForLanguage("swift")); } catch (GeneratorException e) { @@ -187,6 +187,12 @@ private void updateModelsOneOfForSwift(Map models, String mod } } + @Override + public void processOpenAPI(OpenAPI openAPI) { + super.processOpenAPI(openAPI); + Helpers.generateServers(super.fromServers(openAPI.getServers()), additionalProperties); + } + @Override public Map postProcessAllModels(Map objs) { Map models = super.postProcessAllModels(objs); diff --git a/generators/src/main/java/com/algolia/codegen/utils/Helpers.java b/generators/src/main/java/com/algolia/codegen/utils/Helpers.java index 9944564588..5b5c1c7596 100644 --- a/generators/src/main/java/com/algolia/codegen/utils/Helpers.java +++ b/generators/src/main/java/com/algolia/codegen/utils/Helpers.java @@ -5,13 +5,14 @@ import com.fasterxml.jackson.databind.node.ArrayNode; import io.swagger.v3.core.util.Json; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; +import java.net.MalformedURLException; import java.net.URL; import java.util.*; import org.apache.commons.lang3.StringUtils; import org.openapitools.codegen.CodegenOperation; -import org.yaml.snakeyaml.Yaml; +import org.openapitools.codegen.CodegenServer; +import org.openapitools.codegen.CodegenServerVariable; public class Helpers { @@ -86,70 +87,62 @@ public static String toPascalCase(String s) { } /** Inject server info into the client to generate the right URL */ - public static void generateServer(String clientKebab, Map additionalProperties) throws ConfigException { - Yaml yaml = new Yaml(); + public static void generateServers(List servers, Map bundle) throws ConfigException { try { - Map spec = yaml.load(new FileInputStream("specs/bundled/" + clientKebab + ".yml")); - List> servers = (List>) spec.get("servers"); - boolean hasRegionalHost = false; boolean fallbackToAliasHost = false; String regionalHost = ""; String hostWithFallback = ""; Set allowedRegions = new HashSet<>(); - for (Map server : servers) { - if (!server.containsKey("url")) { + for (CodegenServer server : servers) { + if (server.url.isEmpty()) { throw new ConfigException("Invalid server, does not contains 'url'"); } // Determine if the current URL with `region` also have an alias without // variables. - for (Map otherServer : servers) { + for (CodegenServer otherServer : servers) { if (server == otherServer) { continue; } - String otherUrl = (String) otherServer.getOrDefault("url", ""); - if (otherUrl.replace(".{region}", "").equals(server.get("url"))) { - URL fallbackURL = new URL(otherUrl.replace(".{region}", "")); + if (otherServer.url.replace(".{region}", "").equals(server.url)) { + URL fallbackURL = new URL(otherServer.url.replace(".{region}", "")); fallbackToAliasHost = true; hostWithFallback = fallbackURL.getHost(); break; } } - if (!server.containsKey("variables")) { + if (server.variables == null || server.variables.isEmpty()) { continue; } - - Map> variables = (Map>) server.get("variables"); - - if (!variables.containsKey("region") || !variables.get("region").containsKey("enum")) { + CodegenServerVariable regionVar = server.variables.stream().filter(v -> v.name.equals("region")).findFirst().orElse(null); + if (regionVar == null || regionVar.enumValues == null || regionVar.enumValues.isEmpty()) { continue; } - ArrayList regions = (ArrayList) variables.get("region").get("enum"); - hasRegionalHost = true; - for (String region : regions) { + hasRegionalHost = true; + for (String region : regionVar.enumValues) { allowedRegions.add(region); } // This is used for hosts like `insights` that uses `.io` - URL url = new URL((String) server.get("url")); + URL url = new URL(server.url); regionalHost = url.getHost(); } if (servers.size() == 1 && hostWithFallback.isEmpty() && !hasRegionalHost) { - URL url = new URL((String) servers.get(0).get("url")); - additionalProperties.put("uniqueHost", url.getHost()); + URL url = new URL(servers.get(0).url); + bundle.put("uniqueHost", url.getHost()); } - additionalProperties.put("hostWithFallback", hostWithFallback); - additionalProperties.put("hasRegionalHost", hasRegionalHost); - additionalProperties.put("fallbackToAliasHost", fallbackToAliasHost); - additionalProperties.put("regionalHost", regionalHost); - additionalProperties.put("allowedRegions", allowedRegions.toArray(new String[0])); - } catch (Exception e) { - throw new ConfigException("Couldn't generate servers", e); + bundle.put("hostWithFallback", hostWithFallback); + bundle.put("hasRegionalHost", hasRegionalHost); + bundle.put("fallbackToAliasHost", fallbackToAliasHost); + bundle.put("regionalHost", regionalHost); + bundle.put("allowedRegions", allowedRegions.toArray(new String[0])); + } catch (MalformedURLException e) { + throw new ConfigException("Invalid server URL", e); } } diff --git a/templates/java/api.mustache b/templates/java/api.mustache index 263a69f4a2..369aa2a73e 100644 --- a/templates/java/api.mustache +++ b/templates/java/api.mustache @@ -59,11 +59,11 @@ public class {{classname}} extends ApiClient { } public {{classname}}(String appId, String apiKey, ClientOptions options) { - super(appId, apiKey, "{{{baseName}}}", options, getDefaultHosts(appId)); + super(appId, apiKey, "{{{baseName}}}", options, getDefaultHosts({{^uniqueHost}}appId{{/uniqueHost}})); } {{/hasRegionalHost}} - {{^hasRegionalHost}} + {{^uniqueHost}}{{^hasRegionalHost}} private static List getDefaultHosts(String appId) { List hosts = new ArrayList<>(); hosts.add(new Host(appId + "-dsn.algolia.net", EnumSet.of(CallType.READ))); @@ -103,7 +103,15 @@ public class {{classname}} extends ApiClient { hosts.add(new Host(url, EnumSet.of(CallType.READ, CallType.WRITE))); return hosts; } - {{/hasRegionalHost}} + {{/hasRegionalHost}}{{/uniqueHost}} + + {{#uniqueHost}} + private static List getDefaultHosts() { + List hosts = new ArrayList<>(); + hosts.add(new Host("{{{.}}}", EnumSet.of(CallType.READ, CallType.WRITE))); + return hosts; + } + {{/uniqueHost}} {{#operation}} /**