Skip to content

Commit 8ddbb4e

Browse files
committed
almost there
1 parent 96c6a0c commit 8ddbb4e

File tree

9 files changed

+91
-84
lines changed

9 files changed

+91
-84
lines changed

generators/src/main/java/com/algolia/codegen/cts/guides/GuidesGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void addSupportingFiles(List<SupportingFile> supportingFiles, String outp
4242
supportingFiles.add(
4343
new SupportingFile(
4444
"guides/" + client + "/" + f.getName(),
45-
"guides/" + language + outputFolder + f.getName().replace("\\.mustache", "") + extension
45+
"guides/" + language + outputFolder + f.getName().replace(".mustache", "") + extension
4646
)
4747
);
4848
}

generators/src/main/java/com/algolia/codegen/cts/lambda/DynamicSnippetLambda.java

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.algolia.codegen.cts.lambda;
22

33
import com.algolia.codegen.cts.tests.ParametersWithDataType;
4-
import com.algolia.codegen.cts.tests.Step;
4+
import com.algolia.codegen.cts.tests.Snippet;
55
import com.algolia.codegen.exceptions.CTSException;
66
import com.algolia.codegen.utils.Helpers;
77
import com.fasterxml.jackson.core.type.TypeReference;
@@ -28,10 +28,10 @@ public class DynamicSnippetLambda implements Mustache.Lambda {
2828
private final TemplatingExecutor executor;
2929
private final TemplatingEngineAdapter adaptor;
3030

31-
private ParametersWithDataType paramsType;
32-
private Map<String, CodegenOperation> operations;
31+
private final ParametersWithDataType paramsType;
32+
private final Map<String, CodegenOperation> operations;
3333

34-
private Map<String, Step> snippets;
34+
private final Map<String, Snippet> snippets;
3535

3636
public DynamicSnippetLambda(
3737
DefaultCodegen generator,
@@ -44,7 +44,7 @@ public DynamicSnippetLambda(
4444
this.paramsType = new ParametersWithDataType(models, language, client, true);
4545

4646
JsonNode snippetsFile = Helpers.readJsonFile("tests/CTS/guides/search/snippets.json");
47-
this.snippets = Json.mapper().convertValue(snippetsFile, new TypeReference<Map<String, Step>>() {});
47+
this.snippets = Json.mapper().convertValue(snippetsFile, new TypeReference<Map<String, Snippet>>() {});
4848

4949
// we can't access the default template manager, so we have to create our own
5050
TemplateManager templateManager = new TemplateManager(
@@ -59,13 +59,13 @@ public DynamicSnippetLambda(
5959

6060
@Override
6161
public void execute(Template.Fragment fragment, Writer writer) throws IOException, CTSException {
62-
String snippet = fragment.execute();
63-
Step step = snippets.get(snippet);
64-
if (step == null) {
65-
throw new CTSException("Cannot find snippet: " + snippet);
62+
String snippetName = fragment.execute();
63+
Snippet snippet = snippets.get(snippetName);
64+
if (snippet == null) {
65+
throw new CTSException("Cannot find snippet: " + snippetName);
6666
}
6767

68-
String operationId = step.method;
68+
String operationId = snippet.method;
6969

7070
CodegenOperation operation = operations.get(operationId);
7171
if (operation == null) {
@@ -74,15 +74,7 @@ public void execute(Template.Fragment fragment, Writer writer) throws IOExceptio
7474

7575
// set the method attributes
7676
Map<String, Object> context = (Map<String, Object>) fragment.context();
77-
context.put("method", operationId);
78-
context.put("isAsyncMethod", (boolean) operation.vendorExtensions.getOrDefault("x-asynchronous-helper", true));
79-
context.put("isHelper", (boolean) operation.vendorExtensions.getOrDefault("x-helper", false));
80-
context.put("hasParams", operation.hasParams);
81-
82-
// set the parameters
83-
// Map<String, Object> parameters = Json.mapper().readValue(params, Map.class);
84-
// System.out.println("parameters: " + parameters);
85-
// context.put("parameters", params);
77+
snippet.addMethodCall(context, paramsType, operation);
8678

8779
writer.write(adaptor.compileTemplate(executor, context, "tests/client/method.mustache"));
8880
}

generators/src/main/java/com/algolia/codegen/cts/manager/ScalaCTSManager.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.algolia.codegen.utils.Helpers;
55
import java.util.List;
66
import java.util.Map;
7+
import org.openapitools.codegen.SupportingFile;
78

89
public class ScalaCTSManager implements CTSManager {
910

generators/src/main/java/com/algolia/codegen/cts/tests/Snippet.java

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
package com.algolia.codegen.cts.tests;
22

3-
import java.util.Map;
3+
import static org.openapitools.codegen.utils.StringUtils.camelize;
4+
5+
import com.algolia.codegen.exceptions.CTSException;
6+
import com.algolia.codegen.utils.*;
7+
import com.fasterxml.jackson.core.JsonProcessingException;
8+
import java.util.*;
9+
import org.openapitools.codegen.CodegenOperation;
10+
import org.openapitools.codegen.CodegenResponse;
411

512
public class Snippet {
613

714
public String testName;
815
public boolean isSnippet;
916

17+
public String method;
1018
public Map<String, Object> parameters;
1119
public RequestOptions requestOptions;
1220

13-
public Snippet(String testName, Map<String, Object> parameters) {
21+
public Snippet(String method, String testName, Map<String, Object> parameters) {
22+
this.method = method;
1423
this.testName = testName;
1524
this.isSnippet = true;
1625
this.parameters = parameters;
@@ -24,9 +33,60 @@ public String toString() {
2433
sb.append("class Request {\n");
2534
sb.append(" testName: ").append(testName).append("\n");
2635
sb.append(" isSnippet").append(isSnippet).append("\n");
36+
sb.append(" method: ").append(method).append("\n");
2737
sb.append(" parameters: ").append(parameters).append("\n");
2838
sb.append(" requestOptions: ").append(requestOptions).append("\n");
2939
sb.append("}");
3040
return sb.toString();
3141
}
42+
43+
public void addMethodCall(Map<String, Object> context, ParametersWithDataType paramsType, CodegenOperation ope) throws CTSException {
44+
context.put("method", method);
45+
if (ope.returnType != null && ope.returnType.length() > 0) {
46+
context.put("returnType", camelize(ope.returnType));
47+
}
48+
49+
try {
50+
context.put("isGeneric", (boolean) ope.vendorExtensions.getOrDefault("x-is-generic", false));
51+
context.put("isCustomRequest", Helpers.CUSTOM_METHODS.contains(ope.operationIdOriginal));
52+
context.put("isAsyncMethod", (boolean) ope.vendorExtensions.getOrDefault("x-asynchronous-helper", true));
53+
context.put("hasParams", ope.hasParams);
54+
context.put("isHelper", (boolean) ope.vendorExtensions.getOrDefault("x-helper", false));
55+
56+
if (requestOptions != null) {
57+
context.put("hasRequestOptions", true);
58+
Map<String, Object> requestOptionsContext = new HashMap<>();
59+
if (requestOptions.queryParameters != null) {
60+
Map<String, Object> queryParameters = new HashMap<>();
61+
paramsType.enhanceParameters(requestOptions.queryParameters, queryParameters);
62+
requestOptionsContext.put("queryParameters", queryParameters);
63+
}
64+
if (requestOptions.headers != null) {
65+
Map<String, Object> headers = new HashMap<>();
66+
// convert the headers to an acceptable type
67+
paramsType.enhanceParameters(new HashMap<String, Object>(requestOptions.headers), headers);
68+
requestOptionsContext.put("headers", headers);
69+
}
70+
71+
context.put("requestOptions", requestOptionsContext);
72+
}
73+
74+
// Determines whether the endpoint is expected to return a response payload deserialized
75+
// and therefore a variable to store it into.
76+
context.put("hasResponse", true);
77+
78+
for (CodegenResponse response : ope.responses) {
79+
if (response.code.equals("204")) {
80+
context.put("hasResponse", false);
81+
}
82+
}
83+
84+
paramsType.enhanceParameters(parameters, context, ope);
85+
} catch (CTSException e) {
86+
e.setTestName((String) context.get("testName"));
87+
throw e;
88+
} catch (JsonProcessingException e) {
89+
throw new CTSException("Error while enhanching parameters for " + method, e);
90+
}
91+
}
3292
}

generators/src/main/java/com/algolia/codegen/cts/tests/SnippetsGenerator.java

Lines changed: 4 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
package com.algolia.codegen.cts.tests;
22

3-
import static org.openapitools.codegen.utils.StringUtils.camelize;
4-
53
import com.algolia.codegen.cts.manager.CTSManager;
6-
import com.algolia.codegen.exceptions.CTSException;
74
import com.algolia.codegen.utils.*;
85
import java.io.File;
96
import java.util.*;
107
import org.apache.commons.lang3.ArrayUtils;
118
import org.openapitools.codegen.CodegenModel;
129
import org.openapitools.codegen.CodegenOperation;
13-
import org.openapitools.codegen.CodegenResponse;
1410
import org.openapitools.codegen.SupportingFile;
1511

1612
public class SnippetsGenerator extends TestsGenerator {
@@ -66,7 +62,7 @@ private Map<String, Snippet[]> loadSnippets(Map<String, CodegenOperation> operat
6662
if (ope == null || !(boolean) ope.vendorExtensions.getOrDefault("x-helper", false)) {
6763
continue;
6864
}
69-
Snippet newSnippet = new Snippet(test.testName, step.parameters);
65+
Snippet newSnippet = new Snippet(step.method, test.testName, step.parameters);
7066
Snippet[] existing = snippets.get(step.method);
7167
if (existing == null) {
7268
snippets.put(step.method, new Snippet[] { newSnippet });
@@ -95,7 +91,6 @@ public void run(Map<String, CodegenModel> models, Map<String, CodegenOperation>
9591
for (Map.Entry<String, CodegenOperation> entry : operations.entrySet()) {
9692
String operationId = entry.getKey();
9793
CodegenOperation ope = entry.getValue();
98-
boolean isHelper = (boolean) ope.vendorExtensions.getOrDefault("x-helper", false);
9994

10095
if (!snippets.containsKey(operationId)) {
10196
continue;
@@ -112,55 +107,12 @@ public void run(Map<String, CodegenModel> models, Map<String, CodegenOperation>
112107
for (int i = 0; i < ops.size(); i++) {
113108
Map<String, Object> test = new HashMap<>();
114109
Snippet snippet = ops.get(i);
115-
test.put("method", operationId);
116-
String name = snippet.testName == null ? operationId : snippet.testName;
110+
String name = snippet.testName == null ? snippet.method : snippet.testName;
117111
test.put("testName", ops.size() > 1 ? name : "default");
118112
test.put("description", name);
119113
test.put("testIndex", i == 0 ? "" : i);
120-
if (ope.returnType != null && ope.returnType.length() > 0) {
121-
test.put("returnType", camelize(ope.returnType));
122-
}
123-
124-
try {
125-
test.put("isGeneric", (boolean) ope.vendorExtensions.getOrDefault("x-is-generic", false));
126-
test.put("isCustomRequest", Helpers.CUSTOM_METHODS.contains(ope.operationIdOriginal));
127-
test.put("isAsyncMethod", (boolean) ope.vendorExtensions.getOrDefault("x-asynchronous-helper", true));
128-
test.put("hasParams", ope.hasParams);
129-
test.put("isHelper", isHelper);
130-
131-
if (snippet.requestOptions != null) {
132-
test.put("hasRequestOptions", true);
133-
Map<String, Object> requestOptions = new HashMap<>();
134-
if (snippet.requestOptions.queryParameters != null) {
135-
Map<String, Object> queryParameters = new HashMap<>();
136-
paramsType.enhanceParameters(snippet.requestOptions.queryParameters, queryParameters);
137-
requestOptions.put("queryParameters", queryParameters);
138-
}
139-
if (snippet.requestOptions.headers != null) {
140-
Map<String, Object> headers = new HashMap<>();
141-
// convert the headers to an acceptable type
142-
paramsType.enhanceParameters(new HashMap<String, Object>(snippet.requestOptions.headers), headers);
143-
requestOptions.put("headers", headers);
144-
}
145-
test.put("requestOptions", requestOptions);
146-
}
147-
148-
// Determines whether the endpoint is expected to return a response payload deserialized
149-
// and therefore a variable to store it into.
150-
test.put("hasResponse", true);
151-
152-
for (CodegenResponse response : ope.responses) {
153-
if (response.code.equals("204")) {
154-
test.put("hasResponse", false);
155-
}
156-
}
157-
158-
paramsType.enhanceParameters(snippet.parameters, test, ope);
159-
tests.add(test);
160-
} catch (CTSException e) {
161-
e.setTestName((String) test.get("testName"));
162-
throw e;
163-
}
114+
snippet.addMethodCall(test, paramsType, ope);
115+
tests.add(test);
164116
}
165117
Map<String, Object> testObj = new HashMap<>();
166118
testObj.put("snippets", tests);

generators/src/main/java/com/algolia/codegen/cts/tests/TestsGenerator.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@
77
import java.io.File;
88
import java.nio.file.Files;
99
import java.nio.file.Paths;
10-
import java.util.ArrayList;
11-
import java.util.Collections;
12-
import java.util.List;
13-
import java.util.Map;
14-
import java.util.TreeMap;
10+
import java.util.*;
1511
import org.apache.commons.lang3.ArrayUtils;
1612
import org.openapitools.codegen.CodegenModel;
1713
import org.openapitools.codegen.CodegenOperation;

scripts/cli/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,17 @@ program
244244
.command('guides')
245245
.description('Generate the guides')
246246
.addArgument(args.language)
247+
.addArgument(args.clients)
247248
.option(flags.verbose.flag, flags.verbose.description)
248-
.action(async (langArg: LangArg, { verbose }) => {
249+
.action(async (langArg: LangArg, clientArg: string[], { verbose }) => {
250+
const { language, client, clientList } = transformSelection({
251+
langArg,
252+
clientArg,
253+
});
254+
249255
setVerbose(Boolean(verbose));
250256

251-
await guidesGenerateMany(generatorList({ language: 'javascript', client: ['search'], clientList: ['search'] }));
257+
await guidesGenerateMany(generatorList({ language, client, clientList }));
252258
});
253259

254260
program

scripts/install.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ _apic_complete() {
103103
cur="${COMP_WORDS[COMP_CWORD]}"
104104
lang="${COMP_WORDS[COMP_CWORD-1]}" # initial guess, but it might be before in some commands
105105
if [[ COMP_CWORD -eq 1 ]]; then
106-
COMPREPLY=($(compgen -W "build cts exec format generate playground release snippets" -- "$cur"))
106+
COMPREPLY=($(compgen -W "build cts exec format generate playground release snippets guides" -- "$cur"))
107107
else
108108
first="${COMP_WORDS[1]}"
109-
if [[ $first == "generate" || $first == "snippets" ]]; then
109+
if [[ $first == "generate" || $first == "snippets" || $first == "guides" ]]; then
110110
_apic_lang_client_complete 2
111111
elif [[ $first == "playground" ]]; then
112112
_apic_playground_complete

templates/java/tests/generateInnerParams.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{{/isNull}}
44
{{#isVerbatim}}
55
{{{value}}}
6-
{{/value}}
6+
{{/isVerbatim}}
77
{{#isString}}
88
"{{#lambda.escapeQuotes}}{{#lambda.escapeSlash}}{{{value}}}{{/lambda.escapeSlash}}{{/lambda.escapeQuotes}}"
99
{{/isString}}

0 commit comments

Comments
 (0)