Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
{
"trailingComma": "es5",
"singleQuote": true,
"overrides" : [
"overrides": [
{
"files": "*.php",
"options": {
"tabWidth": 4
"tabWidth": 4
}
},
{
"files": "*.java",
"options": {
"printWidth": 140
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

definitely needed!

}
}
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.algolia.codegen;

import com.algolia.codegen.exceptions.*;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.servers.Server;
Expand All @@ -11,54 +12,60 @@
@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";
}

@Override
public CodegenOperation fromOperation(
String path,
String httpMethod,
Operation operation,
List<Server> servers
) {
return Utils.specifyCustomRequest(
super.fromOperation(path, httpMethod, operation, servers)
public void processOpts() {
// generator specific options
setDateLibrary("java8");
setLibrary("okhttp-gson");
setSourceFolder("algoliasearch-core/src/main/java");
setGroupId("com.algolia");
additionalProperties.put("invokerPackage", "com.algolia");
setApiPackage("com.algolia.api");
setApiNameSuffix(Utils.API_SUFFIX);

super.processOpts();

// Prevent all useless file to generate
apiTestTemplateFiles.clear();
modelTestTemplateFiles.clear();
apiDocTemplateFiles.clear();
modelDocTemplateFiles.clear();

supportingFiles.removeIf(file ->
file.getTemplateFile().equals("build.gradle.mustache") ||
file.getTemplateFile().equals("settings.gradle.mustache") ||
file.getTemplateFile().equals("gitignore.mustache") ||
file.getTemplateFile().equals("ApiCallback.mustache") ||
file.getTemplateFile().equals("ApiResponse.mustache") ||
file.getTemplateFile().equals("JSON.mustache") ||
file.getTemplateFile().equals("ProgressRequestBody.mustache") ||
file.getTemplateFile().equals("ProgressResponseBody.mustache") ||
file.getTemplateFile().equals("Pair.mustache")
);
}

/** 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
);
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, List<Server> servers) {
return Utils.specifyCustomRequest(super.fromOperation(path, httpMethod, operation, servers));
}

@Override
public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> objs, List<Object> allModels) {
Map<String, Object> results = super.postProcessOperationsWithModels(objs, allModels);

String kebabClient = Utils.getClientNameKebabCase(results);
additionalProperties.put("isSearchClient", kebabClient.equals("search"));

try {
Utils.generateServer(
Utils.getClientNameKebabCase(results),
additionalProperties
);
additionalProperties.put(
"isSearchClient",
Utils.getClientNameKebabCase(results).equals("search")
);
additionalProperties.put(
"packageVersion",
Utils.getClientConfigField("java", "packageVersion")
);
} catch (GenerationException e) {
Utils.generateServer(kebabClient, additionalProperties);

additionalProperties.put("packageVersion", Utils.getClientConfigField("java", "packageVersion"));
} catch (GeneratorException e) {
e.printStackTrace();
System.exit(1);
}
Expand All @@ -71,23 +78,15 @@ 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");
CodegenModel model = ((Map<String, List<Map<String, CodegenModel>>>) modelContainer).get("models").get(0).get("model");
if (!model.oneOf.isEmpty()) {
List<HashMap<String, String>> oneOfList = new ArrayList();

for (String iterateModel : model.oneOf) {
HashMap<String, String> oneOfModel = new HashMap();

oneOfModel.put("type", iterateModel);
oneOfModel.put(
"name",
iterateModel.replace("<", "").replace(">", "")
);
oneOfModel.put("name", iterateModel.replace("<", "").replace(">", ""));

oneOfList.add(oneOfModel);
}
Expand All @@ -100,49 +99,6 @@ public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
return models;
}

/**
* 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.";
}

@Override
public void processOpts() {
// generator specific options
setDateLibrary("java8");
setLibrary("okhttp-gson");
setSourceFolder("algoliasearch-core/src/main/java");
setGroupId("com.algolia");
additionalProperties.put("invokerPackage", "com.algolia");
setApiPackage("com.algolia.api");
setApiNameSuffix(Utils.API_SUFFIX);

super.processOpts();

// Prevent all useless file to generate
apiTestTemplateFiles.clear();
modelTestTemplateFiles.clear();
apiDocTemplateFiles.clear();
modelDocTemplateFiles.clear();

supportingFiles.removeIf(file ->
file.getTemplateFile().equals("build.gradle.mustache") ||
file.getTemplateFile().equals("settings.gradle.mustache") ||
file.getTemplateFile().equals("gitignore.mustache") ||
file.getTemplateFile().equals("ApiCallback.mustache") ||
file.getTemplateFile().equals("ApiResponse.mustache") ||
file.getTemplateFile().equals("JSON.mustache") ||
file.getTemplateFile().equals("ProgressRequestBody.mustache") ||
file.getTemplateFile().equals("ProgressResponseBody.mustache") ||
file.getTemplateFile().equals("Pair.mustache")
);
}

@Override
public String toDefaultValue(Schema schema) {
// Replace the {} from openapi with new Object()
Expand All @@ -156,10 +112,7 @@ public String toDefaultValue(Schema schema) {
public String toEnumVarName(String value, String datatype) {
if ("String".equals(datatype)) {
// convert camelCase77String to CAMEL_CASE_77_STRING
return value
.replaceAll("-", "_")
.replaceAll("(.+?)([A-Z]|[0-9])", "$1_$2")
.toUpperCase(Locale.ROOT);
return value.replaceAll("-", "_").replaceAll("(.+?)([A-Z]|[0-9])", "$1_$2").toUpperCase(Locale.ROOT);
}
return super.toEnumVarName(value, datatype);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.algolia.codegen;

import com.algolia.codegen.exceptions.*;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.parameters.Parameter;
Expand Down Expand Up @@ -29,28 +30,22 @@ public void processOpts() {
setApiPackage("src");
// clear all supported files to avoid unwanted ones
supportingFiles.clear();
// model
supportingFiles.add(
new SupportingFile("modelBarrel.mustache", "model", "index.ts")
);
// builds
supportingFiles.add(
new SupportingFile("browser.mustache", "builds", "browser.ts")
);
supportingFiles.add(
new SupportingFile("node.mustache", "builds", "node.ts")
);

supportingFiles.add(new SupportingFile("modelBarrel.mustache", "model", "index.ts"));
supportingFiles.add(new SupportingFile("browser.mustache", "builds", "browser.ts"));
supportingFiles.add(new SupportingFile("node.mustache", "builds", "node.ts"));

// root
supportingFiles.add(new SupportingFile("index.mustache", "", "index.js"));
supportingFiles.add(
new SupportingFile("index.d.mustache", "", "index.d.ts")
);
supportingFiles.add(
new SupportingFile("package.mustache", "", "package.json")
);
supportingFiles.add(
new SupportingFile("tsconfig.mustache", "", "tsconfig.json")
);
supportingFiles.add(new SupportingFile("index.d.mustache", "", "index.d.ts"));

supportingFiles.add(new SupportingFile("package.mustache", "", "package.json"));
supportingFiles.add(new SupportingFile("tsconfig.mustache", "", "tsconfig.json"));
}

@Override
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, List<Server> servers) {
return Utils.specifyCustomRequest(super.fromOperation(path, httpMethod, operation, servers));
}

/** Set default generator options */
Expand All @@ -66,36 +61,21 @@ private void setDefaultGeneratorOptions() {

/** 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
);
public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> objs, List<Object> allModels) {
Map<String, Object> results = super.postProcessOperationsWithModels(objs, allModels);

CLIENT = Utils.getClientNameCamelCase(results);

setDefaultGeneratorOptions();
try {
Utils.generateServer(
Utils.getClientNameKebabCase(results),
additionalProperties
);
additionalProperties.put(
"utilsPackageVersion",
Utils.getClientConfigField("javascript", "utilsPackageVersion")
);
} catch (GenerationException e) {
Utils.generateServer(Utils.getClientNameKebabCase(results), additionalProperties);
additionalProperties.put("utilsPackageVersion", Utils.getClientConfigField("javascript", "utilsPackageVersion"));
} catch (GeneratorException e) {
e.printStackTrace();
System.exit(1);
}

List<CodegenOperation> operations =
((Map<String, List<CodegenOperation>>) results.get("operations")).get(
"operation"
);
List<CodegenOperation> operations = ((Map<String, List<CodegenOperation>>) results.get("operations")).get("operation");

// We read operations and detect if we should wrap parameters under an object.
// We only wrap if there is a mix between body parameters and other parameters.
Expand All @@ -111,9 +91,7 @@ public Map<String, Object> postProcessOperationsWithModels(
boolean hasPathParams = !ope.pathParams.isEmpty();

// If there is nothing but body params, we just check if it's a single param
if (
hasBodyParams && !hasHeaderParams && !hasQueryParams && !hasPathParams
) {
if (hasBodyParams && !hasHeaderParams && !hasQueryParams && !hasPathParams) {
// At this point the single parameter is already an object, to avoid double wrapping
// we skip it
if (ope.bodyParams.size() == 1 && !ope.bodyParams.get(0).isArray) {
Expand Down Expand Up @@ -160,18 +138,6 @@ public String apiFilename(String templateName, String tag) {
return super.apiFilename(templateName, toApiFilename(CLIENT));
}

@Override
public CodegenOperation fromOperation(
String path,
String httpMethod,
Operation operation,
List<Server> servers
) {
return Utils.specifyCustomRequest(
super.fromOperation(path, httpMethod, operation, servers)
);
}

@Override
protected String getParameterDataType(Parameter parameter, Schema p) {
String type = super.getParameterDataType(parameter, p);
Expand Down
Loading