Skip to content

Commit 11e5256

Browse files
authored
Merge 3aad06f into eafef18
2 parents eafef18 + 3aad06f commit 11e5256

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+752
-647
lines changed

clients/algoliasearch-client-go/.golangci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ linters:
5050
presets:
5151
- comments
5252
- std-error-handling
53+
rules:
54+
- path: api_search.go
55+
linters:
56+
- wrapcheck
5357
formatters:
5458
enable:
5559
- gofmt

clients/algoliasearch-client-go/README.md

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,42 +41,39 @@ You can now import the Algolia API client in your project and play with it.
4141

4242

4343
```go
44-
import "github.com/algolia/algoliasearch-client-go/v4/algolia/search"
44+
import "github.com/algolia/algoliasearch-client-go/v4/algolia/next/search"
4545

4646
client, err := search.NewClient("YOUR_APP_ID", "YOUR_API_KEY")
4747

4848
// Add a new record to your Algolia index
49-
response, err := client.SaveObject(client.NewApiSaveObjectRequest(
50-
"<YOUR_INDEX_NAME>", map[string]any{"objectID": "id", "test": "val"},
51-
))
49+
saveResponse, err := client.SaveObject("<YOUR_INDEX_NAME>", map[string]any{"objectID": "id", "test": "val"})
5250
if err != nil {
5351
// handle the eventual error
5452
panic(err)
5553
}
5654

5755
// use the model directly
58-
print(response)
56+
print(saveResponse)
5957

6058
// Poll the task status to know when it has been indexed
61-
taskResponse, err := searchClient.WaitForTask("<YOUR_INDEX_NAME>", response.TaskID, nil, nil, nil)
59+
_, err = client.WaitForTask("<YOUR_INDEX_NAME>", saveResponse.TaskID)
6260
if err != nil {
6361
panic(err)
6462
}
6563

6664
// Fetch search results, with typo tolerance
67-
response, err := client.Search(client.NewApiSearchRequest(
68-
69-
search.NewEmptySearchMethodParams().SetRequests(
70-
[]search.SearchQuery{*search.SearchForHitsAsSearchQuery(
71-
search.NewEmptySearchForHits().SetIndexName("<YOUR_INDEX_NAME>").SetQuery("<YOUR_QUERY>").SetHitsPerPage(50))}),
72-
))
65+
response, err := client.Search([]search.SearchQuery{
66+
*search.SearchForHitsAsSearchQuery(search.NewSearchForHits("<YOUR_INDEX_NAME>").SetQuery("<YOUR_QUERY>").SetHitsPerPage(50)),
67+
}, nil)
7368
if err != nil {
7469
// handle the eventual error
7570
panic(err)
7671
}
7772

7873
// use the model directly
7974
print(response)
75+
76+
return 0
8077
```
8178

8279
For full documentation, visit the **[Algolia Go API Client](https://www.algolia.com/doc/libraries/go/)**.

config/clients.config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@
8888
"folder": "clients/algoliasearch-client-go",
8989
"gitRepoId": "algoliasearch-client-go",
9090
"packageVersion": "4.28.1",
91-
"modelFolder": "algolia",
92-
"apiFolder": "algolia",
91+
"modelFolder": "algolia/next",
92+
"apiFolder": "algolia/next",
9393
"dockerImage": "apic_base",
9494
"tests": {
9595
"extension": "_test.go",

generators/src/main/java/com/algolia/codegen/AlgoliaGoGenerator.java

Lines changed: 115 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.algolia.codegen;
22

3+
import com.algolia.codegen.cts.lambda.ScreamingSnakeCaseLambda;
34
import com.algolia.codegen.exceptions.*;
4-
import com.algolia.codegen.lambda.ScreamingSnakeCaseLambda;
55
import com.algolia.codegen.utils.*;
6+
import com.fasterxml.jackson.databind.DeserializationFeature;
7+
import com.fasterxml.jackson.databind.ObjectMapper;
68
import com.google.common.collect.ImmutableMap;
79
import com.google.common.collect.Iterables;
810
import com.samskivert.mustache.Mustache;
@@ -11,6 +13,7 @@
1113
import io.swagger.v3.oas.models.servers.Server;
1214
import java.io.File;
1315
import java.util.*;
16+
import java.util.stream.Collectors;
1417
import org.openapitools.codegen.*;
1518
import org.openapitools.codegen.languages.GoClientCodegen;
1619
import org.openapitools.codegen.model.ModelMap;
@@ -19,6 +22,9 @@
1922

2023
public class AlgoliaGoGenerator extends GoClientCodegen {
2124

25+
// This is used for the CTS generation
26+
private static final AlgoliaGoGenerator INSTANCE = new AlgoliaGoGenerator();
27+
2228
@Override
2329
public String getName() {
2430
return "algolia-go";
@@ -29,10 +35,9 @@ public void processOpts() {
2935
String client = (String) additionalProperties.get("client");
3036

3137
additionalProperties.put("packageName", client.equals("query-suggestions") ? "suggestions" : Helpers.camelize(client));
32-
additionalProperties.put("enumClassPrefix", true);
3338
additionalProperties.put("is" + Helpers.capitalize(Helpers.camelize(client)) + "Client", true);
3439

35-
String outputFolder = "algolia" + File.separator + client;
40+
String outputFolder = "algolia/next/" + client;
3641
setOutputDir(getOutputDir() + File.separator + outputFolder);
3742

3843
super.processOpts();
@@ -44,6 +49,7 @@ public void processOpts() {
4449
typeMapping.put("AnyType", "any");
4550

4651
modelNameMapping.put("range", "modelRange");
52+
typeMapping.put("integer", "int");
4753

4854
apiTestTemplateFiles.clear();
4955
modelTestTemplateFiles.clear();
@@ -54,7 +60,7 @@ public void processOpts() {
5460
supportingFiles.add(new SupportingFile("configuration.mustache", "", "configuration.go"));
5561
supportingFiles.add(new SupportingFile("client.mustache", "", "client.go"));
5662

57-
Helpers.addCommonSupportingFiles(supportingFiles, "../../");
63+
Helpers.addCommonSupportingFiles(supportingFiles, "../../../");
5864

5965
try {
6066
additionalProperties.put("packageVersion", Helpers.getClientConfigField("go", "packageVersion"));
@@ -78,6 +84,13 @@ public void processOpenAPI(OpenAPI openAPI) {
7884
super.processOpenAPI(openAPI);
7985
Helpers.generateServers(super.fromServers(openAPI.getServers()), additionalProperties);
8086
Timeouts.enrichBundle(openAPI, additionalProperties);
87+
additionalProperties.put(
88+
"appDescription",
89+
Arrays.stream(openAPI.getInfo().getDescription().split("\n"))
90+
.map(line -> "// " + line)
91+
.collect(Collectors.joining("\n"))
92+
.trim()
93+
);
8194
}
8295

8396
@Override
@@ -118,7 +131,8 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
118131
String modelName = entry.getKey();
119132
CodegenModel model = entry.getValue().getModels().get(0).getModel();
120133

121-
// for some reason the property additionalPropertiesIsAnyType is not propagated to the
134+
// for some reason the property additionalPropertiesIsAnyType is not propagated
135+
// to the
122136
// property
123137
for (CodegenProperty prop : model.getVars()) {
124138
ModelsMap propertyModel = models.get(prop.datatypeWithEnum);
@@ -140,9 +154,105 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
140154
@Override
141155
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> models) {
142156
OperationsMap operations = super.postProcessOperationsWithModels(objs, models);
157+
158+
// Flatten body params to remove the wrapping object
159+
for (CodegenOperation ope : operations.getOperations().getOperation()) {
160+
// clean up the description
161+
String[] lines = ope.unescapedNotes.split("\n");
162+
ope.notes = (lines[0] +
163+
"\n" +
164+
Arrays.stream(lines)
165+
.skip(1)
166+
.map(line -> "// " + line)
167+
.collect(Collectors.joining("\n"))).trim();
168+
169+
// enrich the params
170+
for (CodegenParameter param : ope.optionalParams) {
171+
param.nameInPascalCase = Helpers.capitalize(param.baseName);
172+
}
173+
174+
CodegenParameter bodyParam = ope.bodyParam;
175+
if (bodyParam != null) {
176+
flattenBody(ope);
177+
}
178+
179+
// If the optional param struct only has 1 param, we can remove the wrapper
180+
if (ope.optionalParams.size() == 1) {
181+
CodegenParameter param = ope.optionalParams.get(0);
182+
183+
// move it to required, it's easier to handle im mustache
184+
ope.optionalParams.clear();
185+
186+
ope.requiredParams.add(param);
187+
}
188+
}
189+
143190
ModelPruner.removeOrphanModelFiles(this, operations, models);
144191
Helpers.removeHelpers(operations);
145192
GenericPropagator.propagateGenericsToOperations(operations, models);
146193
return operations;
147194
}
195+
196+
private void flattenBody(CodegenOperation ope) {
197+
CodegenParameter bodyParam = ope.bodyParam;
198+
bodyParam.nameInPascalCase = Helpers.capitalize(bodyParam.baseName);
199+
if (!bodyParam.isModel) {
200+
return;
201+
}
202+
203+
if (!canFlattenBody(ope)) {
204+
System.out.println(
205+
"Operation " + ope.operationId + " has body param " + bodyParam.paramName + " in colision with a parameter, skipping flattening"
206+
);
207+
return;
208+
}
209+
210+
bodyParam.vendorExtensions.put("x-flat-body", bodyParam.getVars().size() > 0);
211+
212+
if (bodyParam.getVars().size() > 0) {
213+
ope.allParams.removeIf(param -> param.isBodyParam);
214+
ope.requiredParams.removeIf(param -> param.isBodyParam);
215+
ope.optionalParams.removeIf(param -> param.isBodyParam);
216+
}
217+
218+
for (CodegenProperty prop : bodyParam.getVars()) {
219+
prop.nameInLowerCase = toParamName(prop.baseName);
220+
221+
CodegenParameter param = new ObjectMapper()
222+
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
223+
.convertValue(prop, CodegenParameter.class);
224+
param.nameInPascalCase = Helpers.capitalize(prop.baseName);
225+
param.paramName = toParamName(prop.baseName);
226+
227+
if (prop.required) {
228+
ope.requiredParams.add(param);
229+
} else {
230+
ope.optionalParams.add(param);
231+
}
232+
ope.allParams.add(param);
233+
}
234+
}
235+
236+
public static boolean canFlattenBody(CodegenOperation ope) {
237+
if (ope.bodyParam == null || !ope.bodyParam.isModel) {
238+
return false;
239+
}
240+
241+
if (ope.allParams.size() == 1) {
242+
return true;
243+
}
244+
245+
for (CodegenProperty prop : ope.bodyParam.getVars()) {
246+
for (CodegenParameter param : ope.allParams) {
247+
if (param.paramName.equals(prop.baseName)) {
248+
return false;
249+
}
250+
}
251+
}
252+
return true;
253+
}
254+
255+
public static String toEnum(String value) {
256+
return INSTANCE.toEnumVarName(value, "String");
257+
}
148258
}

generators/src/main/java/com/algolia/codegen/AlgoliaJavascriptGenerator.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -236,20 +236,13 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
236236
continue;
237237
}
238238

239-
boolean hasBodyParams = !ope.bodyParams.isEmpty();
239+
boolean hasBodyParams = ope.bodyParam != null;
240240
boolean hasHeaderParams = !ope.headerParams.isEmpty();
241241
boolean hasQueryParams = !ope.queryParams.isEmpty();
242242
boolean hasPathParams = !ope.pathParams.isEmpty();
243243

244244
// If there is nothing but body params, we just check if it's a single param
245-
if (
246-
hasBodyParams &&
247-
!hasHeaderParams &&
248-
!hasQueryParams &&
249-
!hasPathParams &&
250-
ope.bodyParams.size() == 1 &&
251-
!ope.bodyParams.get(0).isArray
252-
) {
245+
if (hasBodyParams && !hasHeaderParams && !hasQueryParams && !hasPathParams && !ope.bodyParam.isArray) {
253246
// At this point the single parameter is already an object, to avoid double wrapping we skip
254247
// it
255248
ope.vendorExtensions.put("x-is-single-body-param", true);

generators/src/main/java/com/algolia/codegen/cts/AlgoliaCTSGenerator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ protected Builder<String, Lambda> addMustacheLambdas() {
102102
lambdas.put("escapeSlash", new EscapeSlashLambda());
103103
lambdas.put("escapeJSON", new EscapeJSONLambda());
104104
lambdas.put("replaceBacktick", new ReplaceBacktickLambda());
105+
lambdas.put("screamingSnakeCase", new ScreamingSnakeCaseLambda());
105106

106107
return lambdas;
107108
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class DynamicSnippetLambda implements Mustache.Lambda {
3232
private final Map<String, CodegenOperation> operations;
3333

3434
private final Map<String, Snippet> snippets;
35+
private final String language;
3536

3637
public DynamicSnippetLambda(
3738
DefaultCodegen generator,
@@ -41,6 +42,7 @@ public DynamicSnippetLambda(
4142
String client
4243
) {
4344
this.operations = operations;
45+
this.language = language;
4446
this.paramsType = new ParametersWithDataType(models, language, client, true);
4547

4648
JsonNode snippetsFile = Helpers.readJsonFile("tests/CTS/guides/" + client + ".json");
@@ -74,7 +76,7 @@ public void execute(Template.Fragment fragment, Writer writer) throws IOExceptio
7476

7577
// set the method attributes
7678
Map<String, Object> context = (Map<String, Object>) fragment.context();
77-
snippet.addMethodCall(context, paramsType, operation);
79+
snippet.addMethodCall(language, context, paramsType, operation);
7880

7981
writer.write(adaptor.compileTemplate(executor, context, "tests/method.mustache"));
8082
}

generators/src/main/java/com/algolia/codegen/lambda/ScreamingSnakeCaseLambda.java renamed to generators/src/main/java/com/algolia/codegen/cts/lambda/ScreamingSnakeCaseLambda.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.algolia.codegen.lambda;
1+
package com.algolia.codegen.cts.lambda;
22

33
import com.algolia.codegen.utils.Helpers;
44
import com.samskivert.mustache.Mustache;

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

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

3+
import com.algolia.codegen.AlgoliaGoGenerator;
34
import com.algolia.codegen.exceptions.GeneratorException;
45
import com.algolia.codegen.utils.*;
6+
import com.samskivert.mustache.Mustache.Lambda;
57
import java.util.*;
68
import org.openapitools.codegen.SupportingFile;
79

@@ -40,4 +42,9 @@ public void addSnippetsSupportingFiles(List<SupportingFile> supportingFiles, Str
4042
supportingFiles.add(new SupportingFile("snippets/.golangci.mustache", output + "/go/.golangci.yml"));
4143
supportingFiles.add(new SupportingFile("snippets/go.mod.mustache", output + "/go/go.mod"));
4244
}
45+
46+
@Override
47+
public void addMustacheLambdas(Map<String, Lambda> lambdas) {
48+
lambdas.put("toEnum", (fragment, writer) -> writer.write(AlgoliaGoGenerator.toEnum(fragment.execute())));
49+
}
4350
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,6 @@ public void addDataToBundle(Map<String, Object> bundle) throws GeneratorExceptio
5252

5353
@Override
5454
public void addMustacheLambdas(Map<String, Lambda> lambdas) {
55-
lambdas.put("javaEnum", (fragment, writer) -> writer.write(AlgoliaJavaGenerator.toEnum(fragment.execute())));
55+
lambdas.put("toEnum", (fragment, writer) -> writer.write(AlgoliaJavaGenerator.toEnum(fragment.execute())));
5656
}
5757
}

0 commit comments

Comments
 (0)