-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
90 changed files
with
1,784 additions
and
1,378 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
dist | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule algoliasearch-client-java-2
updated
from 33defb to bd8719
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"generatorName": "algolia-cts", | ||
"templateDir": "tests/CTS/methods/requests/templates/java", | ||
"outputDir": "tests/output/java", | ||
"artifactId": "java-tests", | ||
"groupId": "com.algolia", | ||
"invokerPackage": "com.algolia", | ||
"inputSpec": "specs/bundled/search.yml" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"generatorName": "algolia-java", | ||
"templateDir": "templates/java/", | ||
"outputDir": "clients/algoliasearch-client-java-2", | ||
"artifactId": "algoliasearch-client-java-2", | ||
"groupId": "com.algolia", | ||
"apiPackage": "com.algolia.search", | ||
"invokerPackage": "com.algolia", | ||
"modelPackage": "com.algolia.model.search", | ||
"library": "okhttp-gson", | ||
"inputSpec": "specs/bundled/search.yml", | ||
"gitHost": "algolia", | ||
"gitUserId": "algolia", | ||
"gitRepoId": "algoliasearch-client-java-2", | ||
"additionalProperties": { | ||
"sourceFolder": "algoliasearch-core", | ||
"java8": true, | ||
"dateLibrary": "java8", | ||
"packageName": "algoliasearch-client-java-2" | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
plugins { | ||
id 'java' | ||
} | ||
|
||
group = 'org.openapitools' | ||
version = '1.0.0' | ||
description = 'algolia-java-openapi-generator' | ||
java.sourceCompatibility = JavaVersion.VERSION_1_8 | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
compileOnly 'org.openapitools:openapi-generator:5.4.0' | ||
compileOnly 'org.yaml:snakeyaml:1.19' | ||
} | ||
|
||
tasks.withType(JavaCompile) { | ||
options.encoding = 'UTF-8' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
rootProject.name = 'algolia-java-openapi-generator' |
173 changes: 173 additions & 0 deletions
173
generators/src/main/java/com/algolia/codegen/AlgoliaJavaGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
package com.algolia.codegen; | ||
|
||
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.util.Map.Entry; | ||
import java.io.FileInputStream; | ||
import java.net.URL; | ||
|
||
import io.swagger.v3.oas.models.media.Schema; | ||
|
||
@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. | ||
* | ||
* @return the friendly name for the generator | ||
*/ | ||
@Override | ||
public String getName() { | ||
return "algolia-java"; | ||
} | ||
|
||
/** | ||
* Inject server info into the client to generate the right URL | ||
*/ | ||
private void generateServer(Map<String, Object> client) { | ||
String clientName = (String) client.get("pathPrefix"); | ||
Yaml yaml = new Yaml(); | ||
try { | ||
Map<String, Object> spec = yaml.load(new FileInputStream("specs/" + clientName + "/spec.yml")); | ||
List<Map<String, Object>> servers = (List<Map<String, Object>>) spec.get("servers"); | ||
|
||
boolean hasRegionalHost = false; | ||
boolean fallbackToAliasHost = false; | ||
|
||
boolean isEuHost = false; | ||
boolean isDeHost = false; | ||
String host = ""; | ||
String topLevelDomain = ""; | ||
|
||
for (Map<String, Object> server : servers) { | ||
if (!server.containsKey("url")) { | ||
throw new GenerationException("Invalid server, does not contains 'url'"); | ||
} | ||
|
||
if (!server.containsKey("variables")) { | ||
continue; | ||
} | ||
|
||
Map<String, Map<String, Object>> variables = (Map<String, Map<String, Object>>) server.get("variables"); | ||
|
||
if (!variables.containsKey("region") || !variables.get("region").containsKey("enum")) { | ||
continue; | ||
} | ||
ArrayList<String> enums = (ArrayList<String>) variables.get("region").get("enum"); | ||
hasRegionalHost = true; | ||
|
||
URL url = new URL((String) server.get("url")); | ||
|
||
if (!fallbackToAliasHost) { | ||
// Determine if the current URL with `region` also have an alias without | ||
// variables. | ||
fallbackToAliasHost = true; | ||
} | ||
|
||
if (enums.contains("eu")) { | ||
isEuHost = true; | ||
} | ||
|
||
if (enums.contains("de")) { | ||
isDeHost = true; | ||
} | ||
|
||
// This is used for hosts like `insights` that uses `.io` | ||
String[] hostParts = url.getHost().split("\\."); | ||
host = hostParts[0]; | ||
topLevelDomain = hostParts[hostParts.length - 1]; | ||
} | ||
client.put("hasRegionalHost", hasRegionalHost); | ||
client.put("fallbackToAliasHost", fallbackToAliasHost); | ||
client.put("isEuHost", isEuHost); | ||
client.put("isDeHost", isDeHost); | ||
client.put("host", host); | ||
client.put("topLevelDomain", topLevelDomain); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
/** | ||
* Provides an opportunity to inspect and modify operation data before the code | ||
* is generated. | ||
*/ | ||
@Override | ||
public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> objs, List<Object> allModels) { | ||
Map<String, Object> results = super.postProcessOperationsWithModels(objs, allModels); | ||
Map<String, Object> client = (Map<String, Object>) results.get("operations"); | ||
|
||
generateServer(client); | ||
|
||
return results; | ||
} | ||
|
||
@Override | ||
public Map<String, Object> postProcessAllModels(Map<String, Object> objs) { | ||
Map<String, Object> models = super.postProcessAllModels(objs); | ||
|
||
for (Object modelContainer : models.values()) { | ||
CodegenModel model = ((Map<String, List<Map<String, CodegenModel>>>) modelContainer).get("models").get(0) | ||
.get("model"); | ||
if (!model.oneOf.isEmpty()) { | ||
model.vendorExtensions.put("x-is-one-of-interface", true); | ||
} | ||
} | ||
|
||
return models; | ||
} | ||
|
||
@Override | ||
public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) { | ||
Map<String, Object> bundle = super.postProcessSupportingFileData(objs); | ||
List<Map<String, Object>> apis = ((Map<String, List<Map<String, Object>>>) bundle.get("apiInfo")).get("apis"); | ||
for (Map<String, Object> api : apis) { | ||
List<CodegenOperation> operations = ((Map<String, List<CodegenOperation>>) api.get("operations")) | ||
.get("operation"); | ||
|
||
for (CodegenOperation ope : operations) { | ||
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 | ||
* | ||
* @return A string value for the help message | ||
*/ | ||
@Override | ||
public String getHelp() { | ||
return "Generates an algolia-java client library."; | ||
} | ||
|
||
public AlgoliaJavaGenerator() { | ||
super(); | ||
|
||
supportingFiles.add(new SupportingFile("EchoResponse.mustache", | ||
"algoliasearch-core/com/algolia/utils/echo", | ||
"EchoResponse.java")); | ||
|
||
// Prevent all useless file to generate | ||
apiTestTemplateFiles.clear(); | ||
modelTestTemplateFiles.clear(); | ||
apiDocTemplateFiles.clear(); | ||
modelDocTemplateFiles.clear(); | ||
} | ||
|
||
@Override | ||
public String toDefaultValue(Schema schema) { | ||
// Replace the {} from openapi with new Object() | ||
if (ModelUtils.isObjectSchema(schema) && schema.getDefault() != null) { | ||
return "new Object()"; | ||
} | ||
return super.toDefaultValue(schema); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
generators/src/main/java/com/algolia/codegen/GenerationException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.algolia.codegen; | ||
|
||
public class GenerationException extends Exception { | ||
public GenerationException(String message) { | ||
super(message); | ||
} | ||
} |
Oops, something went wrong.