Skip to content

style(generator): format on the CI APIC-415 #342

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Apr 6, 2022
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
3 changes: 3 additions & 0 deletions .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
11 changes: 10 additions & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ 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
Comment on lines +33 to +37
Copy link
Member

Choose a reason for hiding this comment

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

This could be the ultimate step of this job, with the same thing we do for the CTS

      - name: Check diff with pushed code
        run: |
          git --no-pager diff
          exit $(git status --porcelain | wc -l)

So we know what's the diff and can debug, wdyt?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As it's not something you can debug by hand file by file I don't think it matters, the only solution is to run the provided command, which will affect every files.

Copy link
Member

Choose a reason for hiding this comment

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

My comment was more about splitting logic but anyway it's not a big deal here, I'm fine with that


outputs:
RUN_SCRIPTS: ${{ steps.setup.outputs.RUN_SCRIPTS }}

Expand All @@ -51,7 +60,7 @@ jobs:

scripts:
runs-on: ubuntu-20.04
timeout-minutes: 20
timeout-minutes: 10
needs: setup
if: ${{ needs.setup.outputs.RUN_SCRIPTS == 'true' }}
steps:
Expand Down
Original file line number Diff line number Diff line change
@@ -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
*/
Expand All @@ -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<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");
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;
Expand All @@ -47,19 +47,28 @@ private void generateServer(Map<String, Object> client) {

for (Map<String, Object> 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<String, Map<String, Object>> variables = (Map<String, Map<String, Object>>) server.get("variables");
Map<String, Map<String, Object>> variables = (Map<String, Map<String, Object>>) server.get(
"variables"
);

if (!variables.containsKey("region") || !variables.get("region").containsKey("enum")) {
if (
!variables.containsKey("region") ||
!variables.get("region").containsKey("enum")
) {
continue;
}
ArrayList<String> enums = (ArrayList<String>) variables.get("region").get("enum");
ArrayList<String> enums = (ArrayList<String>) variables
.get("region")
.get("enum");
hasRegionalHost = true;

URL url = new URL((String) server.get("url"));
Expand Down Expand Up @@ -95,18 +104,30 @@ private void generateServer(Map<String, Object> client) {
}

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

Expand All @@ -118,7 +139,11 @@ 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)
CodegenModel model =
((Map<String, List<Map<String, CodegenModel>>>) modelContainer).get(
"models"
)
.get(0)
.get("model");
if (!model.oneOf.isEmpty()) {
List<HashMap<String, String>> listOneOf = new ArrayList();
Expand All @@ -127,7 +152,10 @@ public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
HashMap<String, String> hashMapOneOf = new HashMap();

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

listOneOf.add(hashMapOneOf);
}
Expand All @@ -141,23 +169,33 @@ public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
}

@Override
public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) {
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");
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");
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<");
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
*/
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Server> servers) {
return Utils.specifyCustomRequest(super.fromOperation(path, httpMethod, operation, servers));
public CodegenOperation fromOperation(
String path,
String httpMethod,
Operation operation,
List<Server> servers
) {
return Utils.specifyCustomRequest(
super.fromOperation(path, httpMethod, operation, servers)
);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
16 changes: 10 additions & 6 deletions generators/src/main/java/com/algolia/codegen/Utils.java
Original file line number Diff line number Diff line change
@@ -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<String> CUSTOM_METHOD = Sets.newHashSet("del", "get", "post", "put");

public static final Set<String> 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)) {
Expand Down
Loading