Skip to content

Commit f26adb4

Browse files
committed
cts is generating
1 parent 7bc1416 commit f26adb4

16 files changed

+102
-77
lines changed

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

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -190,22 +190,11 @@ private void flattenBody(CodegenOperation ope) {
190190
return;
191191
}
192192

193-
// check for colision with other params
194-
for (CodegenProperty prop : bodyParam.getVars()) {
195-
for (CodegenParameter param : ope.allParams) {
196-
if (param.paramName.equals(prop.baseName)) {
197-
System.out.println(
198-
"Operation " +
199-
ope.operationId +
200-
" has body param " +
201-
bodyParam.paramName +
202-
" in colision with param " +
203-
param.paramName +
204-
", skipping flattening"
205-
);
206-
return;
207-
}
208-
}
193+
if (!canFlattenBody(ope)) {
194+
System.out.println(
195+
"Operation " + ope.operationId + " has body param " + bodyParam.paramName + " in colision with a parameter, skipping flattening"
196+
);
197+
return;
209198
}
210199

211200
bodyParam.vendorExtensions.put("x-flat-body", bodyParam.getVars().size() > 0);
@@ -259,6 +248,25 @@ private void flattenBody(CodegenOperation ope) {
259248
}
260249
}
261250

251+
public static boolean canFlattenBody(CodegenOperation ope) {
252+
if (ope.bodyParam == null || !ope.bodyParam.isModel) {
253+
return false;
254+
}
255+
256+
if (ope.allParams.size() == 1) {
257+
return true;
258+
}
259+
260+
for (CodegenProperty prop : ope.bodyParam.getVars()) {
261+
for (CodegenParameter param : ope.allParams) {
262+
if (param.paramName.equals(prop.baseName)) {
263+
return false;
264+
}
265+
}
266+
}
267+
return true;
268+
}
269+
262270
public static String toEnum(String value) {
263271
return INSTANCE.toEnumVarName(value, "String");
264272
}

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

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

3+
import com.algolia.codegen.AlgoliaGoGenerator;
34
import com.algolia.codegen.AlgoliaSwiftGenerator;
45
import com.algolia.codegen.exceptions.*;
56
import com.algolia.codegen.utils.*;
@@ -93,19 +94,8 @@ public void enhanceParameters(Map<String, Object> parameters, Map<String, Object
9394
operation.bodyParam.isModel &&
9495
operation.bodyParam.getVars().size() > 0
9596
) {
96-
// check for colision with other params
97-
boolean hasCollision = false;
98-
for (CodegenProperty prop : operation.bodyParam.getVars()) {
99-
for (CodegenParameter otherParam : operation.allParams) {
100-
if (otherParam.paramName.equals(prop.baseName)) {
101-
hasCollision = true;
102-
break;
103-
}
104-
}
105-
}
106-
if (!hasCollision) {
97+
if (AlgoliaGoGenerator.canFlattenBody(operation)) {
10798
// flatten the body params by skipping one level
108-
System.out.println("Flatten the body in " + operation.operationId);
10999
Map<String, Object> bodyParams = (Map<String, Object>) param.getValue();
110100
for (String nestedParam : bodyParams.keySet()) {
111101
for (CodegenProperty prop : operation.bodyParam.getVars()) {
@@ -117,7 +107,11 @@ public void enhanceParameters(Map<String, Object> parameters, Map<String, Object
117107
}
118108
}
119109
}
120-
// sortParameters(operation.bodyParam, parametersWithDataType);
110+
} else {
111+
// use the parameter as is
112+
Map<String, Object> paramWithType = traverseParams(param.getKey(), param.getValue(), specParam, "", 0, false);
113+
parametersWithDataType.add(paramWithType);
114+
parametersWithDataTypeMap.put((String) paramWithType.get("key"), paramWithType);
121115
}
122116
} else {
123117
Map<String, Object> paramWithType = traverseParams(param.getKey(), param.getValue(), specParam, "", 0, false);
@@ -126,9 +120,8 @@ public void enhanceParameters(Map<String, Object> parameters, Map<String, Object
126120
}
127121
}
128122
}
129-
} else if (language.equals("go") && parameters != null) {
123+
} else if (language.equals("go") && parameters != null && operation.bodyParam.getVars().size() > 0) {
130124
// also flatten when the body is the only parameter
131-
System.out.println("Skipping unique body in " + operation.operationId);
132125
for (String nestedParam : parameters.keySet()) {
133126
for (CodegenProperty prop : operation.bodyParam.getVars()) {
134127
if (prop.baseName.equals(nestedParam)) {
@@ -139,7 +132,6 @@ public void enhanceParameters(Map<String, Object> parameters, Map<String, Object
139132
}
140133
}
141134
}
142-
// sortParameters(operation.bodyParam, parametersWithDataType);
143135
} else {
144136
Map<String, Object> paramWithType = traverseParams(paramName, parameters, spec, "", 0, false);
145137
parametersWithDataType.add(paramWithType);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,12 @@ public void addMethodCall(Map<String, Object> context, ParametersWithDataType pa
5252
}
5353

5454
try {
55+
boolean isHelper = (boolean) ope.vendorExtensions.getOrDefault("x-helper", false);
5556
context.put("isGeneric", (boolean) ope.vendorExtensions.getOrDefault("x-is-generic", false));
5657
context.put("isCustomRequest", Helpers.CUSTOM_METHODS.contains(ope.operationIdOriginal));
5758
context.put("isAsyncMethod", (boolean) ope.vendorExtensions.getOrDefault("x-asynchronous-helper", true));
5859
context.put("hasParams", ope.hasParams);
59-
context.put("isHelper", (boolean) ope.vendorExtensions.getOrDefault("x-helper", false));
60+
context.put("isHelper", isHelper);
6061
context.put("hasRequestOptions", requestOptions != null);
6162

6263
if (requestOptions != null) {
@@ -85,7 +86,7 @@ public void addMethodCall(Map<String, Object> context, ParametersWithDataType pa
8586
}
8687
}
8788

88-
TestsGenerator.setOptionalParameters(ope, context, parameters);
89+
TestsGenerator.setOptionalParameters(ope, context, parameters, isHelper);
8990

9091
paramsType.enhanceParameters(parameters, context, ope);
9192
} catch (CTSException e) {

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ public void run(Map<String, CodegenModel> models, Map<String, CodegenOperation>
8080
testOut.put("autoCreateClient", test.autoCreateClient);
8181
testOut.put("useEchoRequester", true);
8282
testOut.put("isBenchmark", withBenchmark);
83+
84+
if (language.equals("go") && "`addApiKey` throws with invalid parameters".equals(test.testName)) {
85+
// skip this test because the body is flattened in go
86+
continue;
87+
}
88+
8389
for (Step step : test.steps) {
8490
Map<String, Object> stepOut = new HashMap<>();
8591
if (step.times > 1) stepOut.put("times", step.times);
@@ -156,7 +162,7 @@ public void run(Map<String, CodegenModel> models, Map<String, CodegenOperation>
156162
// default to true because most api calls are asynchronous
157163
testOut.put("isAsyncMethod", (boolean) ope.vendorExtensions.getOrDefault("x-asynchronous-helper", true));
158164

159-
setOptionalParameters(ope, stepOut, step.parameters);
165+
setOptionalParameters(ope, stepOut, step.parameters, isHelper);
160166
addRequestOptions(paramsType, step.requestOptions, stepOut);
161167

162168
methodCount++;
@@ -177,6 +183,7 @@ public void run(Map<String, CodegenModel> models, Map<String, CodegenOperation>
177183
((List<Map<String, Object>>) stepOut.getOrDefault("parametersWithDataType", new ArrayList<>())).stream()
178184
.anyMatch(item -> (boolean) item.getOrDefault("isNullObject", false) || (boolean) item.getOrDefault("isNull", false));
179185
if (isNotTestable) {
186+
System.out.println("Skipping test " + test.testName + " for " + language + " because of nil object");
180187
continue;
181188
}
182189
}

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

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.algolia.codegen.cts.tests;
22

3+
import com.algolia.codegen.AlgoliaGoGenerator;
34
import com.algolia.codegen.cts.manager.CTSManager;
45
import com.algolia.codegen.exceptions.CTSException;
56
import com.algolia.codegen.utils.*;
@@ -133,36 +134,29 @@ protected void addRequestOptions(ParametersWithDataType paramsType, RequestOptio
133134
}
134135
}
135136

136-
public static void setOptionalParameters(CodegenOperation ope, Map<String, Object> test, Map<String, Object> parameters) {
137+
public static void setOptionalParameters(
138+
CodegenOperation ope,
139+
Map<String, Object> test,
140+
Map<String, Object> parameters,
141+
boolean isHelper
142+
) {
137143
int bodyPropsOptional = 0;
138144
boolean actuallyHasOptional = false;
139-
if (ope.bodyParam != null && ope.bodyParam.isModel) {
140-
// check for colision with other params
141-
boolean hasCollision = false;
142-
Map<String, Object> paramBody = (Map<String, Object>) parameters.get(ope.bodyParam.paramName);
143-
if (ope.allParams.size() == 1) { // edge case where the body is already flattened
144-
paramBody = parameters;
145+
146+
if (AlgoliaGoGenerator.canFlattenBody(ope)) {
147+
bodyPropsOptional = (int) ope.bodyParam.getVars().stream().filter(prop -> !prop.required).count();
148+
149+
// edge case where the body is already flattened
150+
Map<String, Object> paramBody = paramBody = parameters;
151+
if (ope.allParams.size() > 1) {
152+
paramBody = (Map<String, Object>) parameters.get(ope.bodyParam.paramName);
145153
}
146154

147-
System.out.println(ope.bodyParam.paramName + " len " + ope.bodyParam.getVars().size());
148155
for (CodegenProperty prop : ope.bodyParam.getVars()) {
149-
for (CodegenParameter param : ope.allParams) {
150-
if (param.paramName.equals(prop.baseName)) {
151-
hasCollision = true;
152-
}
153-
}
154-
155-
if (paramBody != null) System.out.println(
156-
prop.baseName + " is required " + prop.required + " " + paramBody.containsKey(prop.baseName)
157-
);
158156
if (!prop.required && paramBody != null && paramBody.containsKey(prop.baseName)) {
159157
actuallyHasOptional = true;
160158
}
161159
}
162-
163-
if (!hasCollision) {
164-
bodyPropsOptional = (int) ope.bodyParam.getVars().stream().filter(prop -> !prop.required).count();
165-
}
166160
}
167161

168162
int totalOptional = ope.optionalParams.size() + bodyPropsOptional;
@@ -174,14 +168,29 @@ public static void setOptionalParameters(CodegenOperation ope, Map<String, Objec
174168
}
175169
}
176170

171+
// I can't figure out the correct condition for this one so it's harcoded for now
172+
boolean isSFFV = ope.operationId.equals("searchForFacetValues") && "composition".equals(ope.tags.get(0).getName());
173+
177174
// hasOptionalWrapper if there is more that one optional param, after the body has been
178175
// flattened, only relevant for go
179-
test.put("hasOptionalWrapper", (totalOptional > 1) && actuallyHasOptional);
180-
test.put("hasNilOptional", (totalOptional > 0) && !actuallyHasOptional);
181-
test.put("hasOptionalRequired", (totalOptional == 1) && actuallyHasOptional);
182-
183-
System.out.println(ope.operationId + " hasOptionalWrapper: " + test.get("hasOptionalWrapper"));
184-
System.out.println("hasNilOptional: " + test.get("hasNilOptional"));
185-
System.out.println("hasOptionalRequired: " + test.get("hasOptionalRequired"));
176+
test.put("hasOptionalWrapper", totalOptional > 1 && actuallyHasOptional && !isSFFV);
177+
test.put("hasInlineOptional", (totalOptional == 1 || isSFFV) && actuallyHasOptional);
178+
test.put("hasNilOptional", totalOptional > 0 && !actuallyHasOptional && !isHelper);
179+
180+
System.out.println(
181+
ope.operationId +
182+
" hasOptionalWrapper: " +
183+
test.get("hasOptionalWrapper") +
184+
" hasNilOptional: " +
185+
test.get("hasNilOptional") +
186+
" hasInlineOptional: " +
187+
test.get("hasInlineOptional") +
188+
" totalOptional: " +
189+
totalOptional +
190+
" actuallyHasOptional: " +
191+
actuallyHasOptional +
192+
" bodyPropsOptional: " +
193+
bodyPropsOptional
194+
);
186195
}
187196
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public void run(Map<String, CodegenModel> models, Map<String, CodegenOperation>
161161
test.put("hasParams", ope.hasParams);
162162
test.put("isHelper", isHelper);
163163

164-
setOptionalParameters(ope, test, req.parameters);
164+
setOptionalParameters(ope, test, req.parameters, isHelper);
165165
addRequestOptions(paramsType, req.requestOptions, test);
166166

167167
// Determines whether the endpoint is expected to return a response payload deserialized

templates/go/client.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,14 +286,14 @@ func reportError(format string, a ...any) error {
286286
}
287287

288288
// A wrapper for strict JSON decoding
289-
func newStrictDecoder(data []byte) *json.Decoder { //nolint:unused
289+
func newStrictDecoder(data []byte) *json.Decoder {
290290
dec := json.NewDecoder(bytes.NewBuffer(data))
291291
dec.DisallowUnknownFields()
292292
return dec
293293
}
294294

295295
// A wrapper for validating a struct, returns nil if value is not a struct
296-
func validateStruct(v any) error { //nolint:unused
296+
func validateStruct(v any) error {
297297
err := validator.New().Struct(v)
298298
validationErrors, ok := err.(validator.ValidationErrors)
299299
if ok && len(validationErrors) > 0 {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{#hasInlineOptional}}{{#parametersWithDataType}}{{^required}}{{^isFreeFormObject}}{{^isObject}}utils.ToPtr({{/isObject}}{{/isFreeFormObject}}{{> tests/generateParams}}{{^isFreeFormObject}}{{^isObject}}){{/isObject}}{{/isFreeFormObject}},{{/required}}{{/parametersWithDataType}}{{/hasInlineOptional}}

templates/go/tests/method.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
client.{{#lambda.titlecase}}{{method}}{{/lambda.titlecase}}({{#parametersWithDataType}}{{#required}}{{> tests/generateParams}},{{/required}}{{/parametersWithDataType}} {{#hasOptionalWrapper}}{{clientPrefix}}.New{{#lambda.titlecase}}{{method}}{{/lambda.titlecase}}Options(){{#parametersWithDataType}}{{^required}}.With{{#lambda.pascalcase}}{{{key}}}{{/lambda.pascalcase}}({{> tests/generateParams}}){{/required}}{{/parametersWithDataType}}{{#requestOptions}},{{/requestOptions}}{{/hasOptionalWrapper}}{{#hasNilOptional}}nil,{{/hasNilOptional}}{{#hasOptionalRequired}}{{#parametersWithDataType}}{{^required}}{{^isFreeFormObject}}utils.ToPtr({{/isFreeFormObject}}{{> tests/generateParams}}{{^isFreeFormObject}}){{/isFreeFormObject}},{{/required}}{{/parametersWithDataType}}{{/hasOptionalRequired}}{{#requestOptions}}{{#queryParameters.parametersWithDataType}}{{clientPrefix}}.WithQueryParam("{{{key}}}", {{> tests/generateInnerParams}}),{{/queryParameters.parametersWithDataType}}{{#headers.parametersWithDataType}}{{clientPrefix}}.WithHeaderParam("{{{key}}}", {{> tests/generateInnerParams}}),{{/headers.parametersWithDataType}} {{#timeouts.read}} ,{{clientPrefix}}.WithReadTimeout({{.}} * time.Millisecond), {{/timeouts.read}} {{#timeouts.write}} {{clientPrefix}}.WithWriteTimeout({{.}} * time.Millisecond), {{/timeouts.write}} {{#timeouts.connect}} ,{{clientPrefix}}.WithConnectTimeout({{.}} * time.Millisecond), {{/timeouts.connect}} {{/requestOptions}})
1+
client.{{#lambda.titlecase}}{{method}}{{/lambda.titlecase}}({{> tests/requiredParams}}{{> tests/optionalWrapper}}{{> tests/nilWrapper}}{{> tests/inlineOptional}}{{> tests/requestOptions}})
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{#hasNilOptional}}nil,{{/hasNilOptional}}

0 commit comments

Comments
 (0)